-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathGlobalExceptionHandler.java
More file actions
50 lines (41 loc) · 2.13 KB
/
GlobalExceptionHandler.java
File metadata and controls
50 lines (41 loc) · 2.13 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.ironhack;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingRequestHeaderException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import jakarta.servlet.ServletException;
import org.springframework.util.MultiValueMap;
import java.util.HashMap;
import java.util.Map;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Map<String, String>> handleValidationExceptions(MethodArgumentNotValidException ex) {
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getFieldErrors().forEach(error ->
errors.put(error.getField(), error.getDefaultMessage()));
return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(MissingRequestHeaderException.class)
public ResponseEntity<Map<String, String>> handleMissingHeader(MissingRequestHeaderException ex) {
Map<String, String> error = new HashMap<>();
error.put("error", "Missing API-Key header. You must provide the 'API-Key' in your request headers.");
return new ResponseEntity<>(error, HttpStatus.UNAUTHORIZED);
}
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<Map<String, String>> handleRuntimeExceptions(RuntimeException ex) {
Map<String, String> error = new HashMap<>();
String message = ex.getMessage();
error.put("error", message);
if (message.contains("not found")) {
return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
} else if (message.contains("Invalid API Key")) {
return new ResponseEntity<>(error, HttpStatus.UNAUTHORIZED);
} else if (message.contains("Invalid price range")) {
return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);
}
}