Skip to content

Commit

Permalink
fix: HTTP status code is now set correctly for validation errors with…
Browse files Browse the repository at this point in the history
… JSON-RPC (400). Related to this, there is now a central place to handle exceptions with JSON-RPC and to configure the correct HTTP status code.

PR #827
  • Loading branch information
jekutzsche authored Jun 27, 2022
1 parent 3d6a77e commit e0b98f7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package iris.client_bff.config;

import static com.googlecode.jsonrpc4j.ErrorResolver.JsonError.*;

import iris.client_bff.cases.eps.CaseDataController;
import iris.client_bff.core.alert.AlertService;
import iris.client_bff.events.eps.EventDataController;
Expand All @@ -11,6 +13,11 @@

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;

import javax.validation.ConstraintViolationException;

import org.springframework.context.NoSuchMessageException;
import org.springframework.context.annotation.Bean;
Expand All @@ -24,6 +31,7 @@
import com.googlecode.jsonrpc4j.AnnotationsErrorResolver;
import com.googlecode.jsonrpc4j.DefaultErrorResolver;
import com.googlecode.jsonrpc4j.ErrorData;
import com.googlecode.jsonrpc4j.ErrorResolver;
import com.googlecode.jsonrpc4j.JsonRpcInterceptor;
import com.googlecode.jsonrpc4j.MultipleErrorResolver;
import com.googlecode.jsonrpc4j.ProxyUtil;
Expand All @@ -35,9 +43,12 @@
public class DataSubmissionConfig {

public static final String DATA_SUBMISSION_ENDPOINT = "/data-submission-rpc";

public static final String DATA_SUBMISSION_ENDPOINT_WITH_SLASH = "/data-submission-rpc/";

private final Map<Class<? extends Throwable>, Integer> exception2CodeMap = Map.of(
ConstraintViolationException.class, INVALID_REQUEST.code,
IllegalArgumentException.class, INVALID_REQUEST.code);

private final MessageSourceAccessor messages;
private final UserService userService;
private final AlertService alertService;
Expand Down Expand Up @@ -94,7 +105,7 @@ private JsonServiceExporter createCompositeJsonServiceExporter() {
private final class MessageResolvingErrorResolver extends MultipleErrorResolver {

private MessageResolvingErrorResolver() {
super(AnnotationsErrorResolver.INSTANCE, DefaultErrorResolver.INSTANCE);
super(AnnotationsErrorResolver.INSTANCE, new IrisErrorResolver(), DefaultErrorResolver.INSTANCE);
}

@Override
Expand All @@ -119,6 +130,25 @@ public JsonError resolveError(Throwable t, Method method, List<JsonNode> argumen
}
}

private class IrisErrorResolver implements ErrorResolver {

@Override
public JsonError resolveError(Throwable t, Method method, List<JsonNode> arguments) {

return determineCode(t)
.map(code -> new JsonError(code, t.getMessage(), new ErrorData(t.getClass().getName(), t.getMessage())))
.orElse(null);
}

private Optional<Integer> determineCode(Throwable t) {

return exception2CodeMap.entrySet().stream()
.filter(it -> it.getKey().isAssignableFrom(t.getClass()))
.map(Entry::getValue)
.findAny();
}
}

/**
* @author Jens Kutzsche
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public interface EventDataController {

@JsonRpcErrors({
@JsonRpcError(exception = ResponseStatusException.class, code = -32600)
@JsonRpcError(exception = ResponseStatusException.class, code = -32602)
})
String submitGuestList(
@JsonRpcParam(value = "_client") @Valid JsonRpcClientDto client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import iris.client_bff.core.validation.Base64;
import iris.client_bff.core.validation.NoSignOfAttack;
import iris.client_bff.core.validation.PhoneNumber;
import iris.client_bff.vaccination_info.VaccinationInfoAnnouncementException;
import iris.client_bff.vaccination_info.VaccinationStatus;
import iris.client_bff.vaccination_info.VaccinationType;

Expand All @@ -14,7 +15,6 @@
import java.util.UUID;

import javax.annotation.Nullable;
import javax.validation.ConstraintViolationException;
import javax.validation.Valid;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
Expand All @@ -35,16 +35,12 @@
public interface VaccinationInfoController {

@JsonRpcErrors({
@JsonRpcError(exception = ConstraintViolationException.class, code = -32600),
@JsonRpcError(exception = InvalidPublicKeyException.class, code = -32600)
@JsonRpcError(exception = InvalidPublicKeyException.class, code = -32600),
@JsonRpcError(exception = VaccinationInfoAnnouncementException.class, code = -32603)
})
AnnouncementResultDto announceVaccinationInfoList(
@JsonRpcParam(value = "announcementData") @Valid AnnouncementDataDto announcementData);

@JsonRpcErrors({
@JsonRpcError(exception = ConstraintViolationException.class, code = -32600),
@JsonRpcError(exception = IllegalArgumentException.class, code = -32600)
})
String submitVaccinationInfoList(
@JsonRpcParam(value = "dataAuthorizationToken") @NotNull UUID dataAuthorizationToken,
@JsonRpcParam(value = "facility") @Valid Facility facility,
Expand Down

0 comments on commit e0b98f7

Please sign in to comment.