Skip to content

Commit e64ad3b

Browse files
committed
add exception handler
1 parent 95aebae commit e64ad3b

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

0 commit comments

Comments
 (0)