Skip to content

Commit e722dc7

Browse files
Feat: Implement method to handle validation exception
1 parent eeeee4e commit e722dc7

File tree

5 files changed

+60
-19
lines changed

5 files changed

+60
-19
lines changed

src/main/java/com/ecommerce/order/exception/CommonException.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
@Getter
99
@AllArgsConstructor
1010
public class CommonException extends RuntimeException {
11-
private HttpStatusCode statusCode;
12-
private String errorCode;
13-
private String message;
11+
private final HttpStatusCode statusCode;
12+
private final String errorCode;
13+
private final String message;
1414
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.ecommerce.order.exception;
22

33
import org.springframework.http.HttpStatus;
4-
import org.springframework.web.bind.annotation.ResponseStatus;
54

6-
@ResponseStatus(value = HttpStatus.NOT_FOUND)
75
public class EntityNotFoundException extends CommonException {
86

97
private static final long serialVersionUID = 1L;
108

119
public <T> EntityNotFoundException(Class<T> entity) {
1210
super(HttpStatus.NOT_FOUND, ExceptionCodes.ENTITY_NOT_FOUND, entity.getSimpleName() + " not found");
1311
}
14-
}
12+
13+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.ecommerce.order.exception;
22

3-
import lombok.NoArgsConstructor;
3+
import lombok.experimental.UtilityClass;
44

5-
@NoArgsConstructor
5+
@UtilityClass
66
public class ExceptionCodes {
77
public static final String ENTITY_NOT_FOUND = "ENTITY_NOT_FOUND";
8+
public static final String VALIDATION_ERROR = "VALIDATION_ERROR";
89
}
Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,54 @@
11
package com.ecommerce.order.exception;
22

3-
import org.springframework.http.ResponseEntity;
4-
import org.springframework.web.bind.annotation.ControllerAdvice;
3+
import java.util.Date;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.validation.FieldError;
9+
import org.springframework.web.bind.MethodArgumentNotValidException;
510
import org.springframework.web.bind.annotation.ExceptionHandler;
6-
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
11+
import org.springframework.web.bind.annotation.ResponseStatus;
12+
import org.springframework.web.bind.annotation.RestControllerAdvice;
13+
import org.springframework.web.context.request.WebRequest;
714

815
import com.ecommerce.order.exception.dto.ErrorResponse;
916

10-
@ControllerAdvice
11-
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
17+
@RestControllerAdvice
18+
public class GlobalExceptionHandler {
19+
1220
@ExceptionHandler(CommonException.class)
13-
public ResponseEntity<ErrorResponse> handleCommonException(CommonException ex) {
14-
return ResponseEntity.status(ex.getStatusCode())
15-
.body(new ErrorResponse(ex.getStatusCode().value(), ex.getErrorCode(), ex.getMessage()));
21+
@ResponseStatus(value = HttpStatus.NOT_FOUND)
22+
public ErrorResponse<String> handleCommonException(
23+
CommonException ex,
24+
WebRequest request) {
25+
26+
return (new ErrorResponse<String>(
27+
ex.getStatusCode().value(),
28+
ex.getErrorCode(),
29+
ex.getMessage(),
30+
request.getDescription(false),
31+
new Date()));
1632
}
17-
}
33+
34+
@ExceptionHandler(MethodArgumentNotValidException.class)
35+
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
36+
public ErrorResponse<Map<String, String>> handleValidationException(
37+
MethodArgumentNotValidException ex,
38+
WebRequest request) {
39+
Map<String, String> errorMessages = new HashMap<>();
40+
ex.getBindingResult().getAllErrors().forEach(error -> {
41+
String fieldName = ((FieldError) error).getField();
42+
String errorMessage = error.getDefaultMessage();
43+
errorMessages.put(fieldName, errorMessage);
44+
});
45+
46+
return new ErrorResponse<>(
47+
HttpStatus.BAD_REQUEST.value(),
48+
ExceptionCodes.VALIDATION_ERROR,
49+
errorMessages,
50+
request.getDescription(false),
51+
new Date());
52+
}
53+
54+
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.ecommerce.order.exception.dto;
22

3+
import java.util.Date;
4+
35
import lombok.Data;
46

57
@Data
6-
public class ErrorResponse {
8+
public class ErrorResponse<T> {
79
private final int statusCode;
810
private final String errorCode;
9-
private final String message;
11+
private final T message;
12+
private final String description;
13+
private final Date timestamp;
1014
}

0 commit comments

Comments
 (0)