#What's the correct way to handle unexpected exceptions in Spring Boot?

1 messages · Page 1 of 1 (latest)

unreal otterBOT
#

Detected code, here are some useful tools:

#

<@&1004656351647117403> please have a look, thanks.

lean ivy
#

why would u .printStackTrace it?

pulsar folio
lean ivy
#
@ExceptionHandler(NoResourceFoundException.class)
public ResponseEntity<ErrorResponse> handleNoResourceFoundException(NoResourceFoundException exception) {
    return ResponseEntity.notFound().build();
}
#

you can do something like this

pulsar folio
lean ivy
#

you can return status, message and other things too

pulsar folio
#

Spring tutorials on google are extremely inconsistent and just weird

lean ivy
# pulsar folio That might work, but do people really do this in every backend project?
@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

unreal otterBOT
# lean ivy ``` @ExceptionHandler(MethodArgumentNotValidException.class) public Response...

Detected code, here are some useful tools:

Formatted code
@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);
}
soft basin
#

spring security automatically response with notFound if an internal exception happens

pulsar folio
soft basin
#

yeah, just saying

#

it is a security issue to give the user internal details and stuff

pulsar folio
lean ivy
#

you can catch that NotFound and return with ResponseEntity

soft basin
#

it does that for you

pulsar folio
soft basin
#

but why

pulsar folio
soft basin
#

and?

pulsar folio
#

All my endpoints are public

soft basin
#

you can disable authentication 🤷‍♂️

lean ivy
pulsar folio
soft basin
#

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

soft basin
pulsar folio
soft basin
#

you should use it though 🤷‍♂️
but fine

lean ivy
pulsar folio
#

I do use security in bigger projects...

soft basin
#

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 cat_thumbs_up

pulsar folio
#

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

soft basin
#

spring security is much more 🤷‍♂️

#

and you shouldnt use spring in the first place if you care about micro optimization and file size lol

pulsar folio
#

this argument has no meaning