-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalExceptionHandler.java
More file actions
51 lines (39 loc) · 2.06 KB
/
GlobalExceptionHandler.java
File metadata and controls
51 lines (39 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.kafein.common.exception;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
@ControllerAdvice
public class GlobalExceptionHandler {
public static final String TIMESTAMP_KEY = "timestamp";
public static final String MESSAGE_KEY = "message";
public static final String DETAILS_KEY = "details";
@ExceptionHandler(Exception.class)
public ResponseEntity<Object> handleGlobalException(Exception ex, WebRequest request) {
Map<String, Object> errorDetails = new HashMap<>();
errorDetails.put(TIMESTAMP_KEY, LocalDateTime.now());
errorDetails.put(MESSAGE_KEY, ex.getMessage());
errorDetails.put(DETAILS_KEY, request.getDescription(false));
return new ResponseEntity<>(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<Object> handleResourceNotFoundException(ResourceNotFoundException ex, WebRequest request) {
Map<String, Object> errorDetails = new HashMap<>();
errorDetails.put(TIMESTAMP_KEY, LocalDateTime.now());
errorDetails.put(MESSAGE_KEY, ex.getMessage());
errorDetails.put(DETAILS_KEY, request.getDescription(false));
return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND);
}
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<Object> handleIllegalArgumentException(IllegalArgumentException ex, WebRequest request) {
Map<String, Object> errorDetails = new HashMap<>();
errorDetails.put(TIMESTAMP_KEY, LocalDateTime.now());
errorDetails.put(MESSAGE_KEY, ex.getMessage());
errorDetails.put(DETAILS_KEY, request.getDescription(false));
return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
}
}