-
Notifications
You must be signed in to change notification settings - Fork 18
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
Niels Dommerholt
committed
Dec 27, 2016
1 parent
e1cc3dc
commit 51113f3
Showing
25 changed files
with
261 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>base-error-handler</artifactId> | ||
<groupId>com.nibado.example</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>examples</artifactId> | ||
|
||
|
||
</project> |
8 changes: 8 additions & 0 deletions
8
examples/src/main/java/com/nibado/example/errorhandlers/example1/UserNotFoundException.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,8 @@ | ||
package com.nibado.example.errorhandlers.example1; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "User not found") | ||
public class UserNotFoundException extends RuntimeException { | ||
} |
35 changes: 35 additions & 0 deletions
35
examples/src/main/java/com/nibado/example/errorhandlers/example2/ExceptionHandlers.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,35 @@ | ||
package com.nibado.example.errorhandlers.example2; | ||
|
||
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
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; | ||
|
||
@ControllerAdvice | ||
@Slf4j | ||
public class ExceptionHandlers { | ||
@ExceptionHandler(UserNotFoundException.class) | ||
@ResponseStatus(HttpStatus.NOT_FOUND) | ||
@ResponseBody | ||
public ErrorResponse handleUserNotFoundException(final UserNotFoundException ex) { | ||
log.error("User not found thrown", ex); | ||
return new ErrorResponse("USER_NOT_FOUND", "The user was not found"); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
@ExceptionHandler(Throwable.class) | ||
@ResponseBody | ||
public ErrorResponse handleThrowable(final Throwable ex) { | ||
log.error("Unexpected error", ex); | ||
return new ErrorResponse("INTERNAL_SERVER_ERROR", "An unexpected internal server error occured"); | ||
} | ||
|
||
@Data | ||
public static class ErrorResponse { | ||
private final String code; | ||
private final String message; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
examples/src/main/java/com/nibado/example/errorhandlers/example2/UserNotFoundException.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,4 @@ | ||
package com.nibado.example.errorhandlers.example2; | ||
|
||
public class UserNotFoundException extends RuntimeException { | ||
} |
30 changes: 30 additions & 0 deletions
30
examples/src/main/java/com/nibado/example/errorhandlers/example3/BaseExceptionHandler.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,30 @@ | ||
package com.nibado.example.errorhandlers.example3; | ||
|
||
import lombok.Data; | ||
import org.slf4j.Logger; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
public abstract class BaseExceptionHandler { | ||
private final Logger log; | ||
|
||
protected BaseExceptionHandler(final Logger log) { | ||
this.log = log; | ||
} | ||
|
||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
@ExceptionHandler(Throwable.class) | ||
@ResponseBody | ||
public ExceptionHandlers.ErrorResponse handleThrowable(final Throwable ex) { | ||
log.error("Unexpected error", ex); | ||
return new ErrorResponse("INTERNAL_SERVER_ERROR", "An unexpected internal server error occured"); | ||
} | ||
|
||
@Data | ||
public static class ErrorResponse { | ||
private final String code; | ||
private final String message; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
examples/src/main/java/com/nibado/example/errorhandlers/example3/ExceptionHandlers.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,24 @@ | ||
package com.nibado.example.errorhandlers.example3; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
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; | ||
|
||
@ControllerAdvice | ||
@Slf4j | ||
public class ExceptionHandlers extends BaseExceptionHandler { | ||
public ExceptionHandlers() { | ||
super(log); | ||
} | ||
|
||
@ExceptionHandler(UserNotFoundException.class) | ||
@ResponseStatus(HttpStatus.NOT_FOUND) | ||
@ResponseBody | ||
public ErrorResponse handleUserNotFoundException(final UserNotFoundException ex) { | ||
log.error("User not found thrown", ex); | ||
return new ErrorResponse("USER_NOT_FOUND", "The user was not found"); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
examples/src/main/java/com/nibado/example/errorhandlers/example3/UserNotFoundException.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,4 @@ | ||
package com.nibado.example.errorhandlers.example3; | ||
|
||
public class UserNotFoundException extends RuntimeException { | ||
} |
86 changes: 86 additions & 0 deletions
86
examples/src/main/java/com/nibado/example/errorhandlers/example4/BaseExceptionHandler.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,86 @@ | ||
package com.nibado.example.errorhandlers.example4; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import org.slf4j.Logger; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.HttpRequestMethodNotSupportedException; | ||
import org.springframework.web.bind.MissingServletRequestParameterException; | ||
import org.springframework.web.bind.ServletRequestBindingException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.springframework.http.HttpStatus.*; | ||
|
||
public abstract class BaseExceptionHandler { | ||
private static final ExceptionMapping DEFAULT_ERROR = new ExceptionMapping( | ||
"SERVER_ERROR", | ||
"Internal server error", | ||
INTERNAL_SERVER_ERROR); | ||
|
||
private final Logger log; | ||
private final Map<Class, ExceptionMapping> exceptionMappings = new HashMap<>(); | ||
|
||
public BaseExceptionHandler(final Logger log) { | ||
this.log = log; | ||
|
||
registerMapping( | ||
MissingServletRequestParameterException.class, | ||
"MISSING_PARAMETER", | ||
"Missing request parameter", | ||
BAD_REQUEST); | ||
registerMapping( | ||
MethodArgumentTypeMismatchException.class, | ||
"ARGUMENT_TYPE_MISMATCH", | ||
"Argument type mismatch", | ||
BAD_REQUEST); | ||
registerMapping( | ||
HttpRequestMethodNotSupportedException.class, | ||
"METHOD_NOT_SUPPORTED", | ||
"HTTP method not supported", | ||
METHOD_NOT_ALLOWED); | ||
registerMapping( | ||
ServletRequestBindingException.class, | ||
"MISSING_HEADER", | ||
"Missing header in request", | ||
BAD_REQUEST); | ||
} | ||
|
||
@ExceptionHandler(Throwable.class) | ||
@ResponseBody | ||
public ErrorResponse handleThrowable(final Throwable ex, final HttpServletResponse response) { | ||
ExceptionMapping mapping = exceptionMappings.getOrDefault(ex.getClass(), DEFAULT_ERROR); | ||
|
||
response.setStatus(mapping.status.value()); | ||
|
||
log.error("{} ({}): {}", mapping.message, mapping.code, ex.getMessage(), ex); | ||
|
||
return new ErrorResponse(mapping.code, mapping.message); | ||
} | ||
|
||
protected void registerMapping( | ||
final Class<?> clazz, | ||
final String code, | ||
final String message, | ||
final HttpStatus status) { | ||
exceptionMappings.put(clazz, new ExceptionMapping(code, message, status)); | ||
} | ||
|
||
@Data | ||
public static class ErrorResponse { | ||
private final String code; | ||
private final String message; | ||
} | ||
|
||
@AllArgsConstructor | ||
private static class ExceptionMapping { | ||
private final String message; | ||
private final String code; | ||
private final HttpStatus status; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
examples/src/main/java/com/nibado/example/errorhandlers/example4/ExceptionHandlers.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,14 @@ | ||
package com.nibado.example.errorhandlers.example4; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
|
||
@ControllerAdvice | ||
@Slf4j | ||
public class ExceptionHandlers extends BaseExceptionHandler { | ||
public ExceptionHandlers() { | ||
super(log); | ||
registerMapping(UserNotFoundException.class, "USER_NOT_FOUND", "User not found", HttpStatus.NOT_FOUND); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
examples/src/main/java/com/nibado/example/errorhandlers/example4/UserNotFoundException.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,4 @@ | ||
package com.nibado.example.errorhandlers.example4; | ||
|
||
public class UserNotFoundException extends RuntimeException { | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...xample/baseerrorhandler/lib/ErrorDTO.java → ...o/example/errorhandlers/lib/ErrorDTO.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
4 changes: 0 additions & 4 deletions
4
...nibado/example/baseerrorhandler/service/controller/exception/NotImplementedException.java
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...baseerrorhandler/service/Application.java → ...le/errorhandlers/service/Application.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
2 changes: 1 addition & 1 deletion
2
...nfiguration/ApplicationConfiguration.java → ...nfiguration/ApplicationConfiguration.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
12 changes: 6 additions & 6 deletions
12
...dler/service/controller/ErrorHandler.java → ...lers/service/controller/ErrorHandler.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
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
4 changes: 4 additions & 0 deletions
4
...om/nibado/example/errorhandlers/service/controller/exception/NotImplementedException.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,4 @@ | ||
package com.nibado.example.errorhandlers.service.controller.exception; | ||
|
||
public class NotImplementedException extends RuntimeException { | ||
} |
2 changes: 1 addition & 1 deletion
2
...ller/exception/UserNotFoundException.java → ...ller/exception/UserNotFoundException.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
8 changes: 8 additions & 0 deletions
8
...src/main/java/com/nibado/example/errorhandlers/service/example/UserNotFoundException.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,8 @@ | ||
package com.nibado.example.errorhandlers.service.example; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "User not found") | ||
public class UserNotFoundException extends RuntimeException { | ||
} |
6 changes: 3 additions & 3 deletions
6
...rhandler/service/service/UserService.java → ...handlers/service/service/UserService.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
2 changes: 1 addition & 1 deletion
2
...rhandler/service/service/domain/User.java → ...handlers/service/service/domain/User.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
2 changes: 1 addition & 1 deletion
2
...xception/InvalidSearchParamException.java → ...xception/InvalidSearchParamException.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
2 changes: 1 addition & 1 deletion
2
...ervice/controller/UserControllerTest.java → ...ervice/controller/UserControllerTest.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
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