title: CXF/ JAX-RS : Return Custom response from interceptor
body: <p>We need to return custom error code and error message when exception occurs during REST invocation. We have created a exception mapper provider, it works well for the exceptions from the application code. However, it doesn't work when exception occurs from the CXF code (e.g. form the CustomValidationInterceptor that I wrote).</p>
  
  <p>For example, if I request with invalid path parameter (e.g invalid phoneNumber). In this case, we need to return a custom error code and error message in JSON format, but it doesn't work even though we have a exception mapper provider created to handle WebApplicationException. </p>
  
  <p>Is there any way to handle exceptions from cxf interceptors and return
   response to user with something like the following? </p>
  
  <pre><code>{
  "errorDetail": {
  "errorCode": "404",
  "errorMessage": "Bad Request"
  }
  }
  </code></pre>
  
  <p>Code Snippet of my CustomValidationInterceptor :</p>
  
  <pre><code>public class CustomValidationInterceptor extends AbstractPhaseInterceptor&lt;Message&gt;{
  
      public CustomValidationInterceptor() {
          super(Phase.PRE_INVOKE); // Put this interceptor in this phase
      }
  
      public void handleMessage(Message message) {
  
          MetadataMap&lt;String, String&gt; metadataMap = (MetadataMap&lt;String, String&gt;) message.get("jaxrs.template.parameters");
  
          if(null != metadataMap) {
              List&lt;String&gt; list = metadataMap.get("phoneNumber");
              if(null != list) {
                  String phoneNumber = list.get(0);
                  boolean result = validatePhoneNumber(phoneNumber);
                  if(!result){
                      throw new TelusServiceException(Response.status(Response.Status.BAD_REQUEST).build(), 400, "phone number not valid");
                  }
              } else {
                  throw new TelusServiceException(Response.status(Response.Status.BAD_REQUEST).build(), 400, "phone number not valid");
              }
          } else {
              throw new TelusServiceException(Response.status(Response.Status.BAD_REQUEST).build(), 400, "phone number not valid");
          }
      }
  
      public boolean validatePhoneNumber(String phoneNumber) {
  
            Pattern pattern = Pattern.compile("^[1-9]\\d{9}$");
            Matcher matcher = pattern.matcher(phoneNumber);
  
            if (!matcher.matches()) {
                return false;
            }
            return true;
       }
  
  }
  </code></pre>
  
  <p>Code Snippet of my Custom Exception Mapper Provider</p>
  
  <pre><code>public class TelusExceptionHandler implements ExceptionMapper&lt;TelusServiceException&gt; {
  
      public Response toResponse(TelusServiceException exception) {
          return Response.status(exception.getErrorDetail().getErrorCode()).entity(exception.getErrorDetail()).build();
      }
  
  }
  </code></pre>
  
  <p>Code Snippet of TelusServiceException</p>
  
  <pre><code>public class TelusServiceException extends WebApplicationException{
  
  // constructors and other methods 
  
      private ErrorDetail errorDetail = null;
  
          public ErrorDetail getErrorDetail() {
          return errorDetail;
      }
  
      public void setErrorDetail(ErrorDetail errorDetail) {
          this.errorDetail = errorDetail;
      }
  
        public TelusServiceException(Response response, int errorCode, String errorMessage) {
          super(response);
  
          errorDetail = new ErrorDetail();
          errorDetail.setErrorCode(errorCode);
          errorDetail.setErrorMessage(errorMessage);
      }
  
  }
  </code></pre>
  
  <p>Code Snippet of ErrorDetail class</p>
  
  <pre><code>@XmlRootElement(name="errorDetail")
  public class ErrorDetail {
  
      private int errorCode;
      private String errorMessage;
  
      @XmlElement(name = "errorCode")
      public int getErrorCode() {
          return errorCode;
      }
  
      public void setErrorCode(int errorCode) {
          this.errorCode = errorCode;
      }
      @XmlElement(name = "errorMessage")
      public String getErrorMessage() {
          return errorMessage;
      }
  
      public void setErrorMessage(String errorMessage) {
          this.errorMessage = errorMessage;
      }
  
  }
  </code></pre>