Skip to content

Commit 10718a0

Browse files
authored
Merge pull request #860 from bohdan-harniuk/760-fix-Throwable-when-IOException-is-thrown-during-plugin-generation
760: fixed throwable when io exception is thrown during plugin generation
2 parents f712cfc + 370596e commit 10718a0

File tree

3 files changed

+63
-26
lines changed

3 files changed

+63
-26
lines changed

resources/magento2/validation.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ validator.command.isNotValid=The {0} field does not contain a valid Magento 2 CL
2222
validator.module.noSuchModule=No such module {0}
2323
validator.file.alreadyExists={0} already exists
2424
validator.file.cantBeCreated={0} can't be created
25+
validator.file.cantBeCreatedWithException=The ''{0}'' cannot be created. Original message was: ''{1}''
2526
validator.file.isNotWritable=%s file is binary or has no document associations
2627
validator.file.noDocumentAssociations={0} file is binary or has no document associations
2728
validator.class.alreadyDeclared={0} already declared in the target module

src/com/magento/idea/magento2plugin/actions/generation/generator/PluginClassGenerator.java

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package com.magento.idea.magento2plugin.actions.generation.generator;
77

8+
import com.intellij.openapi.application.ApplicationManager;
89
import com.intellij.openapi.command.WriteCommandAction;
910
import com.intellij.openapi.editor.Document;
1011
import com.intellij.openapi.project.Project;
@@ -88,7 +89,7 @@ public PluginClassGenerator(
8889
*
8990
* @return PsiFile
9091
*/
91-
@SuppressWarnings("PMD.CognitiveComplexity")
92+
@SuppressWarnings({"PMD.CognitiveComplexity", "PMD.ExcessiveMethodLength"})
9293
@Override
9394
public PsiFile generate(final String actionName) {
9495
final PsiFile[] pluginFile = {null};
@@ -100,15 +101,27 @@ public PsiFile generate(final String actionName) {
100101
pluginClass = createPluginClass(actionName);
101102
}
102103
if (pluginClass == null) {
103-
final String errorMessage = validatorBundle.message(
104-
"validator.file.cantBeCreated",
105-
"Plugin Class"
106-
);
107-
JOptionPane.showMessageDialog(
108-
null,
109-
errorMessage,
110-
errorTitle,
111-
JOptionPane.ERROR_MESSAGE
104+
String errorMessage;
105+
106+
if (fileFromTemplateGenerator.getLastExceptionMessage() == null) {
107+
errorMessage = validatorBundle.message(
108+
"validator.file.cantBeCreated",
109+
"Plugin Class"
110+
);
111+
} else {
112+
errorMessage = validatorBundle.message(
113+
"validator.file.cantBeCreatedWithException",
114+
"Plugin Class",
115+
fileFromTemplateGenerator.getLastExceptionMessage()
116+
);
117+
}
118+
ApplicationManager.getApplication().invokeLater(
119+
() -> JOptionPane.showMessageDialog(
120+
null,
121+
errorMessage,
122+
errorTitle,
123+
JOptionPane.ERROR_MESSAGE
124+
)
112125
);
113126

114127
return;
@@ -132,11 +145,13 @@ public PsiFile generate(final String actionName) {
132145
"validator.file.alreadyExists",
133146
"Plugin Class"
134147
);
135-
JOptionPane.showMessageDialog(
136-
null,
137-
errorMessage,
138-
errorTitle,
139-
JOptionPane.ERROR_MESSAGE
148+
ApplicationManager.getApplication().invokeLater(
149+
() -> JOptionPane.showMessageDialog(
150+
null,
151+
errorMessage,
152+
errorTitle,
153+
JOptionPane.ERROR_MESSAGE
154+
)
140155
);
141156

142157
return;

src/com/magento/idea/magento2plugin/actions/generation/generator/util/FileFromTemplateGenerator.java

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import com.intellij.openapi.application.ApplicationManager;
1313
import com.intellij.openapi.command.CommandProcessor;
1414
import com.intellij.openapi.project.Project;
15-
import com.intellij.openapi.ui.Messages;
1615
import com.intellij.openapi.util.Ref;
1716
import com.intellij.openapi.util.text.StringUtil;
1817
import com.intellij.psi.PsiDirectory;
@@ -31,9 +30,11 @@
3130
import org.jetbrains.annotations.Nullable;
3231

3332
public class FileFromTemplateGenerator {
34-
private final Project project;
3533

36-
public FileFromTemplateGenerator(final Project project) {
34+
private final @NotNull Project project;
35+
private @Nullable String exceptionMessage;
36+
37+
public FileFromTemplateGenerator(final @NotNull Project project) {
3738
this.project = project;
3839
}
3940

@@ -44,27 +45,30 @@ public FileFromTemplateGenerator(final Project project) {
4445
* @param attributes Properties
4546
* @param baseDir PsiDirectory
4647
* @param actionName String
48+
*
4749
* @return PsiFile
4850
*/
49-
@Nullable
50-
public PsiFile generate(
51+
public @Nullable PsiFile generate(
5152
final @NotNull ModuleFileInterface moduleFile,
5253
final @NotNull Properties attributes,
5354
final @NotNull PsiDirectory baseDir,
5455
final @NotNull String actionName
5556
) {
5657
final Ref<PsiFile> fileRef = new Ref<>(null);
5758
final Ref<String> exceptionRef = new Ref<>(null);
59+
exceptionMessage = null;//NOPMD
5860
final String filePath = baseDir.getText().concat("/").concat(moduleFile.getFileName());
61+
5962
CommandProcessor.getInstance().executeCommand(project, () -> {
6063
final Runnable run = () -> {
6164
try {
62-
PsiFile file = createFile(moduleFile, filePath, baseDir, attributes);
65+
final PsiFile file = createFile(moduleFile, filePath, baseDir, attributes);
66+
6367
if (file != null) {
6468
fileRef.set(file);
6569
}
66-
} catch (IncorrectOperationException | IOException var9) {
67-
exceptionRef.set(var9.getMessage());
70+
} catch (IncorrectOperationException | IOException exception) {
71+
exceptionRef.set(exception.getMessage());
6872
}
6973
};
7074
ApplicationManager.getApplication().runWriteAction(run);
@@ -73,11 +77,20 @@ public PsiFile generate(
7377
if (exceptionRef.isNull()) {
7478
return fileRef.get();
7579
}
80+
exceptionMessage = exceptionRef.get();
7681

77-
Messages.showErrorDialog(exceptionRef.get(), actionName);
7882
return null;
7983
}
8084

85+
/**
86+
* Get last thrown exception message if exists.
87+
*
88+
* @return String
89+
*/
90+
public @Nullable String getLastExceptionMessage() {
91+
return exceptionMessage;
92+
}
93+
8194
@Nullable
8295
private PsiFile createFile(
8396
final @NotNull ModuleFileInterface moduleFile,
@@ -89,13 +102,19 @@ private PsiFile createFile(
89102
final String fileName = path.get(path.size() - 1);
90103
final PsiFile fileTemplate = createFileFromTemplate(
91104
getTemplateManager(),
92-
baseDir, moduleFile.getTemplate(), attributes, fileName, moduleFile.getLanguage());
105+
baseDir,
106+
moduleFile.getTemplate(),
107+
attributes,
108+
fileName,
109+
moduleFile.getLanguage()
110+
);
111+
93112
if (fileTemplate == null) {
94113
throw new IncorrectOperationException("Template not found!");
95114
} else {
96115
PsiElement file;
97-
98116
file = baseDir.add(fileTemplate);
117+
99118
if (file instanceof PsiFile) {
100119
return (PsiFile)file;
101120
} else {
@@ -125,6 +144,7 @@ public PsiFile createFileFromTemplate(
125144
final @NotNull Language language
126145
) throws IOException {
127146
FileTemplate fileTemplate;
147+
128148
try {
129149
fileTemplate = templateManager.getInternalTemplate(templateName);
130150
} catch (IllegalStateException e) {
@@ -140,6 +160,7 @@ public PsiFile createFileFromTemplate(
140160
true,
141161
false
142162
);
163+
143164
if (fileTemplate.isReformatCode()) {
144165
CodeStyleManager.getInstance(project).reformat(file);
145166
}

0 commit comments

Comments
 (0)