Skip to content

Commit

Permalink
Issue #106: Improved exception handling of JSON deserialization of Un…
Browse files Browse the repository at this point in the history
…supportedSignalException.

Signed-off-by: Juergen Fickel <juergen.fickel@bosch.io>
  • Loading branch information
Juergen Fickel committed Dec 1, 2021
1 parent af651e3 commit c799ae7
Showing 1 changed file with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;

import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeExceptionBuilder;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.JsonParsableException;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonParseException;

/**
* Thrown if a {@link Signal} is not supported.
Expand Down Expand Up @@ -96,14 +97,23 @@ public static UnsupportedSignalException fromMessage(@Nullable final String mess
*/
public static UnsupportedSignalException fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) {
checkNotNull(jsonObject, "jsonObject");
checkNotNull(dittoHeaders, "dittoHeaders");

final Builder builder = new Builder();
builder.dittoHeaders(dittoHeaders);
builder.message(jsonObject.getValueOrThrow(JsonFields.MESSAGE));
jsonObject.getValue(JsonFields.STATUS).flatMap(HttpStatus::tryGetInstance).ifPresent(builder::httpStatus);
jsonObject.getValue(JsonFields.DESCRIPTION).ifPresent(builder::description);
jsonObject.getValue(JsonFields.HREF).map(URI::create).ifPresent(builder::href);
builder.dittoHeaders(checkNotNull(dittoHeaders, "dittoHeaders"));

try {
builder.message(jsonObject.getValueOrThrow(JsonFields.MESSAGE));
jsonObject.getValue(JsonFields.STATUS).flatMap(HttpStatus::tryGetInstance).ifPresent(builder::httpStatus);
jsonObject.getValue(JsonFields.DESCRIPTION).ifPresent(builder::description);
jsonObject.getValue(JsonFields.HREF).map(URI::create).ifPresent(builder::href);
} catch (final Exception e) {
throw JsonParseException.newBuilder()
.message(MessageFormat.format("Failed to deserialize JSON object to a {0}: {1}",
UnsupportedSignalException.class.getSimpleName(),
e.getMessage()))
.cause(e)
.build();
}

return builder.build();
}
Expand Down

0 comments on commit c799ae7

Please sign in to comment.