Skip to content

Commit 5738f22

Browse files
committed
validation feature:
add validation-error message to AppLogAspect . unify ErrorMessage for validation and appException
1 parent d0bd323 commit 5738f22

7 files changed

Lines changed: 41 additions & 40 deletions

File tree

src/main/java/ir/bigz/springbootreal/SpringbootRealApplication.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.springframework.transaction.annotation.EnableTransactionManagement;
1010

1111
//TODO add pagination to dao
12-
//TODO add validation
1312
//TODO complex model
1413

1514
@SpringBootApplication(exclude = {

src/main/java/ir/bigz/springbootreal/commons/generallog/AppLogAspect.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package ir.bigz.springbootreal.commons.generallog;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import ir.bigz.springbootreal.exception.AppException;
5+
import ir.bigz.springbootreal.exception.HttpExceptionModel;
46
import org.aspectj.lang.JoinPoint;
5-
import org.aspectj.lang.annotation.AfterReturning;
6-
import org.aspectj.lang.annotation.AfterThrowing;
7-
import org.aspectj.lang.annotation.Aspect;
8-
import org.aspectj.lang.annotation.Before;
7+
import org.aspectj.lang.annotation.*;
98
import org.slf4j.Logger;
109
import org.slf4j.LoggerFactory;
1110
import org.springframework.http.ResponseEntity;
@@ -43,4 +42,13 @@ public void logAfterThrowException(JoinPoint joinPoint, AppException exception){
4342
LOG.info("exception method: {} | errorCode: {} | message: {}",
4443
methodName, exception.getHttpErrorCode(), exception.getDetail());
4544
}
45+
46+
@AfterReturning(value = "execution(* ir.bigz.springbootreal.exception.validation.ErrorController.*(..))", returning = "object")
47+
public void logAfterThrowValidationException(JoinPoint joinPoint, Object object){
48+
HttpExceptionModel httpExceptionModel = (HttpExceptionModel) ((ResponseEntity) object).getBody();
49+
LOG.info("validation exception path: {} | errorCode: {} | errors: {}",
50+
httpExceptionModel.getValidationError().getPath(),
51+
httpExceptionModel.getHttpErrorCode(),
52+
httpExceptionModel.getValidationError().getErrors());
53+
}
4654
}

src/main/java/ir/bigz/springbootreal/exception/HttpErrorCode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ public enum HttpErrorCode {
77
ERR_10700 (10700, "Invalid Entity For Persist", HttpStatus.BAD_REQUEST),
88
ERR_10701 (10701, "process of the request has been error", HttpStatus.BAD_REQUEST),
99
ERR_10702 (10702, "User Not Found", HttpStatus.NOT_FOUND),
10-
ERR_10703 (10703, "Invalid Entity For Update", HttpStatus.BAD_REQUEST);
10+
ERR_10703 (10703, "Invalid Entity For Update", HttpStatus.BAD_REQUEST),
11+
ERR_10704 (10704, "validation Error", HttpStatus.BAD_REQUEST);
1112

1213
private final int code;
1314
private final HttpStatus status;

src/main/java/ir/bigz/springbootreal/exception/HttpExceptionHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public ResponseEntity<Object> handleApiRequestException(AppException e){
1717

1818
HttpExceptionModel apiException = new HttpExceptionModel(e.getDetail(),
1919
e.getHttpErrorCode(),
20-
timeLog());
20+
timeLog(), null);
2121

2222
return new ResponseEntity<>(apiException, e.getHttpErrorCode().getStatus());
2323
}
Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
11
package ir.bigz.springbootreal.exception;
22

3-
import java.time.ZonedDateTime;
3+
import ir.bigz.springbootreal.exception.validation.ValidationErrorResponseModel;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Builder;
6+
import lombok.Getter;
47

8+
@Getter
9+
@AllArgsConstructor
10+
@Builder
511
public class HttpExceptionModel {
612

713
private final String message;
814
private final HttpErrorCode httpErrorCode;
915
private final String timestamp;
10-
11-
public HttpExceptionModel(String message, HttpErrorCode httpErrorCode, String timestamp) {
12-
this.message = message;
13-
this.httpErrorCode = httpErrorCode;
14-
this.timestamp = timestamp;
15-
}
16-
17-
public String getMessage() {
18-
return message;
19-
}
20-
21-
public HttpErrorCode getHttpErrorCode() {
22-
return httpErrorCode;
23-
}
24-
25-
public String getTimestamp() {
26-
return timestamp;
27-
}
16+
private final ValidationErrorResponseModel validationError;
2817
}

src/main/java/ir/bigz/springbootreal/exception/validation/ErrorController.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ir.bigz.springbootreal.exception.validation;
22

3+
import ir.bigz.springbootreal.exception.HttpErrorCode;
4+
import ir.bigz.springbootreal.exception.HttpExceptionModel;
35
import org.springframework.http.HttpHeaders;
46
import org.springframework.http.HttpStatus;
57
import org.springframework.http.ResponseEntity;
@@ -26,20 +28,23 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotV
2628

2729
Map<String, String> errors = new HashMap<>();
2830
ex.getBindingResult().getAllErrors().forEach((error) -> {
29-
String fieldName = ((FieldError) error).getField();
30-
String errorMessage = error.getDefaultMessage();
31-
errors.put(fieldName, errorMessage);
32-
});
33-
34-
35-
36-
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ErrorResponseModel.builder()
37-
.errors(errors)
38-
.timestamp(timeLog())
39-
.path(request.getDescription(false)).build());
31+
String fieldName = ((FieldError) error).getField();
32+
String errorMessage = error.getDefaultMessage();
33+
errors.put(fieldName, errorMessage);
34+
});
35+
36+
return ResponseEntity.status(HttpErrorCode.ERR_10704.getStatus())
37+
.body(HttpExceptionModel.builder()
38+
.httpErrorCode(HttpErrorCode.ERR_10704)
39+
.message(HttpErrorCode.ERR_10704.getReason())
40+
.timestamp(timeLog())
41+
.validationError(ValidationErrorResponseModel.builder()
42+
.errors(errors)
43+
.path(request.getDescription(false))
44+
.build()).build());
4045
}
4146

42-
private String timeLog(){
47+
private String timeLog() {
4348
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd - HH:mm:ss z");
4449
return ZonedDateTime.now(ZoneId.of("Asia/Tehran")).format(formatter);
4550
}

src/main/java/ir/bigz/springbootreal/exception/validation/ErrorResponseModel.java renamed to src/main/java/ir/bigz/springbootreal/exception/validation/ValidationErrorResponseModel.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77

88
@Getter
99
@Builder
10-
public class ErrorResponseModel {
10+
public class ValidationErrorResponseModel {
1111

1212
private final Map<String, String> errors;
13-
private final String timestamp;
1413
private final String path;
1514
}

0 commit comments

Comments
 (0)