diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2016-07-26 19:43:13 +0300 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2016-07-26 19:47:14 +0300 |
| commit | 8234767203a7f673b12c8f51b83762c511274366 (patch) | |
| tree | 3dfa221a1bc0c94860363a74bdaf4c3dbb23226d /src/main/com/jantuomi/tunkki/exception | |
| parent | 9c189c2d1ef35184ec9a9d08bdeeb52936902867 (diff) | |
Rename project to tunkki
Diffstat (limited to 'src/main/com/jantuomi/tunkki/exception')
| -rw-r--r-- | src/main/com/jantuomi/tunkki/exception/BorkError.java | 61 | ||||
| -rw-r--r-- | src/main/com/jantuomi/tunkki/exception/ExceptionManager.java | 21 |
2 files changed, 82 insertions, 0 deletions
diff --git a/src/main/com/jantuomi/tunkki/exception/BorkError.java b/src/main/com/jantuomi/tunkki/exception/BorkError.java new file mode 100644 index 0000000..6c7c2a1 --- /dev/null +++ b/src/main/com/jantuomi/tunkki/exception/BorkError.java @@ -0,0 +1,61 @@ +package com.jantuomi.tunkki.exception; + + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by jan on 12.6.2016. + */ +public class BorkError extends Exception { + + public enum ExceptionType { + IllegalTokenError, + SyntaxError, + TypeError, + ArgumentError, + UndeclaredSymbolError, + IncludeError, + GeneralError + } + + public static Map<ExceptionType, String> errorTexts = new HashMap<>(); + + static { + errorTexts.put(ExceptionType.IllegalTokenError, "Unknown or illegal token %s found."); + errorTexts.put(ExceptionType.SyntaxError, "Unexpected %s."); + errorTexts.put(ExceptionType.TypeError, "Incompatible types %s and %s."); + errorTexts.put(ExceptionType.ArgumentError, "The parameter list given to function %s is either of wrong length or the parameters are of wrong type. Actual: %s"); + errorTexts.put(ExceptionType.UndeclaredSymbolError, "No symbol %s defined, parameters: [%s]."); + errorTexts.put(ExceptionType.IncludeError, "File %s could not be included."); + errorTexts.put(ExceptionType.GeneralError, "%s"); + } + + private ExceptionType exceptionType; + private String completeMessage; + + public BorkError(ExceptionType exceptionType, int line, String... args) { + super(exceptionType.toString(), null, false, false); + this.exceptionType = exceptionType; + this.completeMessage = formatMessage(exceptionType.toString() + ": " + what(exceptionType), line, args); + } + + private static String formatMessage(String message, int line, String... args) { + + message = String.format(message, args); + if (line == -1) { + return "\n" + message; + } else { + return String.format("\nline %d: ", line) + message; + } + } + + public static String what(ExceptionType type) { + return errorTexts.get(type); + } + + @Override + public void printStackTrace() { + System.err.println(completeMessage); + } +} diff --git a/src/main/com/jantuomi/tunkki/exception/ExceptionManager.java b/src/main/com/jantuomi/tunkki/exception/ExceptionManager.java new file mode 100644 index 0000000..7c7a6b5 --- /dev/null +++ b/src/main/com/jantuomi/tunkki/exception/ExceptionManager.java @@ -0,0 +1,21 @@ +package com.jantuomi.tunkki.exception; + +/** + * Created by jan on 10.6.2016. + */ +public class ExceptionManager { + + private static final ExceptionManager instance = new ExceptionManager(); + + private ExceptionManager() { + } + + public static ExceptionManager getInstance() { + return instance; + } + + public static void raise(BorkError.ExceptionType ex, int line, String... args) throws BorkError { + BorkError e = new BorkError(ex, line, args); + throw e; + } +} |
