|
5 | 5 | package io.airbyte.commons.server.errors;
|
6 | 6 |
|
7 | 7 | import io.airbyte.api.model.generated.KnownExceptionInfo;
|
| 8 | +import java.util.Map; |
8 | 9 | import org.apache.logging.log4j.core.util.Throwables;
|
9 | 10 |
|
10 | 11 | /**
|
11 | 12 | * Exception wrapper to handle formatting API exception outputs nicely.
|
12 | 13 | */
|
13 | 14 | public abstract class KnownException extends RuntimeException {
|
14 | 15 |
|
| 16 | + private final Map<String, Object> details; // Add an optional details field |
| 17 | + |
15 | 18 | public KnownException(final String message) {
|
16 | 19 | super(message);
|
| 20 | + this.details = null; |
| 21 | + } |
| 22 | + |
| 23 | + public KnownException(final String message, final Map<String, Object> details) { |
| 24 | + super(message); |
| 25 | + this.details = details; |
17 | 26 | }
|
18 | 27 |
|
19 | 28 | public KnownException(final String message, final Throwable cause) {
|
20 | 29 | super(message, cause);
|
| 30 | + this.details = null; |
| 31 | + } |
| 32 | + |
| 33 | + public KnownException(final String message, final Throwable cause, final Map<String, Object> details) { |
| 34 | + super(message, cause); |
| 35 | + this.details = details; |
21 | 36 | }
|
22 | 37 |
|
23 | 38 | public abstract int getHttpCode();
|
24 | 39 |
|
| 40 | + public Map<String, Object> getDetails() { |
| 41 | + return details; |
| 42 | + } |
| 43 | + |
25 | 44 | public KnownExceptionInfo getKnownExceptionInfo() {
|
26 |
| - return KnownException.infoFromThrowable(this); |
| 45 | + return KnownException.infoFromThrowable(this, details); |
| 46 | + } |
| 47 | + |
| 48 | + public static KnownExceptionInfo infoFromThrowableWithMessage(final Throwable t, final String message) { |
| 49 | + return infoFromThrowableWithMessage(t, message, null); // Call the other static method with null details |
27 | 50 | }
|
28 | 51 |
|
29 | 52 | /**
|
30 | 53 | * Static factory for creating a known exception.
|
31 | 54 | *
|
32 | 55 | * @param t throwable to wrap
|
33 | 56 | * @param message error message
|
| 57 | + * @param details additional details |
34 | 58 | * @return known exception
|
35 | 59 | */
|
36 |
| - public static KnownExceptionInfo infoFromThrowableWithMessage(final Throwable t, final String message) { |
| 60 | + public static KnownExceptionInfo infoFromThrowableWithMessage(final Throwable t, final String message, final Map<String, Object> details) { |
37 | 61 | final KnownExceptionInfo exceptionInfo = new KnownExceptionInfo()
|
38 | 62 | .exceptionClassName(t.getClass().getName())
|
39 | 63 | .message(message)
|
40 | 64 | .exceptionStack(Throwables.toStringList(t));
|
| 65 | + |
41 | 66 | if (t.getCause() != null) {
|
42 |
| - exceptionInfo.rootCauseExceptionClassName(t.getClass().getClass().getName()); |
| 67 | + exceptionInfo.rootCauseExceptionClassName(t.getCause().getClass().getName()); |
43 | 68 | exceptionInfo.rootCauseExceptionStack(Throwables.toStringList(t.getCause()));
|
44 | 69 | }
|
| 70 | + |
| 71 | + if (details != null) { |
| 72 | + exceptionInfo.details(details); |
| 73 | + } |
| 74 | + |
45 | 75 | return exceptionInfo;
|
46 | 76 | }
|
47 | 77 |
|
48 |
| - public static KnownExceptionInfo infoFromThrowable(final Throwable t) { |
49 |
| - return infoFromThrowableWithMessage(t, t.getMessage()); |
| 78 | + public static KnownExceptionInfo infoFromThrowable(final Throwable t, final Map<String, Object> details) { |
| 79 | + return infoFromThrowableWithMessage(t, t.getMessage(), details); |
50 | 80 | }
|
51 | 81 |
|
52 | 82 | }
|
0 commit comments