File tree Expand file tree Collapse file tree
src/main/java/com/samourai/javaserver Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .samourai .javaserver .exceptions ;
2+
3+ import org .slf4j .Logger ;
4+ import org .slf4j .LoggerFactory ;
5+ import org .springframework .http .HttpStatus ;
6+
7+ public class NotifiableException extends Exception {
8+ private static final Logger log = LoggerFactory .getLogger (NotifiableException .class );
9+ private static final HttpStatus STATUS_DEFAULT = HttpStatus .INTERNAL_SERVER_ERROR ;
10+
11+ private HttpStatus httpStatus ;
12+
13+ public NotifiableException (String message ) {
14+ this (message , STATUS_DEFAULT );
15+ }
16+
17+ public NotifiableException (HttpStatus status ) {
18+ this (status .getReasonPhrase (), status );
19+ }
20+
21+ public NotifiableException (String message , HttpStatus httpStatus ) {
22+ super (message );
23+ this .httpStatus = httpStatus ;
24+ }
25+
26+ public HttpStatus getHttpStatus () {
27+ return httpStatus ;
28+ }
29+
30+ public static NotifiableException computeNotifiableException (Exception e ) {
31+ if (NotifiableException .class .isAssignableFrom (e .getClass ())) {
32+ return (NotifiableException ) e ;
33+ }
34+ log .warn ("Exception obfuscated to user" , e );
35+ return new NotifiableException ("Error" );
36+ }
37+ }
Original file line number Diff line number Diff line change 1+ package com .samourai .javaserver .rest ;
2+
3+ import com .samourai .javaserver .exceptions .NotifiableException ;
4+ import org .springframework .http .HttpHeaders ;
5+ import org .springframework .http .HttpStatus ;
6+ import org .springframework .http .ResponseEntity ;
7+ import org .springframework .web .bind .MethodArgumentNotValidException ;
8+ import org .springframework .web .bind .annotation .ExceptionHandler ;
9+ import org .springframework .web .context .request .WebRequest ;
10+ import org .springframework .web .servlet .mvc .method .annotation .ResponseEntityExceptionHandler ;
11+
12+ public abstract class AbstractRestExceptionHandler extends ResponseEntityExceptionHandler {
13+ protected abstract Object handleError (NotifiableException e );
14+
15+ @ ExceptionHandler (value = {Exception .class })
16+ public ResponseEntity <Object > handleException (Exception e ) {
17+ NotifiableException notifiable = NotifiableException .computeNotifiableException (e );
18+ Object restErrorResponse = handleError (notifiable );
19+ return new ResponseEntity <>(restErrorResponse , notifiable .getHttpStatus ());
20+ }
21+
22+ @ Override
23+ protected ResponseEntity <Object > handleMethodArgumentNotValid (
24+ MethodArgumentNotValidException e ,
25+ HttpHeaders headers ,
26+ HttpStatus status ,
27+ WebRequest request ) {
28+ return handleException (
29+ new NotifiableException ("Invalid parameter: " + e .getParameter ().getParameterName ()));
30+ }
31+ }
You can’t perform that action at this time.
0 commit comments