Package sk.antons.sbutils.http
Class JsonExceptionHandler
java.lang.Object
sk.antons.sbutils.http.JsonExceptionHandler
Helper class for creating exception advice class.
It allows you to define following
<ul>
<li> method for logging exception
<li> method for resolving http status code
<li> method for converting exception to json node response
</ul>
Exception is handled in following order
<ul>
<li> if logger is defined exception is send to logger
<li> then traversing causedBy chain is searching for first exception for which statusResolver returns code (if not nound firstone is used with code 500)
<li> then processor converts exception to json http response
</ul>
<pre>
@ControllerAdvice(basePackages = "sk.antons.project.api")
public class JsonExceptionAdvice {
private static Logger log = LoggerFactory.getLogger(JsonExceptionAdvice.class);
JsonExceptionHandler handler = JsonExceptionHandler.instance()
.logger(t -> log.info("request failed {} ", Stk.trace(t)))
// .statusResolver(JsonExceptionHandler.DefaultStatusResolver.instance()
// .status(MyAppException.class, HttpStatus.CONFLICT))
// .processor(JsonExceptionHandler.DefaultExceptionProcessor.instance())
);
@ExceptionHandler(Throwable.class)
public ResponseEntity<ObjectNode> throwable(final Throwable ex) {
return handler.process(ex);
}
}
</pre>- Author:
- antons
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classSimple exception to ObjectNode mapper.static classResolve http status from exception class. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic JsonExceptionHandlerinstance()method for logging exceptionorg.springframework.http.ResponseEntity<com.fasterxml.jackson.databind.node.ObjectNode>Process Exception to http response.method for converting exception to json responsestatusResolver(Function<Throwable, org.springframework.http.HttpStatus> value) method for resolving http status from exception
-
Constructor Details
-
JsonExceptionHandler
public JsonExceptionHandler()
-
-
Method Details
-
instance
-
logger
method for logging exception- Parameters:
value- method- Returns:
- this
-
statusResolver
public JsonExceptionHandler statusResolver(Function<Throwable, org.springframework.http.HttpStatus> value) method for resolving http status from exception- Parameters:
value- method- Returns:
- this
-
processor
public JsonExceptionHandler processor(Function<Throwable, com.fasterxml.jackson.databind.node.ObjectNode> value) method for converting exception to json response- Parameters:
value- method- Returns:
- this
-
process
public org.springframework.http.ResponseEntity<com.fasterxml.jackson.databind.node.ObjectNode> process(Throwable ex) Process Exception to http response. (main implementation)- Parameters:
ex-- Returns:
-