Rearrange the methods on ErrorReporter to be more convenient.

Test: m product-config-test && java -jar out/host/linux-x86/testcases/product-config-test/product-config-test.jar
Change-Id: Icd25d2d0897df99ba29de52cd08a42cd9e4b9514
This commit is contained in:
Joe Onorato 2021-01-19 21:21:13 -08:00
parent 6edf0ec2ed
commit 0c7e0c0e3c
4 changed files with 62 additions and 23 deletions

View File

@ -49,6 +49,16 @@ public class ErrorReporter {
*/ */
private boolean mHadError; private boolean mHadError;
public static class FatalException extends RuntimeException {
FatalException(String message) {
super(message);
}
FatalException(String message, Throwable chain) {
super(message, chain);
}
}
/** /**
* Whether errors are errors, warnings or hidden. * Whether errors are errors, warnings or hidden.
*/ */
@ -127,6 +137,35 @@ public class ErrorReporter {
public String getHelp() { public String getHelp() {
return mHelp; return mHelp;
} }
/**
* Add an error with no source position.
*/
public void add(String message) {
ErrorReporter.this.add(this, false, new Position(), message);
}
/**
* Add an error.
*/
public void add(Position pos, String message) {
ErrorReporter.this.add(this, false, pos, message);
}
/**
* Add an error with no source position, and throw a FatalException, stopping processing
* immediately.
*/
public void fatal(String message) {
ErrorReporter.this.add(this, true, new Position(), message);
}
/**
* Add an error, and throw a FatalException, stopping processing immediately.
*/
public void fatal(Position pos, String message) {
ErrorReporter.this.add(this, true, pos, message);
}
} }
/** /**
@ -154,6 +193,13 @@ public class ErrorReporter {
public String getMessage() { public String getMessage() {
return mMessage; return mMessage;
} }
@Override
public String toString() {
return mPosition
+ "[" + mCategory.getLevel().getLabel() + " " + mCategory.getCode() + "] "
+ mMessage;
}
} }
private void initLocked() { private void initLocked() {
@ -190,23 +236,17 @@ public class ErrorReporter {
} }
} }
/**
* Add an error with no source position.
*/
public void add(Category category, String message) {
add(category, new Position(), message);
}
/** /**
* Add an error. * Add an error.
*/ */
public void add(Category category, Position pos, String message) { private void add(Category category, boolean fatal, Position pos, String message) {
synchronized (mEntries) { synchronized (mEntries) {
initLocked(); initLocked();
if (mCategories.get(category.getCode()) != category) { if (mCategories.get(category.getCode()) != category) {
throw new RuntimeException("Errors.Category used from the wrong Errors object."); throw new RuntimeException("Errors.Category used from the wrong Errors object.");
} }
mEntries.add(new Entry(category, pos, message)); final Entry entry = new Entry(category, pos, message);
mEntries.add(entry);
final Level level = category.getLevel(); final Level level = category.getLevel();
if (level == Level.WARNING || level == Level.ERROR) { if (level == Level.WARNING || level == Level.ERROR) {
mHadWarningOrError = true; mHadWarningOrError = true;
@ -214,6 +254,9 @@ public class ErrorReporter {
if (level == Level.ERROR) { if (level == Level.ERROR) {
mHadError = true; mHadError = true;
} }
if (fatal) {
throw new FatalException(entry.toString());
}
} }
} }
@ -250,13 +293,10 @@ public class ErrorReporter {
public void printErrors(PrintStream out) { public void printErrors(PrintStream out) {
synchronized (mEntries) { synchronized (mEntries) {
for (Entry entry: mEntries) { for (Entry entry: mEntries) {
final Category category = entry.getCategory(); if (entry.getCategory().getLevel() == Level.HIDDEN) {
final Level level = category.getLevel();
if (level == Level.HIDDEN) {
continue; continue;
} }
out.println(entry.getPosition() + "[" + level.getLabel() + " " out.println(entry.toString());
+ category.getCode() + "] " + entry.getMessage());
} }
} }
} }

View File

@ -38,7 +38,7 @@ public class Main {
// TODO: Get the variables that were defined in starlark and use that to write // TODO: Get the variables that were defined in starlark and use that to write
// out the make, soong and bazel input files. // out the make, soong and bazel input files.
mErrors.add(mErrors.ERROR_COMMAND_LINE, "asdf"); mErrors.ERROR_COMMAND_LINE.add("asdf");
throw new RuntimeException("poop"); throw new RuntimeException("poop");
} }

View File

@ -96,14 +96,14 @@ public class Options {
mIndex++; mIndex++;
} }
} catch (ParseException ex) { } catch (ParseException ex) {
mErrors.add(mErrors.ERROR_COMMAND_LINE, ex.getMessage()); mErrors.ERROR_COMMAND_LINE.add(ex.getMessage());
} }
return mResult; return mResult;
} }
private void addWarning(Errors.Category category, String message) { private void addWarning(Errors.Category category, String message) {
mErrors.add(category, message); category.add(message);
} }
private String getNextNonFlagArg() { private String getNextNonFlagArg() {
@ -133,12 +133,11 @@ public class Options {
final int code = requireNextNumberArg(arg); final int code = requireNextNumberArg(arg);
final Errors.Category category = mErrors.getCategories().get(code); final Errors.Category category = mErrors.getCategories().get(code);
if (category == null) { if (category == null) {
mErrors.add(mErrors.WARNING_UNKNOWN_COMMAND_LINE_ERROR, mErrors.WARNING_UNKNOWN_COMMAND_LINE_ERROR.add("Unknown error code: " + code);
"Unknown error code: " + code);
return; return;
} }
if (!category.isLevelSettable()) { if (!category.isLevelSettable()) {
mErrors.add(mErrors.ERROR_COMMAND_LINE, "Can't set level for error " + code); mErrors.ERROR_COMMAND_LINE.add("Can't set level for error " + code);
return; return;
} }
category.setLevel(level); category.setLevel(level);

View File

@ -30,7 +30,7 @@ public class ErrorReporterTest {
public void testAdding() { public void testAdding() {
TestErrors errors = new TestErrors(); TestErrors errors = new TestErrors();
errors.add(errors.ERROR, new Position("a", 12), "Errrororrrr"); errors.ERROR.add(new Position("a", 12), "Errrororrrr");
Assert.assertTrue(errors.hadWarningOrError()); Assert.assertTrue(errors.hadWarningOrError());
Assert.assertTrue(errors.hadError()); Assert.assertTrue(errors.hadError());
@ -66,7 +66,7 @@ public class ErrorReporterTest {
public void testWarning() { public void testWarning() {
TestErrors errors = new TestErrors(); TestErrors errors = new TestErrors();
errors.add(errors.WARNING, "Waaaaarninggggg"); errors.WARNING.add("Waaaaarninggggg");
Assert.assertTrue(errors.hadWarningOrError()); Assert.assertTrue(errors.hadWarningOrError());
Assert.assertFalse(errors.hadError()); Assert.assertFalse(errors.hadError());
@ -80,7 +80,7 @@ public class ErrorReporterTest {
public void testHidden() { public void testHidden() {
TestErrors errors = new TestErrors(); TestErrors errors = new TestErrors();
errors.add(errors.HIDDEN, "Hidddeennn"); errors.HIDDEN.add("Hidddeennn");
Assert.assertFalse(errors.hadWarningOrError()); Assert.assertFalse(errors.hadWarningOrError());
Assert.assertFalse(errors.hadError()); Assert.assertFalse(errors.hadError());