From 8396c9c5570e1198d1717428eebd20f98c56d5ce Mon Sep 17 00:00:00 2001 From: Ewen Cheslack-Postava Date: Tue, 3 Feb 2015 11:14:29 -0800 Subject: [PATCH] Add RestConstraintViolationException for better error codes for some constraint violations. Some applications will check constraints that are not as generic as parsing or simple non-null requirements. In this case, they should be able to provide a more specific error code than the generic 422 that is generated by default. --- .../ConstraintViolationExceptionMapper.java | 14 +++-- .../RestConstraintViolationException.java | 52 +++++++++++++++++++ 2 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 core/src/main/java/io/confluent/rest/exceptions/RestConstraintViolationException.java diff --git a/core/src/main/java/io/confluent/rest/exceptions/ConstraintViolationExceptionMapper.java b/core/src/main/java/io/confluent/rest/exceptions/ConstraintViolationExceptionMapper.java index d4e5119b8e..a359516e8a 100644 --- a/core/src/main/java/io/confluent/rest/exceptions/ConstraintViolationExceptionMapper.java +++ b/core/src/main/java/io/confluent/rest/exceptions/ConstraintViolationExceptionMapper.java @@ -47,10 +47,16 @@ public String getReasonPhrase() { @Override public Response toResponse(ConstraintViolationException exception) { - final ErrorMessage message = new ErrorMessage( - UNPROCESSABLE_ENTITY_CODE, - ConstraintViolations.formatUntyped(exception.getConstraintViolations()) - ); + final ErrorMessage message; + if (exception instanceof RestConstraintViolationException) { + RestConstraintViolationException restException = (RestConstraintViolationException)exception; + message = new ErrorMessage(restException.getErrorCode(), restException.getMessage()); + } else { + message = new ErrorMessage( + UNPROCESSABLE_ENTITY_CODE, + ConstraintViolations.formatUntyped(exception.getConstraintViolations()) + ); + } return Response.status(UNPROCESSABLE_ENTITY_CODE) .entity(message) diff --git a/core/src/main/java/io/confluent/rest/exceptions/RestConstraintViolationException.java b/core/src/main/java/io/confluent/rest/exceptions/RestConstraintViolationException.java new file mode 100644 index 0000000000..fc0cfaff82 --- /dev/null +++ b/core/src/main/java/io/confluent/rest/exceptions/RestConstraintViolationException.java @@ -0,0 +1,52 @@ +/** + * Copyright 2015 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + +package io.confluent.rest.exceptions; + +import java.util.Set; + +import javax.validation.ConstraintViolation; +import javax.validation.ConstraintViolationException; +import javax.ws.rs.core.Response; + +/** + * ConstraintViolationException that includes RestException-like data to create a standard error + * response. Note that this does not inherit from RestException because it must inherit from + * ConstraintViolationException to be caught by the correct ExceptionMapper. + */ +public class RestConstraintViolationException extends ConstraintViolationException { + + private int errorCode; + + public RestConstraintViolationException(String message, int errorCode) { + this(message, errorCode, null); + } + + public RestConstraintViolationException(String message, int errorCode, + Set> + constraintViolations) { + super(message, constraintViolations); + this.errorCode = errorCode; + } + + public int getStatus() { + return ConstraintViolationExceptionMapper.UNPROCESSABLE_ENTITY_CODE; + } + + public int getErrorCode() { + return errorCode; + } +}