0

I have multiple controllers whose exceptions are handled in the ControllerAdvice. All controllers use common exceptions types (like HttpClientException, DBException, etc.). But there is one specific controller which exceptions should be handled differently.

In my current implementation all methods of this specific controller are wrapped with try-catch and throw CustomException in case of any exception. Then, in ControllerAdvice I process this type of exception.

However, I want to handle all exceptions in ControllerAdvice as well as get rid of CustomException and try-catch in controller methods.

Is there any way to find out the source controller name in the exception advice? I could check it and handle the exception differently. Or maybe some other solution exist?

iJoker0998
  • 21
  • 3

1 Answers1

0

Inside of your controller advice, you can provide Handler for your custom exception as below.

@ControllerAdvice
public class CustomGlobalExceptionHandler {
    @ExceptionHandler(CustomException.class)
    public final ResponseEntity<ApiResponseDTO> manageException(CustomException ex) {
        log.error("Error in CustomException: {}", ex.getMessage(), ex);
        ApiResponseDTO error = ApiResponseDTO.builder()
                .message(ex.getMessage())
                .result(0)
                .build();
        return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
    }

    @ExceptionHandler(CustomException1.class)
    public final ResponseEntity<ApiResponseDTO> manageException1(CustomException1 ex) {
        log.error("Error in CustomException1: {}", ex.getMessage(), ex);
        ApiResponseDTO error = ApiResponseDTO.builder()
                .message(ex.getMessage())
                .result(0)
                .build();
        return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
    }

    @ExceptionHandler(Exception.class)
    public final ResponseEntity<ApiResponseDTO> manageException1(Exception ex) {
        log.error("Error in Common Exception Handler: {}", ex.getMessage(), ex);
        StackTraceElement[] ste = ex.getStackTrace();
        String className=ste[ste.length - 1].getClassName();
        System.out.println(className);
        if(className.equalsIgnoreCase("com.a")){
            System.out.println("Do A related stuff");
        }else{
            System.out.println("Do B related stuff");
        }
        ApiResponseDTO error = ApiResponseDTO.builder()
                .message(ex.getMessage())
                .result(0)
                .build();
        return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
    }

}

As mentioned in last block, you can get class name from where this exception thrown and utilizing that name to branching out your stuff.

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
Sagar Gangwal
  • 7,544
  • 3
  • 24
  • 38
  • Thanks for your reply! But what if nullPointer exception or another one? I still need to handle it differently – iJoker0998 Aug 20 '20 at 08:53
  • @iJoker0998 , For any other exception you can provide default exception handler as i mentioned in my answer `@ExceptionHandler(Exception.class)` – Sagar Gangwal Aug 20 '20 at 08:56
  • Its right. But i need `@ExceptionHandler(Exception.class)` with several realisations. All controllers needs one realisation, but one specific controller needs different realisation. I can't create two `@ExceptionHandler(Exception.class)` so i want to find out the source name of controller in the `@ControllerAdvice` or some another solution. – iJoker0998 Aug 20 '20 at 09:03
  • I need another realisation for all of `@ControllerAdvice` methods (to set differ fields to response object) but it is very large code to put it in controller – iJoker0998 Aug 20 '20 at 09:08
  • As mentioned in last block, you can get class name from where this exception thrown and utilizing that name to branching out your stuff. – Sagar Gangwal Aug 20 '20 at 09:20
  • 1
    It works! But it solution does not suit me. I'll use try-catch in controller. Thank you! – iJoker0998 Aug 20 '20 at 10:28
  • You can not put try catch on all hundred method call, it's become tedious day by day as your code length will increase. Try to do with this approach and implement `custom utility` classes. That will help you. – Sagar Gangwal Aug 20 '20 at 10:32