From 1f1794b4f8d661881f5eb4be30d2812a8a43a824 Mon Sep 17 00:00:00 2001
From: Michael Hoennig <michael@hoennig.de>
Date: Tue, 2 Apr 2019 18:57:25 +0200
Subject: [PATCH] constraint violation error handler

---
 .../web/rest/errors/ErrorConstants.java       |  3 ++
 .../web/rest/errors/ExceptionTranslator.java  | 30 +++++++++++++++----
 src/main/webapp/i18n/de/global.json           |  3 +-
 src/main/webapp/i18n/en/global.json           |  3 +-
 4 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/hostsharing/hsadminng/web/rest/errors/ErrorConstants.java b/src/main/java/org/hostsharing/hsadminng/web/rest/errors/ErrorConstants.java
index abf93b23..2a9f016f 100644
--- a/src/main/java/org/hostsharing/hsadminng/web/rest/errors/ErrorConstants.java
+++ b/src/main/java/org/hostsharing/hsadminng/web/rest/errors/ErrorConstants.java
@@ -6,7 +6,10 @@ public final class ErrorConstants {
 
     public static final String ERR_CONCURRENCY_FAILURE = "error.concurrencyFailure";
     public static final String ERR_VALIDATION = "error.validation";
+    public static final String ERR_VALIDATION_DUPLICATE = "entity.validation.duplicate";
+
     public static final String PROBLEM_BASE_URL = "https://www.jhipster.tech/problem";
+
     public static final URI DEFAULT_TYPE = URI.create(PROBLEM_BASE_URL + "/problem-with-message");
     public static final URI CONSTRAINT_VIOLATION_TYPE = URI.create(PROBLEM_BASE_URL + "/constraint-violation");
     public static final URI PARAMETERIZED_TYPE = URI.create(PROBLEM_BASE_URL + "/parameterized");
diff --git a/src/main/java/org/hostsharing/hsadminng/web/rest/errors/ExceptionTranslator.java b/src/main/java/org/hostsharing/hsadminng/web/rest/errors/ExceptionTranslator.java
index 64d5ae3c..d810090c 100644
--- a/src/main/java/org/hostsharing/hsadminng/web/rest/errors/ExceptionTranslator.java
+++ b/src/main/java/org/hostsharing/hsadminng/web/rest/errors/ExceptionTranslator.java
@@ -3,11 +3,15 @@ package org.hostsharing.hsadminng.web.rest.errors;
 import org.hostsharing.hsadminng.web.rest.util.HeaderUtil;
 
 import org.springframework.dao.ConcurrencyFailureException;
+import org.springframework.dao.DataIntegrityViolationException;
+import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.validation.BindingResult;
 import org.springframework.web.bind.MethodArgumentNotValidException;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.ResponseStatus;
 import org.springframework.web.context.request.NativeWebRequest;
 import org.zalando.problem.DefaultProblem;
 import org.zalando.problem.Problem;
@@ -23,6 +27,8 @@ import java.util.List;
 import java.util.NoSuchElementException;
 import java.util.stream.Collectors;
 
+import static org.hostsharing.hsadminng.web.rest.errors.ErrorConstants.*;
+
 /**
  * Controller advice to translate the server side exceptions to client-friendly json structures.
  * The error response follows RFC7807 - Problem Details for HTTP APIs (https://tools.ietf.org/html/rfc7807)
@@ -48,7 +54,7 @@ public class ExceptionTranslator implements ProblemHandling {
             return entity;
         }
         ProblemBuilder builder = Problem.builder()
-            .withType(Problem.DEFAULT_TYPE.equals(problem.getType()) ? ErrorConstants.DEFAULT_TYPE : problem.getType())
+            .withType(Problem.DEFAULT_TYPE.equals(problem.getType()) ? DEFAULT_TYPE : problem.getType())
             .withStatus(problem.getStatus())
             .withTitle(problem.getTitle())
             .with(PATH_KEY, request.getNativeRequest(HttpServletRequest.class).getRequestURI());
@@ -56,7 +62,7 @@ public class ExceptionTranslator implements ProblemHandling {
         if (problem instanceof ConstraintViolationProblem) {
             builder
                 .with(VIOLATIONS_KEY, ((ConstraintViolationProblem) problem).getViolations())
-                .with(MESSAGE_KEY, ErrorConstants.ERR_VALIDATION);
+                .with(MESSAGE_KEY, ERR_VALIDATION);
         } else {
             builder
                 .withCause(((DefaultProblem) problem).getCause())
@@ -78,10 +84,10 @@ public class ExceptionTranslator implements ProblemHandling {
             .collect(Collectors.toList());
 
         Problem problem = Problem.builder()
-            .withType(ErrorConstants.CONSTRAINT_VIOLATION_TYPE)
+            .withType(CONSTRAINT_VIOLATION_TYPE)
             .withTitle("Method argument not valid")
             .withStatus(defaultConstraintViolationStatus())
-            .with(MESSAGE_KEY, ErrorConstants.ERR_VALIDATION)
+            .with(MESSAGE_KEY, ERR_VALIDATION)
             .with(FIELD_ERRORS_KEY, fieldErrors)
             .build();
         return create(ex, problem, request);
@@ -91,7 +97,7 @@ public class ExceptionTranslator implements ProblemHandling {
     public ResponseEntity<Problem> handleNoSuchElementException(NoSuchElementException ex, NativeWebRequest request) {
         Problem problem = Problem.builder()
             .withStatus(Status.NOT_FOUND)
-            .with(MESSAGE_KEY, ErrorConstants.ENTITY_NOT_FOUND_TYPE)
+            .with(MESSAGE_KEY, ENTITY_NOT_FOUND_TYPE)
             .build();
         return create(ex, problem, request);
     }
@@ -105,8 +111,20 @@ public class ExceptionTranslator implements ProblemHandling {
     public ResponseEntity<Problem> handleConcurrencyFailure(ConcurrencyFailureException ex, NativeWebRequest request) {
         Problem problem = Problem.builder()
             .withStatus(Status.CONFLICT)
-            .with(MESSAGE_KEY, ErrorConstants.ERR_CONCURRENCY_FAILURE)
+            .with(MESSAGE_KEY, ERR_CONCURRENCY_FAILURE)
             .build();
         return create(ex, problem, request);
     }
+
+    @ExceptionHandler(DataIntegrityViolationException.class)
+    @ResponseBody
+    @ResponseStatus(HttpStatus.CONFLICT)
+    public ResponseEntity<Problem> processDataIntegrityViolationException(DataIntegrityViolationException exception, NativeWebRequest request) {
+        // UX_CUSTOMER_JHI_NUMBER_INDEX_5
+        Problem problem = Problem.builder()
+            .withStatus(Status.CONFLICT)
+            .with(MESSAGE_KEY, ERR_VALIDATION_DUPLICATE)
+            .build();
+        return create(exception, problem, request);
+    }
 }
diff --git a/src/main/webapp/i18n/de/global.json b/src/main/webapp/i18n/de/global.json
index f6ef880f..70e1dbdc 100644
--- a/src/main/webapp/i18n/de/global.json
+++ b/src/main/webapp/i18n/de/global.json
@@ -123,7 +123,8 @@
             "maxbytes": "Dieses Feld sollte nicht mehr als {{max}} bytes haben.",
             "pattern": "Dieses Feld muss das Muster {{pattern}} erfüllen.",
             "number": "Dieses Feld muss eine Zahl sein.",
-            "datetimelocal": "Dieses Feld muss eine Datums- und Zeitangabe enthalten."
+            "datetimelocal": "Dieses Feld muss eine Datums- und Zeitangabe enthalten.",
+            "duplicate": "Ein Wert ist doppelt zu existierenden Daten."
         }
     },
     "error": {
diff --git a/src/main/webapp/i18n/en/global.json b/src/main/webapp/i18n/en/global.json
index 3b496f5b..a021d22a 100644
--- a/src/main/webapp/i18n/en/global.json
+++ b/src/main/webapp/i18n/en/global.json
@@ -124,7 +124,8 @@
             "pattern": "This field should follow pattern for {{ pattern }}.",
             "number": "This field should be a number.",
             "datetimelocal": "This field should be a date and time.",
-            "patternLogin": "This field can only contain letters, digits and e-mail addresses."
+            "patternLogin": "This field can only contain letters, digits and e-mail addresses.",
+            "duplicate": "This value is duplicate to existing data."
         }
     },
     "error": {