-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0ab7da9
commit abf16e4
Showing
9 changed files
with
176 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/com/juliuskrah/quartz/web/rest/errors/ErrorVO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.juliuskrah.quartz.web.rest.errors; | ||
|
||
import java.io.Serializable; | ||
import java.util.List; | ||
|
||
import org.immutables.value.Value; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
|
||
@Value.Immutable | ||
@JsonSerialize(as = ImmutableErrorVO.class) | ||
@JsonDeserialize(as = ImmutableErrorVO.class) | ||
public interface ErrorVO extends Serializable { | ||
String message(); | ||
|
||
String description(); | ||
|
||
List<FieldErrorVO> fieldErrors(); | ||
} |
95 changes: 95 additions & 0 deletions
95
src/main/java/com/juliuskrah/quartz/web/rest/errors/ExceptionTranslator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.juliuskrah.quartz.web.rest.errors; | ||
|
||
import static org.springframework.http.HttpStatus.BAD_REQUEST; | ||
import static org.springframework.http.HttpStatus.CONFLICT; | ||
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.core.annotation.AnnotationUtils; | ||
import org.springframework.dao.DataIntegrityViolationException; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class ExceptionTranslator { | ||
|
||
@ExceptionHandler(IllegalStateException.class) | ||
@ResponseStatus(BAD_REQUEST) | ||
public ErrorVO processUnsupportedTriggerError(IllegalStateException ex) { | ||
ErrorVO dto = ImmutableErrorVO.builder() | ||
.message("400: Bad Request") | ||
.description(ex.getMessage()) | ||
.build(); | ||
return dto; | ||
} | ||
|
||
@ExceptionHandler(IllegalArgumentException.class) | ||
@ResponseStatus(BAD_REQUEST) | ||
public ErrorVO processInvalidCronExpressionError(IllegalArgumentException ex) { | ||
ErrorVO dto = ImmutableErrorVO.builder() | ||
.message("400: Bad Request") | ||
.description(ex.getMessage()) | ||
.build(); | ||
return dto; | ||
} | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
@ResponseStatus(BAD_REQUEST) | ||
public ErrorVO processValidationError(MethodArgumentNotValidException ex) { | ||
BindingResult result = ex.getBindingResult(); | ||
List<FieldError> fieldErrors = result.getFieldErrors(); | ||
ImmutableErrorVO.Builder builder = ImmutableErrorVO.builder() | ||
.message("400: Bad Request") | ||
.description("Validation errors exist"); | ||
|
||
for (FieldError fieldError : fieldErrors) { | ||
builder.addFieldErrors(ImmutableFieldErrorVO.builder() | ||
.objectName(fieldError.getObjectName()) | ||
.field(fieldError.getField()) | ||
.message(fieldError.getCode()) | ||
.build()); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
@ExceptionHandler(DataIntegrityViolationException.class) | ||
@ResponseStatus(CONFLICT) | ||
public ErrorVO processDataIntegrityViolationError(DataIntegrityViolationException ex) { | ||
ErrorVO dto = ImmutableErrorVO.builder() | ||
.message("409: Conflict") | ||
.description(ex.getMessage()) | ||
.build(); | ||
return dto; | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<ErrorVO> processException(Exception ex) throws Exception { | ||
if (log.isDebugEnabled()) { | ||
log.debug("An unexpected error occurred: {}", ex.getMessage(), ex); | ||
} else { | ||
log.error("An unexpected error occurred: {}", ex.getMessage()); | ||
} | ||
// If the exception is annotated with @ResponseStatus rethrow it and let | ||
// the framework handle it | ||
// AnnotationUtils is a Spring Framework utility class. | ||
if (AnnotationUtils.findAnnotation | ||
(ex.getClass(), ResponseStatus.class) != null) | ||
throw ex; | ||
|
||
ErrorVO dto = ImmutableErrorVO.builder() | ||
.message("500: Internal server error") | ||
.description("Internal server error has occurred") | ||
.build(); | ||
return ResponseEntity.status(INTERNAL_SERVER_ERROR).body(dto); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/juliuskrah/quartz/web/rest/errors/FieldErrorVO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.juliuskrah.quartz.web.rest.errors; | ||
|
||
import java.io.Serializable; | ||
|
||
import org.immutables.value.Value; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
|
||
@Value.Immutable | ||
@JsonSerialize(as = ImmutableFieldErrorVO.class) | ||
@JsonDeserialize(as = ImmutableFieldErrorVO.class) | ||
public interface FieldErrorVO extends Serializable { | ||
String objectName(); | ||
|
||
String field(); | ||
|
||
String message(); | ||
} |