Skip to content

Instantly share code, notes, and snippets.

@namila007
Last active March 29, 2019 10:00
Show Gist options
  • Save namila007/5f29b8ee9d424a2040bc48d53c1207d7 to your computer and use it in GitHub Desktop.
Save namila007/5f29b8ee9d424a2040bc48d53c1207d7 to your computer and use it in GitHub Desktop.
@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler
{
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<Object> resourceEntityNotFound( ResourceNotFoundException ex )
{
ApiError apiError = createError( ex.getLocalizedMessage(), HttpStatus.BAD_REQUEST, ex );
return new ResponseEntity<>( apiError, apiError.getStatus() );
}
private ApiError createError( String msg, HttpStatus status, Exception e )
{
ApiError apiError = new ApiError( status );
apiError.setMessage( msg );
apiError.setDebugMessage( e.getMessage() );
return apiError;
}
}
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException
{
public ResourceNotFoundException( String exception )
{
super( exception );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment