name:
src/main/java/io/protobase/f7/errors/F7Exception.java
-rw-r--r--
1840
1package io.protobase.f7.errors;
2
3/**
4 * Abstract F7 parsing or execution error.
5 */
6public abstract class F7Exception extends RuntimeException {
7 private F7ExceptionName name;
8 private String message = "";
9
10 public F7Exception(F7ExceptionName name, String message) {
11 this.name = name;
12 this.message = message;
13 }
14
15 public F7Exception(F7ExceptionName name) {
16 this.name = name;
17 }
18
19 /**
20 * Sometimes we want to construct an exception from the error name, which is the same as the enumeration name.
21 *
22 * @param errorName of the exception.
23 * @return new exception.
24 */
25 public static F7Exception fromString(String errorName) {
26 switch (errorName) {
27 case "#NULL!":
28 return new NullException("");
29 case "#DIV/0!":
30 return new DivException("");
31 case "#VALUE!":
32 return new ValueException("");
33 case "#REF!":
34 return new RefException("");
35 case "#NAME?":
36 return new NameException("");
37 case "#NUM!":
38 return new NumException("");
39 case "#N/A":
40 return new NAException("");
41 case "#ERROR!":
42 return new ParseException("");
43 default:
44 throw new ParseException("");
45 }
46 }
47
48 /**
49 * Get the name of this exception.
50 *
51 * @return name.
52 */
53 public F7ExceptionName getName() {
54 return name;
55 }
56
57 /**
58 * Get the message for this exception.
59 *
60 * @return message.
61 */
62 @Override
63 public String getMessage() {
64 return message;
65 }
66
67 /**
68 * Two F7Errors are considered equal if and only if their names are the same.
69 *
70 * @param other - to compare to.
71 * @return true if names are equal.
72 */
73 @Override
74 public boolean equals(Object other) {
75 return other instanceof F7Exception && ((F7Exception) other).getName().toString().equals(this.getName().toString());
76 }
77}