Refactored some of the error management code
This commit is contained in:
parent
ad2375e2bd
commit
c4e3740dfb
|
@ -3,7 +3,7 @@ package error;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prints error messages.
|
* Prints error messages and potentially terminates the application.
|
||||||
*/
|
*/
|
||||||
public class ErrorManager {
|
public class ErrorManager {
|
||||||
|
|
||||||
|
@ -15,27 +15,13 @@ public class ErrorManager {
|
||||||
public static final String ANSI_PURPLE = "\u001B[35m";
|
public static final String ANSI_PURPLE = "\u001B[35m";
|
||||||
|
|
||||||
public static void generateError(LispException lispException) {
|
public static void generateError(LispException lispException) {
|
||||||
generateError(lispException.getMessage(), lispException.getSeverity());
|
String color = (lispException.getSeverity() >= CRITICAL_LEVEL) ? ANSI_PURPLE : ANSI_RED;
|
||||||
}
|
String formattedMessage = MessageFormat.format("{0}error: {1}{2}", color, lispException.getMessage(),
|
||||||
|
ANSI_RESET);
|
||||||
/**
|
|
||||||
* Prints out the specified error message to the console and decides whether
|
|
||||||
* or not to terminate the currently running program.
|
|
||||||
*
|
|
||||||
* @param message
|
|
||||||
* the error message
|
|
||||||
* @param level
|
|
||||||
* the "criticality" level of the error
|
|
||||||
* @postcondition If <code>level >= CRITICAL_LEVEL</code> the currently
|
|
||||||
* running program has been terminated.
|
|
||||||
*/
|
|
||||||
public static void generateError(String message, int level) {
|
|
||||||
String color = (level >= CRITICAL_LEVEL) ? ANSI_PURPLE : ANSI_RED;
|
|
||||||
String formattedMessage = MessageFormat.format("{0}error: {1}{2}", color, message, ANSI_RESET);
|
|
||||||
|
|
||||||
System.out.println(formattedMessage);
|
System.out.println(formattedMessage);
|
||||||
|
|
||||||
if (level >= CRITICAL_LEVEL) {
|
if (lispException.getSeverity() >= CRITICAL_LEVEL) {
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,8 +32,8 @@ public class LispInterpreter {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Evaluate the S-expressions found in the file given as a command-line
|
* Evaluate the S-expressions found in the file given as a command-line
|
||||||
* argument and print the results to the console. If no file name was
|
* argument and print the results to the console. If no file name was given,
|
||||||
* given, retrieve the S-expressions from standard input.
|
* retrieve the S-expressions from standard input.
|
||||||
*
|
*
|
||||||
* @param args
|
* @param args
|
||||||
* the command-line arguments:
|
* the command-line arguments:
|
||||||
|
@ -51,7 +51,20 @@ public class LispInterpreter {
|
||||||
try {
|
try {
|
||||||
parser = new LispParser(new FileInputStream(args[0]), args[0]);
|
parser = new LispParser(new FileInputStream(args[0]), args[0]);
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
ErrorManager.generateError(e.getMessage(), ErrorManager.CRITICAL_LEVEL);
|
ErrorManager.generateError(new LispException() {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getSeverity() {
|
||||||
|
return ErrorManager.CRITICAL_LEVEL;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMessage() {
|
||||||
|
return e.getMessage();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// no file name was given, create a 'LispParser' on standard input
|
// no file name was given, create a 'LispParser' on standard input
|
||||||
|
@ -75,7 +88,20 @@ public class LispInterpreter {
|
||||||
ErrorManager.generateError(e);
|
ErrorManager.generateError(e);
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
LispInterpreter.erasePrompt(interactive);
|
LispInterpreter.erasePrompt(interactive);
|
||||||
ErrorManager.generateError(e.getMessage(), 0);
|
ErrorManager.generateError(new LispException() {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getSeverity() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMessage() {
|
||||||
|
return e.getMessage();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (interactive) {
|
if (interactive) {
|
||||||
|
|
Loading…
Reference in New Issue