File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .cyberrisk .cyber_risk_platform .exception ;
2+
3+ import lombok .extern .slf4j .Slf4j ;
4+ import org .springframework .http .HttpStatus ;
5+ import org .springframework .http .ResponseEntity ;
6+ import org .springframework .web .bind .annotation .ExceptionHandler ;
7+ import org .springframework .web .bind .annotation .RestControllerAdvice ;
8+
9+ import java .time .LocalDateTime ;
10+ import java .util .Map ;
11+
12+ @ Slf4j
13+ @ RestControllerAdvice
14+ public class GlobalExceptionHandler {
15+
16+ @ ExceptionHandler (RuntimeException .class )
17+ public ResponseEntity <Map <String , Object >> handleRuntimeException (RuntimeException ex ) {
18+ log .error ("Runtime exception: {}" , ex .getMessage ());
19+ return ResponseEntity .status (HttpStatus .NOT_FOUND ).body (Map .of (
20+ "status" , 404 ,
21+ "error" , ex .getMessage (),
22+ "timestamp" , LocalDateTime .now ().toString ()
23+ ));
24+ }
25+
26+ @ ExceptionHandler (Exception .class )
27+ public ResponseEntity <Map <String , Object >> handleGenericException (Exception ex ) {
28+ log .error ("Unexpected error: {}" , ex .getMessage ());
29+ return ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR ).body (Map .of (
30+ "status" , 500 ,
31+ "error" , "An unexpected error occurred" ,
32+ "timestamp" , LocalDateTime .now ().toString ()
33+ ));
34+ }
35+ }
You can’t perform that action at this time.
0 commit comments