vehk
vehk

Reputation: 107

Any way to pass second parameter in exceptionHandler?

Have a service method which can throw an exception

if (NOT_FOUND) {
    throw new ResourceNotFoundException("Resource not found");
}

And ControllerAdvice with exceptionHandler

@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity handleResourceNotFound(Exception ex) {
  return new ResponseEntity("your-error-message");
}

So i need to pass another String as param and access it form exceptionHanlder:

if (NOT_FOUND) {
    String param2 = "param2";
    throw new ResourceNotFoundException("Recource not found", param2);
}


@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<Problem> handleResourceNotFound(Exception ex) {
  doomething(param2);
  return new ResponseEntity({your-error-message});
}

Is there any way to do it?

Upvotes: 0

Views: 2403

Answers (3)

Oleg Cherednik
Oleg Cherednik

Reputation: 18255

Just update your advice:

@ExceptionHandler(ResourceNotFoundException.class)
// public ResponseEntity handleResourceNotFound(Exception ex) {
public ResponseEntity handleResourceNotFound(ResourceNotFoundException ex) {
  // ex contains all parameters that you need
  return new ResponseEntity("your-error-message");
}

Upvotes: 0

Martin Frank
Martin Frank

Reputation: 3474

i refer to the error handle tutorial (chapter 6.3) , where you see an proper usage of the Exception Handler as you did in your code snippet.

there is an interesing aspect of your approach:

Of course, we'll use the global exception handling mechanism that we discussed earlier to handle the AccessDeniedException as well:

so the purpose of your Exception Handler is to handle Exceptions of a vast variety of exceptions: global exception handling. You would not know where the exception was thrown. Therefor it makes no sense to add additional logic to your Exception handler on the Handler side.

rather than adding a backpack to the handler you should take the action on the point of existing. that would take a small archituetur change and handle there:

if (NOT_FOUND) {
    String param2 = "param2";
    doomething(param2);
    throw new ResourceNotFoundException("Recource not found");
}

clean code - separation of concerns

  • if it was a general aspect of exception handling, you would have the general informatiaon already in your hands

  • if it is a specific aspect, then it should be handled where it happens!

Upvotes: 0

JArgente
JArgente

Reputation: 2297

Yes, as Abra said, you can create your own exception that inherits from ResourceNotFoundException, and add the other parameter there. Then in the exception handler you can get it from the exception:

This could be the class

public class CustomException extends ResourceNotFoundException {
  private String otherParam;

  public CustomException(String message, String otherParam) {
     super(message);
     this.otherParam = otherParam
  {
   public getOtherParam() {
     return otherParam;
   }

}

Then you throw it

throw new CustomException("Recource not found", param2);

Then in exception handler you can get the second param

@ExceptionHandler(CustomException.class)
public ResponseEntity<Problem> handleResourceNotFound(CustomException ex) {
  doomething(ex.getOtherParam());
  return new ResponseEntity(ex.getMessage());
}

Upvotes: 3

Related Questions