I have an already implemented controller method which returns a value or throws an exception.
@Override
@PostMapping(Endpoint.SUB_RESOURCE)
@ResponseStatus(HttpStatus.CREATED)
public EmployeeBankAccountOutputDto createBankAccount(@ApiParam("Account data") @RequestBody final EmployeeBankAccountInputDto accountCreationDto) {
try {
return service.createBankAccount(accountCreationDto);
} catch (InvalidAccountDataException ex) {
throw new InvalidAccountDataResponseException(ex, ex.getErrors());}
} catch (CountryNotFoundException ex) {
throw new CountryNotFoundResponseException(ex.getCountryCode(), ex);
}
}
In this code I want to just log exception message in warning level and not throwing exception because I don't want to see something like :
14:37:02,610 ERROR [] c.m.w.ApiExceptionHandler:135 - Account API error occurred: 400
So if I just put
log.warn(ex.getMessage()); to the first catch statement, it is giving
missing return statement
error for that catch block as normal. So How can handle not throwing exception to calling api and just send the exception message properly, because return type of the method is a dto?
ResponseEntityExceptionHandler.