provide file to blame on exceptions

This commit is contained in:
marhali 2022-02-03 21:01:54 +01:00
parent 778490b8d8
commit d078e3d8f6
5 changed files with 24 additions and 21 deletions

View File

@ -1,6 +1,5 @@
package de.marhali.easyi18n.io;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
@ -12,6 +11,7 @@ import de.marhali.easyi18n.model.*;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
@ -37,18 +37,15 @@ public class IOHandler {
this.parserStrategyType = settings.getParserStrategy();
this.parserStrategy = parserStrategyType.getStrategy()
.getDeclaredConstructor(SettingsState.class).newInstance(settings);
Logger.getInstance(IOHandler.class).debug("Using: ",
settings.getFolderStrategy(), settings.getParserStrategy(), settings.getFilePattern());
}
/**
* Reads translation files from the local project into our data structure. <br>
* <b>Note:</b> This method needs to be called from a Read-Action-Context (see ApplicationManager)
* @return Translation data based on the configured strategies
* @throws Exception Could not read translation data
* @throws IOException Could not read translation data
*/
public @NotNull TranslationData read() throws Exception {
public @NotNull TranslationData read() throws IOException {
String localesPath = this.settings.getLocalesPath();
if(localesPath == null || localesPath.isEmpty()) {
@ -65,7 +62,11 @@ public class IOHandler {
List<TranslationFile> translationFiles = this.folderStrategy.analyzeFolderStructure(localesDirectory);
for(TranslationFile file : translationFiles) {
this.parserStrategy.read(file, data);
try {
this.parserStrategy.read(file, data);
} catch(Exception ex) {
throw new IOException(file + "\n\n" + ex.getMessage(), ex);
}
}
return data;
@ -75,9 +76,9 @@ public class IOHandler {
* Writes the provided translation data to the local project files <br>
* <b>Note:</b> This method must be called from an Write-Action-Context (see ApplicationManager)
* @param data Cached translation data to save
* @throws Exception Write action failed
* @throws IOException Write action failed
*/
public void write(@NotNull TranslationData data) throws Exception {
public void write(@NotNull TranslationData data) throws IOException {
String localesPath = this.settings.getLocalesPath();
if(localesPath == null || localesPath.isEmpty()) {
@ -88,7 +89,11 @@ public class IOHandler {
this.folderStrategy.constructFolderStructure(localesPath, this.parserStrategyType, data);
for(TranslationFile file : translationFiles) {
this.parserStrategy.write(data, file);
try {
this.parserStrategy.write(data, file);
} catch (Exception ex) {
throw new IOException(file + "\n\n" + ex.getMessage(), ex);
}
}
}
}

View File

@ -4,7 +4,6 @@ import de.marhali.easyi18n.model.*;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.Objects;
/**
@ -24,14 +23,14 @@ public abstract class ParserStrategy {
* @param file File to read from
* @param data Target translation data to save the parsed data
*/
public abstract void read(@NotNull TranslationFile file, @NotNull TranslationData data) throws IOException;
public abstract void read(@NotNull TranslationFile file, @NotNull TranslationData data) throws Exception;
/**
* Writes the relevant data to the translation file (consider namespace and locale)
* @param data Translation data cache
* @param file Target translation file
*/
public abstract void write(@NotNull TranslationData data, @NotNull TranslationFile file) throws IOException;
public abstract void write(@NotNull TranslationData data, @NotNull TranslationFile file) throws Exception;
/**
* Determines translation node to use for parsing

View File

@ -28,7 +28,7 @@ public class JsonParserStrategy extends ParserStrategy {
}
@Override
public void read(@NotNull TranslationFile file, @NotNull TranslationData data) throws IOException {
public void read(@NotNull TranslationFile file, @NotNull TranslationData data) throws Exception {
data.addLocale(file.getLocale());
VirtualFile vf = file.getVirtualFile();
@ -43,7 +43,7 @@ public class JsonParserStrategy extends ParserStrategy {
}
@Override
public void write(@NotNull TranslationData data, @NotNull TranslationFile file) throws IOException {
public void write(@NotNull TranslationData data, @NotNull TranslationFile file) throws Exception {
TranslationNode targetNode = super.getTargetNode(data, file);
JsonObject output = new JsonObject();

View File

@ -10,7 +10,6 @@ import de.marhali.easyi18n.model.TranslationNode;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
@ -26,7 +25,7 @@ public class PropertiesParserStrategy extends ParserStrategy {
}
@Override
public void read(@NotNull TranslationFile file, @NotNull TranslationData data) throws IOException {
public void read(@NotNull TranslationFile file, @NotNull TranslationData data) throws Exception {
data.addLocale(file.getLocale());
VirtualFile vf = file.getVirtualFile();
@ -41,7 +40,7 @@ public class PropertiesParserStrategy extends ParserStrategy {
}
@Override
public void write(@NotNull TranslationData data, @NotNull TranslationFile file) throws IOException {
public void write(@NotNull TranslationData data, @NotNull TranslationFile file) throws Exception {
TranslationNode targetNode = super.getTargetNode(data, file);
TranslationData targetData = new TranslationData(data.getLocales(), targetNode);

View File

@ -9,10 +9,10 @@ import de.marhali.easyi18n.model.TranslationFile;
import de.marhali.easyi18n.model.TranslationNode;
import org.jetbrains.annotations.NotNull;
import thito.nodeflow.config.MapSection;
import thito.nodeflow.config.Section;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
@ -27,7 +27,7 @@ public class YamlParserStrategy extends ParserStrategy {
}
@Override
public void read(@NotNull TranslationFile file, @NotNull TranslationData data) throws IOException {
public void read(@NotNull TranslationFile file, @NotNull TranslationData data) throws Exception {
data.addLocale(file.getLocale());
VirtualFile vf = file.getVirtualFile();
@ -40,7 +40,7 @@ public class YamlParserStrategy extends ParserStrategy {
}
@Override
public void write(@NotNull TranslationData data, @NotNull TranslationFile file) throws IOException {
public void write(@NotNull TranslationData data, @NotNull TranslationFile file) throws Exception {
TranslationNode targetNode = super.getTargetNode(data, file);
Section output = new MapSection();