Skip to content

Commit

Permalink
handle unparseable exceptions when building a ThingErrorResponse from…
Browse files Browse the repository at this point in the history
… JSON

Signed-off-by: Dominik Guggemos <dominik.guggemos@bosch-si.com>
  • Loading branch information
dguggemos authored and thjaeckle committed Jan 11, 2018
1 parent dd3970e commit 74f9c4c
Showing 1 changed file with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.model.base.common.HttpStatusCode;
import org.eclipse.ditto.model.base.exceptions.DittoJsonException;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.json.JsonSchemaVersion;
import org.eclipse.ditto.signals.commands.base.AbstractCommandResponse;
import org.eclipse.ditto.signals.commands.base.CommandResponse;
import org.eclipse.ditto.signals.commands.base.ErrorResponse;
import org.eclipse.ditto.signals.commands.things.exceptions.ThingErrorRegistry;

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

final DittoRuntimeException exception = thingErrorRegistry.parse(payload, dittoHeaders);
DittoRuntimeException exception;
try {
exception = thingErrorRegistry.parse(payload, dittoHeaders);
} catch (final Exception e) {
final int status = jsonObject.getValue(CommandResponse.JsonFields.STATUS).orElse(500);
final String errorCode =
payload.getValue(DittoRuntimeException.JsonFields.ERROR_CODE).orElse("unknown:unknown");
final String errorMessage =
payload.getValue(DittoRuntimeException.JsonFields.MESSAGE).orElse("An unknown error occurred");
final String errorDescription = payload.getValue(DittoRuntimeException.JsonFields.DESCRIPTION).orElse("");
exception =
DittoRuntimeException.newBuilder(errorCode,
HttpStatusCode.forInt(status).orElse(HttpStatusCode.INTERNAL_SERVER_ERROR))
.message(errorMessage)
.description(errorDescription)
.build();
}

return of(thingId, exception, dittoHeaders);
}
Expand Down

0 comments on commit 74f9c4c

Please sign in to comment.