Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

saner exception handling #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/main/java/com/cadrlife/jhaml/JHamlBatchConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public List<File> convertAllInPath(File startDirectory) throws IOException {

@Override
protected void handleFile(File file, Collection<File> results) throws IOException {
try {
if (file.getName().endsWith("." + hamlExtension)) {
String warning = warning(file.getName());
String gsp = new JHaml()
Expand All @@ -38,6 +39,16 @@ protected void handleFile(File file, Collection<File> results) throws IOExceptio
}
}
results.add(file);
} catch (RuntimeException e) {
if (e instanceof JHamlParseException) {
RuntimeException ex = new JHamlParseException(e.getMessage(),
((JHamlParseException) e).getLineNumber(), file);
throw ex;
}
else {
throw e;
}
}
}

private String warning(String filename) {
Expand Down Expand Up @@ -86,5 +97,4 @@ public void setOutputModificationWarning(boolean outputModificationWarning) {
public boolean isOutputModificationWarning() {
return outputModificationWarning;
}

}
10 changes: 10 additions & 0 deletions src/main/java/com/cadrlife/jhaml/JHamlParseException.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
package com.cadrlife.jhaml;
import java.io.File;

public class JHamlParseException extends RuntimeException {
private final int lineNumber;
private final File file;

public JHamlParseException(String message) {
super(message);
lineNumber = -1;
this.file = null;
}

public JHamlParseException(String message, int lineNumber) {
super("Line " + lineNumber + ": " + message);
this.lineNumber = lineNumber;
this.file = null;
}

public JHamlParseException(String message, int lineNumber, File file) {
super("File " + file + ", " + message);
this.lineNumber = lineNumber;
this.file = file;
}

public int getLineNumber() {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/cadrlife/jhaml/JHamlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,5 @@ public void escapeAtBeginningOfLine() {
public void whitespaceAtEndIsIgnored() {
assertEquals("<div>\n <p>Hello</p>\n</div>", jhaml.parse("%div\n %p Hello\n "));
}
}
}