Cleanup exception messages

This commit is contained in:
Clément Fournier 2020-09-02 03:33:56 +02:00
parent a6e1f634f5
commit 2d9d74e1ec
7 changed files with 45 additions and 24 deletions

View File

@ -27,9 +27,7 @@ public final class ApexParser implements Parser {
final Compilation astRoot = CompilerService.INSTANCE.parseApex(task.getTextDocument());
if (astRoot == null) {
throw new ParseException("Couldn't parse the source - there is not root node - Syntax Error??");
}
assert astRoot != null : "Normally replaced by Compilation.INVALID";
final ApexTreeBuilder treeBuilder = new ApexTreeBuilder(task.getTextDocument(), task.getCommentMarker());
AbstractApexNode<Compilation> treeRoot = treeBuilder.build(astRoot);
@ -37,7 +35,7 @@ public final class ApexParser implements Parser {
fileNode.setNoPmdComments(treeBuilder.getSuppressMap());
return fileNode;
} catch (apex.jorje.services.exception.ParseException e) {
throw new ParseException(e);
throw new ParseException(e).setFileName(task.getFileDisplayName());
}
}
}

View File

@ -6,6 +6,7 @@ package net.sourceforge.pmd.lang.ast;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.nullness.qual.NonNull;
import net.sourceforge.pmd.util.document.TextFile;
@ -55,6 +56,22 @@ public class FileAnalysisException extends RuntimeException {
return filename;
}
@Override
public String getMessage() {
return errorKind() + StringUtils.uncapitalize(positionToString()) + ": " + super.getMessage();
}
protected String errorKind() {
return "Error";
}
protected String positionToString() {
if (hasFileName()) {
return " in file '" + getFileName() + "'";
}
return "";
}
/**
* Wraps the cause into an analysis exception. If it is itself an analysis

View File

@ -40,11 +40,6 @@ public class ParseException extends FileAnalysisException {
this.currentToken = null;
}
public ParseException(String message, Throwable cause) {
super(message, cause);
this.currentToken = null;
}
public ParseException(String message, JavaccToken token) {
super(message);
this.currentToken = token;
@ -59,6 +54,11 @@ public class ParseException extends FileAnalysisException {
currentToken = currentTokenVal;
}
@Override
protected String errorKind() {
return "Parse exception";
}
/**
* It uses "currentToken" and "expectedTokenSequences" to generate a parse
* error message and returns it. If this object has been created

View File

@ -53,12 +53,14 @@ public final class TokenMgrError extends FileAnalysisException {
return column;
}
@Override
protected String positionToString() {
return super.positionToString() + " at line " + line + ", column " + column;
}
@Override
public String getMessage() {
String leader = hasFileName() ? "Lexical error in file " + getFileName() : "Lexical error";
return leader + " at line " + line + ", column " + column + ". Encountered: " + super.getMessage();
protected String errorKind() {
return "Lexical error";
}
/**

View File

@ -52,7 +52,7 @@ public final class JavaEscapeReader extends BackslashEscapeReader {
return Chars.wrap(Character.toString(c));
} catch (NumberFormatException | IndexOutOfBoundsException e) {
throw new MalformedSourceException("Invalid escape sequence", e, posOfFirstBackSlash, getLine(posOfFirstBackSlash), getColumn(posOfFirstBackSlash));
throw new MalformedSourceException("Invalid unicode escape " + " at line", e, getLine(posOfFirstBackSlash), getColumn(posOfFirstBackSlash));
}
}

View File

@ -5,25 +5,29 @@
package net.sourceforge.pmd.lang.ast.impl.javacc.io;
import net.sourceforge.pmd.lang.ast.FileAnalysisException;
import net.sourceforge.pmd.lang.ast.TokenMgrError;
/**
*
* A {@link FileAnalysisException} thrown when the source format is invalid,
* for example if some unicode escapes cannot be translated.
*/
public class MalformedSourceException extends FileAnalysisException {
private final int offset;
private final int line;
private final int col;
public MalformedSourceException(String message, Throwable cause, int offset, int line, int col) {
public MalformedSourceException(String message, Throwable cause, int line, int col) {
super(message, cause);
this.offset = offset;
this.line = line;
this.col = col;
}
public TokenMgrError toLexException(String filename) {
return new TokenMgrError(line, col, filename, getMessage(), getCause());
@Override
protected String positionToString() {
return super.positionToString() + " at line " + line + ", column " + col;
}
@Override
protected String errorKind() {
return "Source format error";
}
}

View File

@ -14,7 +14,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import net.sourceforge.pmd.lang.ast.TokenMgrError;
import net.sourceforge.pmd.lang.ast.impl.javacc.io.MalformedSourceException;
import net.sourceforge.pmd.lang.ast.test.BaseParsingHelper;
import net.sourceforge.pmd.lang.java.BaseJavaTreeDumpTest;
import net.sourceforge.pmd.lang.java.JavaParsingHelper;
@ -38,8 +38,8 @@ public class ParserCornersTest extends BaseJavaTreeDumpTest {
@Test
public void testInvalidUnicodeEscape() {
expect.expect(TokenMgrError.class); // previously Error
expect.expectMessage("Lexical error at line 1, column 2. Encountered: Invalid unicode escape");
expect.expect(MalformedSourceException.class); // previously Error
expect.expectMessage("Source format error at line 1, column 1: Invalid unicode escape");
java.parse("\\u00k0");
}