Skip to content

Commit

Permalink
feat: add timestamp field for exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
thisdudkin committed Aug 25, 2024
1 parent 99291bd commit 5e9e16a
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.WebRequest;

import java.time.ZonedDateTime;

import static org.springframework.http.HttpStatus.BAD_REQUEST;

/**
Expand All @@ -53,9 +55,9 @@ public class ExceptionControllerAdvice {
* @param className The name of the exception class
* @param exMessage The message of the exception
*/
private record ErrorInfo(String className, String exMessage) {
private record ErrorInfo(String className, String exMessage, ZonedDateTime timestamp) {
public ErrorInfo(Exception ex) {
this(ex.getClass().getName(), ex.getLocalizedMessage());
this(ex.getClass().getName(), ex.getLocalizedMessage(), ZonedDateTime.now());
}
}

Expand Down Expand Up @@ -101,7 +103,7 @@ public ResponseEntity<ErrorInfo> handleMethodArgumentNotValidException(MethodArg
BindingResult bindingResult = ex.getBindingResult();
if (bindingResult.hasErrors()) {
errors.addAllErrors(bindingResult);
return ResponseEntity.badRequest().body(new ErrorInfo("MethodArgumentNotValidException", "Validation failed"));
return ResponseEntity.badRequest().body(new ErrorInfo("MethodArgumentNotValidException", "Validation failed", ZonedDateTime.now()));
}
return ResponseEntity.badRequest().build();
}
Expand Down

0 comments on commit 5e9e16a

Please sign in to comment.