#What's the correct way to handle unexpected exceptions in Spring Boot?
1 messages · Page 1 of 1 (latest)
why would u .printStackTrace it?
Because if a user encounters an internal server error, I want to be able to check what happened?
I would log things into log file
@ExceptionHandler(NoResourceFoundException.class)
public ResponseEntity<ErrorResponse> handleNoResourceFoundException(NoResourceFoundException exception) {
return ResponseEntity.notFound().build();
}
you can do something like this
I don't think a free host(which I'm using) would allow that lol
then try to return something with ResponseEntity
you can return status, message and other things too
That might work, but do people really do this in every backend project?
Spring tutorials on google are extremely inconsistent and just weird
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex) {
List<ErrorDetails> errors = new ArrayList<>();
ex.getBindingResult().getFieldErrors().forEach(error -> {
String errorMessage = error.getDefaultMessage();
String field = error.getField();
errors.add(new ErrorDetails(errorMessage, field, LocalDateTime.now()));
});
log.error(ex.getMessage(), errors);
return ResponseEntity.badRequest().body(errors);
}
from one open source spring project
Detected code, here are some useful tools:
@ExceptionHandler(MethodArgumentNotValidException.class ) public ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex) {
List<ErrorDetails> errors = new ArrayList<>();
ex.getBindingResult().getFieldErrors().forEach(error -> {
String errorMessage = error.getDefaultMessage();
String field = error.getField();
errors.add(new ErrorDetails(errorMessage, field, LocalDateTime.now()));
}
);
log.error(ex.getMessage(), errors);
return ResponseEntity.badRequest().body(errors);
}
spring security automatically response with notFound if an internal exception happens
I'm not currently using spring security
I agree, that's why I wanted to prevent exception messages from leaking
you can catch that NotFound and return with ResponseEntity
why not spring security then?
it does that for you
I guess that's what I will do
but why
I don't have a need for a user system or anything like that
and?
All my endpoints are public
you can disable authentication 🤷♂️
i think he doesn't need security or maybe just not good at security
which defeats the purpose of using spring security lol
and you asked for this:
That might work, but do people really do this in every backend project?
and no, people dont do it manually, it is provided by spring security
no spring security is sooooo much more than just authentication
as I said I just don't need it currently
you should use it though 🤷♂️
but fine
Anyways, try to make it in that way
but I suggest you to use security in later projects
I do use security in bigger projects...
why not use it in smaller projects?
whatever
if you really want to do it yourself for some obscure reason then sure, do it like it was mentioned before with exception handler 
it's not obscure, but okay
there's 0 problems with my way
You don't use a big library for just 1% of its functionality
it makes the file size bigger, and just makes no sense in general
spring security is much more 🤷♂️
and you shouldnt use spring in the first place if you care about micro optimization and file size lol
I never said it doesn't have more content, I said that I don't need any of it
this argument has no meaning