-
Notifications
You must be signed in to change notification settings - Fork 214
/
RootRouteExceptionHandler.java
121 lines (102 loc) · 5.75 KB
/
RootRouteExceptionHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.gateway.service.endpoints.routes;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.text.MessageFormat;
import java.util.Optional;
import java.util.concurrent.CompletionException;
import java.util.function.Function;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.json.JsonRuntimeException;
import org.eclipse.ditto.base.model.exceptions.DittoJsonException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.internal.utils.pekko.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.pekko.logging.ThreadSafeDittoLogger;
import org.apache.pekko.http.javadsl.model.HttpResponse;
import org.apache.pekko.http.javadsl.model.StatusCodes;
import org.apache.pekko.http.javadsl.server.Directives;
import org.apache.pekko.http.javadsl.server.ExceptionHandler;
import org.apache.pekko.http.javadsl.server.Route;
import org.apache.pekko.japi.pf.FI;
/**
* This class provides an {@link ExceptionHandler} for the root route.
*/
@Immutable
public final class RootRouteExceptionHandler {
private static final ThreadSafeDittoLogger LOGGER = DittoLoggerFactory
.getThreadSafeLogger(RootRouteExceptionHandler.class);
private final Function<DittoRuntimeException, HttpResponse> dreToHttpResponse;
private RootRouteExceptionHandler(final Function<DittoRuntimeException, HttpResponse> dreToHttpResponse) {
this.dreToHttpResponse = checkNotNull(dreToHttpResponse, "dittoRuntimeExceptionToHttpResponse");
}
/**
* Returns an instance of an {@link ExceptionHandler} for the root route.
*
* @param dittoRuntimeExceptionToHttpResponse this function is used to convert a DittoRuntimeException to a
* HttpResponse.
* @return the instance.
* @throws NullPointerException if {@code dittoRuntimeExceptionToHttpResponse} is {@code null}.
*/
public static ExceptionHandler getInstance(
final Function<DittoRuntimeException, HttpResponse> dittoRuntimeExceptionToHttpResponse) {
final RootRouteExceptionHandler rootRouteExceptionHandler =
new RootRouteExceptionHandler(dittoRuntimeExceptionToHttpResponse);
final FI.TypedPredicate<CompletionException> isCausedByDittoRuntimeException = exception -> {
@Nullable final Throwable cause = exception.getCause();
return cause instanceof DittoRuntimeException;
};
final FI.TypedPredicate<CompletionException> isCausedByJsonRuntimeException = exception -> {
@Nullable final Throwable cause = exception.getCause();
return cause instanceof JsonRuntimeException;
};
return ExceptionHandler.newBuilder()
.match(DittoRuntimeException.class, rootRouteExceptionHandler::handleDittoRuntimeException)
.match(JsonRuntimeException.class, jsonRuntimeException -> {
final DittoRuntimeException dittoJsonException = new DittoJsonException(jsonRuntimeException);
return rootRouteExceptionHandler.handleDittoRuntimeException(dittoJsonException);
})
.match(CompletionException.class, isCausedByDittoRuntimeException, ce -> {
final DittoRuntimeException dittoRuntimeException = (DittoRuntimeException) ce.getCause();
return rootRouteExceptionHandler.handleDittoRuntimeException(dittoRuntimeException);
})
.match(CompletionException.class, isCausedByJsonRuntimeException, ce -> {
final JsonRuntimeException jsonRuntimeException = (JsonRuntimeException) ce.getCause();
final DittoRuntimeException dittoRuntimeException = new DittoJsonException(jsonRuntimeException);
return rootRouteExceptionHandler.handleDittoRuntimeException(dittoRuntimeException);
})
.matchAny(RootRouteExceptionHandler::handleInternalServerError)
.build();
}
private Route handleDittoRuntimeException(final DittoRuntimeException dittoRuntimeException) {
logException(dittoRuntimeException);
return Directives.complete(dreToHttpResponse.apply(dittoRuntimeException));
}
private static void logException(final DittoRuntimeException exception) {
final DittoHeaders dittoHeaders = exception.getDittoHeaders();
final Optional<String> correlationIdOptional = dittoHeaders.getCorrelationId();
final String simpleExceptionName = exception.getClass().getSimpleName();
if (correlationIdOptional.isEmpty()) {
LOGGER.warn("Correlation ID was missing in headers of <{}>!", simpleExceptionName);
}
// We do not want to print stack trace of exception thus exception must not be last argument of logger call.
final String message = MessageFormat.format("<{0}> occurred in gateway root route: <{1}>!", simpleExceptionName,
exception.toString());
LOGGER.withCorrelationId(dittoHeaders).info(message);
}
private static Route handleInternalServerError(final Throwable cause) {
LOGGER.error("Unexpected exception in gateway root route: <{}>!", cause.getMessage(), cause);
return Directives.complete(StatusCodes.INTERNAL_SERVER_ERROR);
}
}