11package uk .gov .companieshouse .pscdataapi .config ;
22
3+ import static uk .gov .companieshouse .pscdataapi .PscDataApiApplication .APPLICATION_NAME_SPACE ;
4+
35import com .mongodb .MongoException ;
46import java .time .LocalDateTime ;
57import java .time .format .DateTimeParseException ;
68import java .util .LinkedHashMap ;
79import java .util .Map ;
8- import org .springframework .beans .factory .annotation .Autowired ;
910import org .springframework .dao .DataAccessException ;
1011import org .springframework .http .HttpStatus ;
1112import org .springframework .http .ResponseEntity ;
1617import org .springframework .web .context .request .WebRequest ;
1718import org .springframework .web .servlet .NoHandlerFoundException ;
1819import uk .gov .companieshouse .logging .Logger ;
20+ import uk .gov .companieshouse .logging .LoggerFactory ;
1921import uk .gov .companieshouse .pscdataapi .exceptions .BadGatewayException ;
2022import uk .gov .companieshouse .pscdataapi .exceptions .BadRequestException ;
2123import uk .gov .companieshouse .pscdataapi .exceptions .ConflictException ;
2628@ ControllerAdvice
2729public class ExceptionHandlerConfig {
2830
29- private final Logger logger ;
30-
31- @ Autowired
32- public ExceptionHandlerConfig (Logger logger ) {
33- this .logger = logger ;
34- }
35-
31+ private static final Logger LOGGER = LoggerFactory .getLogger (APPLICATION_NAME_SPACE );
3632 private static final String TIMESTAMP = "timestamp" ;
3733 private static final String MESSAGE = "message" ;
3834 private static final String EXCEPTION_ATTRIBUTE = "javax.servlet.error.exception" ;
@@ -46,8 +42,7 @@ public ExceptionHandlerConfig(Logger logger) {
4642 */
4743 @ ExceptionHandler (value = {Exception .class , SerDesException .class })
4844 public ResponseEntity <Object > handleException (Exception ex , WebRequest request ) {
49- logger .error (String .format ("Unexpected exception, response code: %s" ,
50- HttpStatus .INTERNAL_SERVER_ERROR ), ex );
45+ LOGGER .error ("Unexpected exception, response code: %s" .formatted (HttpStatus .INTERNAL_SERVER_ERROR ), ex );
5146
5247 Map <String , Object > responseBody = new LinkedHashMap <>();
5348 responseBody .put (TIMESTAMP , LocalDateTime .now ());
@@ -65,8 +60,7 @@ public ResponseEntity<Object> handleException(Exception ex, WebRequest request)
6560 */
6661 @ ExceptionHandler (value = {IllegalArgumentException .class , NoHandlerFoundException .class })
6762 public ResponseEntity <Object > handleNotFoundException (Exception ex , WebRequest request ) {
68- logger .error (String .format ("Resource not found, response code: %s" ,
69- HttpStatus .NOT_FOUND ), ex );
63+ LOGGER .error ("Resource not found, response code: %s" .formatted (HttpStatus .NOT_FOUND ), ex );
7064
7165 Map <String , Object > responseBody = new LinkedHashMap <>();
7266 responseBody .put (TIMESTAMP , LocalDateTime .now ());
@@ -84,10 +78,8 @@ public ResponseEntity<Object> handleNotFoundException(Exception ex, WebRequest r
8478 */
8579 @ ExceptionHandler (value = {MethodNotAllowedException .class ,
8680 HttpRequestMethodNotSupportedException .class })
87- public ResponseEntity <Object > handleMethodNotAllowedException (
88- Exception ex , WebRequest request ) {
89- logger .error (String .format ("Unable to process the request, response code: %s" ,
90- HttpStatus .METHOD_NOT_ALLOWED ), ex );
81+ public ResponseEntity <Object > handleMethodNotAllowedException (Exception ex , WebRequest request ) {
82+ LOGGER .error ("Method not allowed, response code: %s" .formatted (HttpStatus .METHOD_NOT_ALLOWED ), ex );
9183
9284 Map <String , Object > responseBody = new LinkedHashMap <>();
9385 responseBody .put (TIMESTAMP , LocalDateTime .now ());
@@ -107,8 +99,7 @@ public ResponseEntity<Object> handleMethodNotAllowedException(
10799 DataAccessException .class , MongoException .class })
108100 public ResponseEntity <Object > handleServiceUnavailableException (Exception ex ,
109101 WebRequest request ) {
110- logger .error (String .format ("Service unavailable, response code: %s" ,
111- HttpStatus .SERVICE_UNAVAILABLE ), ex );
102+ LOGGER .error ("Service unavailable, response code: %s" .formatted (HttpStatus .SERVICE_UNAVAILABLE ), ex );
112103
113104 Map <String , Object > responseBody = new LinkedHashMap <>();
114105 responseBody .put (TIMESTAMP , LocalDateTime .now ());
@@ -127,7 +118,7 @@ public ResponseEntity<Object> handleServiceUnavailableException(Exception ex,
127118 @ ExceptionHandler (value = {BadRequestException .class , DateTimeParseException .class ,
128119 HttpMessageNotReadableException .class })
129120 public ResponseEntity <Object > handleBadRequestException (Exception ex , WebRequest request ) {
130- logger .error (String . format ( "Bad request, response code: %s" , HttpStatus .BAD_REQUEST ), ex );
121+ LOGGER .error ("Bad request, response code: %s" . formatted ( HttpStatus .BAD_REQUEST ), ex );
131122
132123 Map <String , Object > responseBody = new LinkedHashMap <>();
133124 responseBody .put (TIMESTAMP , LocalDateTime .now ());
@@ -146,7 +137,7 @@ public ResponseEntity<Object> handleBadRequestException(Exception ex, WebRequest
146137 */
147138 @ ExceptionHandler (value = {ConflictException .class })
148139 public ResponseEntity <Object > handleConflictException (Exception ex , WebRequest request ) {
149- logger .error (String . format ( "Conflict, response code: %s" , HttpStatus .CONFLICT ), ex );
140+ LOGGER .error ("Conflict, response code: %s" . formatted ( HttpStatus .CONFLICT ), ex );
150141
151142 Map <String , Object > responseBody = new LinkedHashMap <>();
152143 responseBody .put (TIMESTAMP , LocalDateTime .now ());
@@ -157,7 +148,7 @@ public ResponseEntity<Object> handleConflictException(Exception ex, WebRequest r
157148
158149 @ ExceptionHandler (value = {BadGatewayException .class })
159150 public ResponseEntity <Object > handleBadGatewayException (Exception ex , WebRequest request ) {
160- logger .error (String . format ( "Bad Gateway, response code: %s" , HttpStatus .BAD_GATEWAY ), ex );
151+ LOGGER .error ("Bad Gateway, response code: %s" . formatted ( HttpStatus .BAD_GATEWAY ), ex );
161152
162153 Map <String , Object > responseBody = new LinkedHashMap <>();
163154 responseBody .put (TIMESTAMP , LocalDateTime .now ());
0 commit comments