Skip to content

Commit 74f9c4c

Browse files
dguggemosthjaeckle
authored andcommitted
handle unparseable exceptions when building a ThingErrorResponse from JSON
Signed-off-by: Dominik Guggemos <[email protected]>
1 parent dd3970e commit 74f9c4c

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

signals/commands/things/src/main/java/org/eclipse/ditto/signals/commands/things/ThingErrorResponse.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
import org.eclipse.ditto.json.JsonObject;
2626
import org.eclipse.ditto.json.JsonObjectBuilder;
2727
import org.eclipse.ditto.json.JsonPointer;
28+
import org.eclipse.ditto.model.base.common.HttpStatusCode;
2829
import org.eclipse.ditto.model.base.exceptions.DittoJsonException;
2930
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException;
3031
import org.eclipse.ditto.model.base.headers.DittoHeaders;
3132
import org.eclipse.ditto.model.base.json.JsonSchemaVersion;
3233
import org.eclipse.ditto.signals.commands.base.AbstractCommandResponse;
34+
import org.eclipse.ditto.signals.commands.base.CommandResponse;
3335
import org.eclipse.ditto.signals.commands.base.ErrorResponse;
3436
import org.eclipse.ditto.signals.commands.things.exceptions.ThingErrorRegistry;
3537

@@ -168,7 +170,23 @@ public static ThingErrorResponse fromJson(final ThingErrorRegistry thingErrorReg
168170
.build());
169171
final JsonObject payload = jsonObject.getValueOrThrow(ThingCommandResponse.JsonFields.PAYLOAD).asObject();
170172

171-
final DittoRuntimeException exception = thingErrorRegistry.parse(payload, dittoHeaders);
173+
DittoRuntimeException exception;
174+
try {
175+
exception = thingErrorRegistry.parse(payload, dittoHeaders);
176+
} catch (final Exception e) {
177+
final int status = jsonObject.getValue(CommandResponse.JsonFields.STATUS).orElse(500);
178+
final String errorCode =
179+
payload.getValue(DittoRuntimeException.JsonFields.ERROR_CODE).orElse("unknown:unknown");
180+
final String errorMessage =
181+
payload.getValue(DittoRuntimeException.JsonFields.MESSAGE).orElse("An unknown error occurred");
182+
final String errorDescription = payload.getValue(DittoRuntimeException.JsonFields.DESCRIPTION).orElse("");
183+
exception =
184+
DittoRuntimeException.newBuilder(errorCode,
185+
HttpStatusCode.forInt(status).orElse(HttpStatusCode.INTERNAL_SERVER_ERROR))
186+
.message(errorMessage)
187+
.description(errorDescription)
188+
.build();
189+
}
172190

173191
return of(thingId, exception, dittoHeaders);
174192
}

0 commit comments

Comments
 (0)