/
CommonErrorRegistry.java
executable file
·135 lines (108 loc) · 7.04 KB
/
CommonErrorRegistry.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright (c) 2017-2018 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.signals.commands.base;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.json.JsonFieldSelectorInvalidException;
import org.eclipse.ditto.json.JsonMissingFieldException;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonParseException;
import org.eclipse.ditto.json.JsonPointerInvalidException;
import org.eclipse.ditto.model.base.exceptions.DittoHeaderInvalidException;
import org.eclipse.ditto.model.base.exceptions.DittoJsonException;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException;
import org.eclipse.ditto.model.base.exceptions.InvalidRqlExpressionException;
import org.eclipse.ditto.signals.base.AbstractErrorRegistry;
import org.eclipse.ditto.signals.base.ErrorRegistry;
import org.eclipse.ditto.signals.base.JsonParsable;
import org.eclipse.ditto.signals.base.JsonTypeNotParsableException;
import org.eclipse.ditto.signals.commands.base.exceptions.GatewayAuthenticationFailedException;
import org.eclipse.ditto.signals.commands.base.exceptions.GatewayAuthenticationProviderUnavailableException;
import org.eclipse.ditto.signals.commands.base.exceptions.GatewayInternalErrorException;
import org.eclipse.ditto.signals.commands.base.exceptions.GatewayMethodNotAllowedException;
import org.eclipse.ditto.signals.commands.base.exceptions.GatewayPlaceholderNotResolvableException;
import org.eclipse.ditto.signals.commands.base.exceptions.GatewayPlaceholderReferenceNotSupportedException;
import org.eclipse.ditto.signals.commands.base.exceptions.GatewayPlaceholderReferenceUnknownFieldException;
import org.eclipse.ditto.signals.commands.base.exceptions.GatewayQueryTimeExceededException;
import org.eclipse.ditto.signals.commands.base.exceptions.GatewayServiceTimeoutException;
import org.eclipse.ditto.signals.commands.base.exceptions.GatewayServiceTooManyRequestsException;
import org.eclipse.ditto.signals.commands.base.exceptions.GatewayServiceUnavailableException;
/**
* A {@link ErrorRegistry} aware of common {@link DittoRuntimeException}s.
*/
@Immutable
public final class CommonErrorRegistry extends AbstractErrorRegistry<DittoRuntimeException> {
private CommonErrorRegistry(final Map<String, JsonParsable<DittoRuntimeException>> parseStrategies) {
super(parseStrategies);
}
/**
* Returns a new {@code CommonErrorRegistry}.
*
* @return the error registry.
*/
public static CommonErrorRegistry newInstance() {
final Map<String, JsonParsable<DittoRuntimeException>> parseStrategies = new HashMap<>();
parseStrategies.put(JsonParseException.ERROR_CODE, (jsonObject, dittoHeaders) -> new DittoJsonException(
JsonParseException.newBuilder().message(getMessage(jsonObject)).build(), dittoHeaders));
parseStrategies.put(JsonMissingFieldException.ERROR_CODE, (jsonObject, dittoHeaders) -> new DittoJsonException(
JsonMissingFieldException.newBuilder().message(getMessage(jsonObject)).build(), dittoHeaders));
parseStrategies.put(JsonFieldSelectorInvalidException.ERROR_CODE,
(jsonObject, dittoHeaders) -> new DittoJsonException(
JsonFieldSelectorInvalidException.newBuilder().message(getMessage(jsonObject)).build(),
dittoHeaders));
parseStrategies.put(JsonPointerInvalidException.ERROR_CODE,
(jsonObject, dittoHeaders) -> new DittoJsonException(
JsonPointerInvalidException.newBuilder().message(getMessage(jsonObject)).build(),
dittoHeaders));
// other common exceptions
parseStrategies.put(DittoHeaderInvalidException.ERROR_CODE, DittoHeaderInvalidException::fromJson);
parseStrategies.put(CommandNotSupportedException.ERROR_CODE, CommandNotSupportedException::fromJson);
parseStrategies.put(JsonTypeNotParsableException.ERROR_CODE, JsonTypeNotParsableException::fromJson);
parseStrategies.put(InvalidRqlExpressionException.ERROR_CODE, InvalidRqlExpressionException::fromJson);
// Gateway exceptions
parseStrategies.put(GatewayAuthenticationFailedException.ERROR_CODE,
GatewayAuthenticationFailedException::fromJson);
parseStrategies.put(GatewayAuthenticationProviderUnavailableException.ERROR_CODE,
(jsonObject, dittoHeaders) -> GatewayAuthenticationProviderUnavailableException
.fromMessage(getMessage(jsonObject), dittoHeaders));
parseStrategies.put(GatewayInternalErrorException.ERROR_CODE,
(jsonObject, dittoHeaders) -> GatewayInternalErrorException
.fromMessage(getMessage(jsonObject), dittoHeaders));
parseStrategies.put(GatewayMethodNotAllowedException.ERROR_CODE,
(jsonObject, dittoHeaders) -> GatewayMethodNotAllowedException
.fromMessage(getMessage(jsonObject), dittoHeaders));
parseStrategies.put(GatewayQueryTimeExceededException.ERROR_CODE,
(jsonObject, dittoHeaders) -> GatewayQueryTimeExceededException
.fromMessage(getMessage(jsonObject), dittoHeaders));
parseStrategies.put(GatewayServiceTimeoutException.ERROR_CODE,
(jsonObject, dittoHeaders) -> GatewayServiceTimeoutException
.fromMessage(getMessage(jsonObject), dittoHeaders));
parseStrategies.put(GatewayServiceUnavailableException.ERROR_CODE,
(jsonObject, dittoHeaders) -> GatewayServiceUnavailableException
.fromMessage(getMessage(jsonObject), dittoHeaders));
parseStrategies.put(GatewayServiceTooManyRequestsException.ERROR_CODE,
(jsonObject, dittoHeaders) -> GatewayServiceTooManyRequestsException
.fromMessage(getMessage(jsonObject), dittoHeaders));
parseStrategies.put(GatewayPlaceholderNotResolvableException.ERROR_CODE,
GatewayPlaceholderNotResolvableException::fromJson);
parseStrategies.put(GatewayPlaceholderReferenceNotSupportedException.ERROR_CODE,
GatewayPlaceholderReferenceNotSupportedException::fromJson);
parseStrategies.put(GatewayPlaceholderReferenceUnknownFieldException.ERROR_CODE,
GatewayPlaceholderReferenceUnknownFieldException::fromJson);
return new CommonErrorRegistry(parseStrategies);
}
private static String getMessage(final JsonObject jsonObject) {
return jsonObject.getValue(DittoJsonException.JsonFields.MESSAGE)
.orElseThrow(() -> JsonMissingFieldException.newBuilder()
.fieldName(DittoRuntimeException.JsonFields.MESSAGE.getPointer().toString()).build());
}
}