/*
 * Copyright 2015 fdefalco.
 *
 * 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 org.ohdsi.webapi.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import org.springframework.messaging.support.ErrorMessage;

/**
 *
 * @author fdefalco
 */

@Provider
public class GenericExceptionMapper implements ExceptionMapper<Throwable> {

    @Override
    public Response toResponse(Throwable ex) {
        ErrorMessage errorMessage = new ErrorMessage(ex);
        StringWriter errorStackTrace = new StringWriter();
        ex.printStackTrace(new PrintWriter(errorStackTrace));
        Status responseStatus = ex instanceof NotFoundException? Status.NOT_FOUND : Status.INTERNAL_SERVER_ERROR;

        return Response.status(responseStatus)
                .entity(errorMessage)
                .type(MediaType.APPLICATION_JSON)
                .build();
    }
}
@@ -630,58 +630,125 @@
 ort 
-java.io.PrintWriter;%0Aimport java.io.StringWriter
+org.springframework.dao.DataIntegrityViolationException;%0Aimport org.springframework.messaging.support.ErrorMessage
 ;%0A
+%0A
 impo
@@ -975,58 +975,56 @@
 ort 
-org.springframework.messaging.support.ErrorMessage
+java.io.PrintWriter;%0Aimport java.io.StringWriter
 ;%0A%0A/
@@ -1414,62 +1414,262 @@
 atus
- = ex instanceof NotFoundException? Status.NOT_FOUND :
+;%0A        if(ex instanceof DataIntegrityViolationException) %7B%0A            responseStatus = Status.CONFLICT;%0A        %7D else if (ex instanceof NotFoundException) %7B%0A            responseStatus = Status.NOT_FOUND;%0A        %7D else %7B%0A            responseStatus =
  Sta
@@ -1694,16 +1694,26 @@
 R_ERROR;
+%0A        %7D
 %0A%0A      

Return HTTP status 409 ("CONFLICT") for DataIntegrityViolationException (#853)