Skip to content

Commit 9651f44

Browse files
product-api: exceptions
1 parent cffbba8 commit 9651f44

File tree

3 files changed

+101
-4
lines changed

3 files changed

+101
-4
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.amigoscode.exception;
2+
3+
import java.time.Instant;
4+
import java.util.Map;
5+
6+
public record ErrorResponse(
7+
String message,
8+
String error,
9+
int statusCode,
10+
String path,
11+
Instant timestamp,
12+
Map<String, String> fieldErrors) {
13+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.amigoscode.exception;
2+
3+
import jakarta.servlet.http.HttpServletRequest;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.validation.FieldError;
7+
import org.springframework.web.bind.MethodArgumentNotValidException;
8+
import org.springframework.web.bind.annotation.ControllerAdvice;
9+
import org.springframework.web.bind.annotation.ExceptionHandler;
10+
11+
import java.time.Instant;
12+
import java.util.Map;
13+
import java.util.Objects;
14+
import java.util.stream.Collectors;
15+
16+
@ControllerAdvice
17+
public class GlobalExceptionHandler {
18+
19+
@ExceptionHandler(ResourceNotFound.class)
20+
public ResponseEntity<ErrorResponse> handleResourceNotFound(
21+
ResourceNotFound ex,
22+
HttpServletRequest request
23+
) {
24+
ErrorResponse errorResponse = new ErrorResponse(
25+
ex.getMessage(),
26+
HttpStatus.NOT_FOUND.getReasonPhrase(),
27+
HttpStatus.NOT_FOUND.value(),
28+
request.getRequestURI(),
29+
Instant.now(),
30+
null
31+
);
32+
return new ResponseEntity<>(
33+
errorResponse,
34+
HttpStatus.NOT_FOUND
35+
);
36+
}
37+
38+
@ExceptionHandler(MethodArgumentNotValidException.class)
39+
public ResponseEntity<ErrorResponse> handleValidationException(
40+
MethodArgumentNotValidException ex,
41+
HttpServletRequest request
42+
) {
43+
44+
Map<String, String> errors = ex.getBindingResult()
45+
.getFieldErrors()
46+
.stream()
47+
.collect(Collectors.toMap(
48+
FieldError::getField,
49+
fieldError ->
50+
Objects.requireNonNullElse(
51+
fieldError.getDefaultMessage(),
52+
"no error available"
53+
)
54+
));
55+
ErrorResponse errorResponse = new ErrorResponse(
56+
ex.getMessage(),
57+
HttpStatus.BAD_REQUEST.getReasonPhrase(),
58+
HttpStatus.BAD_REQUEST.value(),
59+
request.getRequestURI(),
60+
Instant.now(),
61+
errors
62+
);
63+
return new ResponseEntity<>(
64+
errorResponse,
65+
HttpStatus.BAD_REQUEST
66+
);
67+
}
68+
69+
@ExceptionHandler(Exception.class)
70+
public ResponseEntity<ErrorResponse> handleGenericException(
71+
Exception ex,
72+
HttpServletRequest request
73+
) {
74+
ErrorResponse errorResponse = new ErrorResponse(
75+
ex.getMessage(),
76+
HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),
77+
HttpStatus.INTERNAL_SERVER_ERROR.value(),
78+
request.getRequestURI(),
79+
Instant.now(),
80+
null
81+
);
82+
return new ResponseEntity<>(
83+
errorResponse,
84+
HttpStatus.INTERNAL_SERVER_ERROR
85+
);
86+
}
87+
88+
}

src/main/java/com/amigoscode/exception/ResourceNotFound.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package com.amigoscode.exception;
22

3-
import org.springframework.http.HttpStatus;
4-
import org.springframework.web.bind.annotation.ResponseStatus;
5-
6-
@ResponseStatus(HttpStatus.NOT_FOUND)
73
public class ResourceNotFound extends RuntimeException {
84
public ResourceNotFound(String message) {
95
super(message);

0 commit comments

Comments
 (0)