-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestExceptionHandler.java
More file actions
73 lines (64 loc) · 2.89 KB
/
RestExceptionHandler.java
File metadata and controls
73 lines (64 loc) · 2.89 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.app.exception;
import com.app.dto.StandardResponse;
import com.app.utils.MessageConstant;
import com.app.utils.ResponseUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.stream.Collectors;
@RestControllerAdvice
@RequiredArgsConstructor
@Log4j2
public class RestExceptionHandler {
private final ResponseUtil responseUtil;
/**
* Handles validation errors for method arguments.
*
* @param ex The MethodArgumentNotValidException to handle.
* @return A ResponseEntity with a structured error response.
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<StandardResponse<Object>> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
return buildErrorResponse(MessageConstant.E1005, HttpStatus.BAD_REQUEST, ex.getBindingResult().getFieldErrors().stream()
.collect(Collectors.toMap(FieldError::getField, responseUtil::getMessage)));
}
/**
* Handles custom ServiceException.
*
* @param ex The ServiceException to handle.
* @return A ResponseEntity with an error response.
*/
@ExceptionHandler(CustomRuntimeException.class)
public ResponseEntity<StandardResponse<Object>> handleServiceException(CustomRuntimeException ex) {
log.error("Service exception: ", ex);
return buildErrorResponse(ex.getResponseCode(), ex.getHttpStatus(), ex.getArgs());
}
/**
* Handles generic RuntimeExceptions.
*
* @param ex The RuntimeException to handle.
* @return A ResponseEntity with an error response.
*/
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<StandardResponse<Object>> handleRuntimeException(RuntimeException ex) {
log.error("Runtime exception: ", ex);
return buildErrorResponse(MessageConstant.E1000, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
}
/**
* Utility method to build a standardized error response.
*
* @param responseCode The response code for the error.
* @param details The error details to include in the response.
* @param httpStatus The HTTP status for the response.
* @return A ResponseEntity containing the StandardResponse.
*/
private ResponseEntity<StandardResponse<Object>> buildErrorResponse(String responseCode, HttpStatus httpStatus, Object... details) {
StandardResponse<Object> response = responseUtil.createErrorResponse(responseCode, details);
return ResponseEntity.status(httpStatus).body(response);
}
}