|
18 | 18 | */
|
19 | 19 | package io.streamthoughts.kafka.connect.filepulse.filter;
|
20 | 20 |
|
| 21 | +import java.io.PrintWriter; |
| 22 | +import java.io.StringWriter; |
21 | 23 | import java.util.Objects;
|
22 | 24 |
|
23 | 25 | public class FilterError {
|
24 | 26 |
|
25 |
| - private final String message; |
| 27 | + private final String exceptionMessage; |
| 28 | + |
| 29 | + private final String exceptionClassName; |
| 30 | + |
| 31 | + private final String exceptionStacktrace; |
26 | 32 |
|
27 | 33 | private final String filter;
|
28 | 34 |
|
29 | 35 | /**
|
30 |
| - * Creates a new {@link FilterError} instance. |
31 |
| - * |
32 |
| - * @param error the error message message. |
33 |
| - * @param filter the filter name that failed. |
| 36 | + * Helper method to create a new {@link FilterError} object from a given {@link Throwable}. |
34 | 37 | */
|
35 |
| - FilterError(final String error, final String filter) { |
36 |
| - Objects.requireNonNull(error, "The message message"); |
37 |
| - Objects.requireNonNull(filter, "The filter message"); |
38 |
| - this.message = error; |
39 |
| - this.filter = filter; |
| 38 | + public static FilterError of(final Throwable throwable, final String filter) { |
| 39 | + final Throwable cause; |
| 40 | + if (throwable instanceof FilterException && throwable.getCause() != null) { |
| 41 | + cause = throwable.getCause(); |
| 42 | + } else { |
| 43 | + cause = throwable; |
| 44 | + } |
| 45 | + return new FilterError(cause.getMessage(), cause.getClass().getName(), getStacktrace(cause), filter); |
40 | 46 | }
|
41 | 47 |
|
42 |
| - public String message() { |
43 |
| - return message; |
| 48 | + /** |
| 49 | + * Creates a new {@link FilterError} instance. |
| 50 | + * |
| 51 | + * @param message the exception message. |
| 52 | + * @param classname the exception className. |
| 53 | + * @param stacktrace the exception stacktrace. |
| 54 | + * @param filter the failed filter name. |
| 55 | + */ |
| 56 | + public FilterError(final String message, |
| 57 | + final String classname, |
| 58 | + final String stacktrace, |
| 59 | + final String filter) { |
| 60 | + this.exceptionMessage = Objects.requireNonNull(message, "'message' should not be null"); |
| 61 | + this.exceptionClassName = Objects.requireNonNull(classname, "'classname' should not be null"); |
| 62 | + this.exceptionStacktrace = Objects.requireNonNull(stacktrace, "'stacktrace' should not be null"); |
| 63 | + this.filter = Objects.requireNonNull(filter, "'filter' should not be null"); |
44 | 64 | }
|
45 | 65 |
|
46 | 66 | public String filter() {
|
47 | 67 | return filter;
|
48 | 68 | }
|
49 | 69 |
|
| 70 | + public String exceptionMessage() { |
| 71 | + return exceptionMessage; |
| 72 | + } |
| 73 | + |
| 74 | + public String exceptionStacktrace() { |
| 75 | + return exceptionStacktrace; |
| 76 | + } |
| 77 | + |
| 78 | + public String getExceptionClassName() { |
| 79 | + return exceptionClassName; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * {@inheritDoc} |
| 84 | + */ |
| 85 | + @Override |
| 86 | + public boolean equals(Object o) { |
| 87 | + if (this == o) return true; |
| 88 | + if (o == null || getClass() != o.getClass()) return false; |
| 89 | + FilterError that = (FilterError) o; |
| 90 | + return Objects.equals(exceptionMessage, that.exceptionMessage) && |
| 91 | + Objects.equals(exceptionClassName, that.exceptionClassName) && |
| 92 | + Objects.equals(exceptionStacktrace, that.exceptionStacktrace) && |
| 93 | + Objects.equals(filter, that.filter); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * {@inheritDoc} |
| 98 | + */ |
50 | 99 | @Override
|
51 |
| - public String toString() { |
52 |
| - return "[" + |
53 |
| - "message=" + message + |
54 |
| - ", filter=" + filter + |
55 |
| - ']'; |
| 100 | + public int hashCode() { |
| 101 | + return Objects.hash(exceptionMessage, exceptionClassName, exceptionStacktrace, filter); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @return the stacktrace representation for the given {@link Throwable}. |
| 106 | + */ |
| 107 | + private static String getStacktrace(final Throwable throwable) { |
| 108 | + final StringWriter sw = new StringWriter(); |
| 109 | + final PrintWriter pw = new PrintWriter(sw, true); |
| 110 | + throwable.printStackTrace(pw); |
| 111 | + return sw.getBuffer().toString(); |
56 | 112 | }
|
57 | 113 | }
|
0 commit comments