@Override protected JsonTypeNotParsableException doBuild(final DittoHeaders dittoHeaders, final String message, final String description, final Throwable cause, final URI href) { return new JsonTypeNotParsableException(dittoHeaders, message, description, cause, href); } }
/** * Constructs a new {@code JsonTypeNotParsableException} object with the exception message extracted from the * given JSON object. * * @param jsonObject the JSON to read the {@link JsonFields#MESSAGE} field from. * @param dittoHeaders the headers of the command which resulted in this exception. * @return the new JsonTypeNotParsableException. * @throws org.eclipse.ditto.json.JsonMissingFieldException if the {@code jsonObject} does not have the {@link * JsonFields#MESSAGE} field. */ public static JsonTypeNotParsableException fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new Builder() .dittoHeaders(dittoHeaders) .message(readMessage(jsonObject)) .description(readDescription(jsonObject).orElse(DEFAULT_DESCRIPTION)) .href(readHRef(jsonObject).orElse(null)) .build(); }
private T fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { final String type = resolveType(jsonObject); final JsonParsable<T> jsonObjectParsable = parseStrategies.get(type); if (null != jsonObjectParsable) { try { return jsonObjectParsable.parse(jsonObject, dittoHeaders); } catch (final JsonRuntimeException jre) { final JsonExceptionBuilder builder = JsonRuntimeException.newBuilder(jre.getErrorCode()) .message("Error when parsing Json type '" + type + "': " + jre.getMessage()) .cause(jre.getCause()); jre.getDescription().ifPresent(builder::description); jre.getHref().ifPresent(builder::href); // rethrow after enhancing the exception with the type which failed to parse throw (JsonRuntimeException) builder.build(); } } else { throw JsonTypeNotParsableException.newBuilder(type, getClass().getSimpleName()) .dittoHeaders(dittoHeaders) .build(); } }
private void deserializePiggybackCommand(final ExecutePiggybackCommand command, final Consumer<Jsonifiable<?>> onSuccess, final Consumer<DittoRuntimeException> onError) { final JsonObject piggybackCommandJson = command.getPiggybackCommand(); final String piggybackCommandType = piggybackCommandJson.getValue(Command.JsonFields.TYPE).orElse(null); if (serviceMappingStrategy.containsKey(piggybackCommandType)) { try { final Jsonifiable jsonifiable = serviceMappingStrategy.get(piggybackCommandType) .apply(piggybackCommandJson, command.getDittoHeaders()); onSuccess.accept(jsonifiable); } catch (final DittoRuntimeException e) { log.warning("Got DittoRuntimeException while parsing piggybackCommand <{}>: {}", piggybackCommandType, e.getMessage()); onError.accept(e); } } else { final String message = String.format("ExecutePiggybackCommand with piggybackCommand <%s> cannot be executed " + "by this service as there is no mappingStrategy for it.", piggybackCommandType); log.warning(message); final JsonTypeNotParsableException typeNotMappableException = JsonTypeNotParsableException.fromMessage(message, command.getDittoHeaders()); onError.accept(typeNotMappableException); } }
/** * Constructs a new {@code JsonTypeNotParsableException} object with the exception message extracted from the * given JSON object. * * @param jsonObject the JSON to read the {@link JsonFields#MESSAGE} field from. * @param dittoHeaders the headers of the command which resulted in this exception. * @return the new JsonTypeNotParsableException. * @throws org.eclipse.ditto.json.JsonMissingFieldException if the {@code jsonObject} does not have the {@link * JsonFields#MESSAGE} field. */ public static JsonTypeNotParsableException fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new Builder() .dittoHeaders(dittoHeaders) .message(readMessage(jsonObject)) .description(readDescription(jsonObject).orElse(DEFAULT_DESCRIPTION)) .href(readHRef(jsonObject).orElse(null)) .build(); }
private T fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { final String type = resolveType(jsonObject); final JsonParsable<T> jsonObjectParsable = parseStrategies.get(type); if (null != jsonObjectParsable) { try { return jsonObjectParsable.parse(jsonObject, dittoHeaders); } catch (final JsonRuntimeException jre) { final JsonExceptionBuilder builder = JsonRuntimeException.newBuilder(jre.getErrorCode()) .message("Error when parsing Json type '" + type + "': " + jre.getMessage()) .cause(jre.getCause()); jre.getDescription().ifPresent(builder::description); jre.getHref().ifPresent(builder::href); // rethrow after enhancing the exception with the type which failed to parse throw (JsonRuntimeException) builder.build(); } } else { throw JsonTypeNotParsableException.newBuilder(type, getClass().getSimpleName()) .dittoHeaders(dittoHeaders) .build(); } }
private void handleExecutePiggyBack(final ExecutePiggybackCommand command) { LogUtil.enhanceLogWithCorrelationId(log, command); final JsonObject piggybackCommandJson = command.getPiggybackCommand(); final String piggybackCommandType = piggybackCommandJson.getValueOrThrow(Command.JsonFields.TYPE); if (serviceMappingStrategy.containsKey(piggybackCommandType)) { final Jsonifiable<?> piggybackCommand; try { piggybackCommand = serviceMappingStrategy.get(piggybackCommandType) .apply(piggybackCommandJson, command.getDittoHeaders()); } catch (final DittoRuntimeException e) { log.warning("Got DittoRuntimeException while parsing piggybackCommand <{}>: {}", piggybackCommandType, e.getMessage()); getSender().tell(e, getSelf()); return; } log.info("Received PiggybackCommand: <{}> - telling to: <{}>", piggybackCommand, command.getTargetActorSelection()); getContext().actorSelection(command.getTargetActorSelection()).forward(piggybackCommand, getContext()); } else { final String message = String.format("ExecutePiggybackCommand with piggybackCommand <%s> cannot be executed " + "by this service as there is no mappingStrategy for it.", piggybackCommandType); log.warning(message); final JsonTypeNotParsableException typeNotMappableException = JsonTypeNotParsableException.fromMessage(message, command.getDittoHeaders()); getSender().tell(typeNotMappableException, getSelf()); } }
@Override protected JsonTypeNotParsableException doBuild(final DittoHeaders dittoHeaders, final String message, final String description, final Throwable cause, final URI href) { return new JsonTypeNotParsableException(dittoHeaders, message, description, cause, href); } }