0

I am developing a project using Spring Boot. I have created a general error handler for error management. However, despite throwing my BusinessException, the @ExceptionHandler is catching the Exception instead of the intended one. Where could I be making a mistake?

RegisterController

public Boolean register(RegisterRequest request)
    {
        Optional<User> user = userDao.findByUsername(request.getUsername());
        if (user.isPresent())
        {
            throw new BusinessException(BusinessExceptionReason.USER_ALREADY_EXIST);
        }
        userDetailService.createUserDetail(user);
        userDao.save(user);
        return true;
    }

BusinessExceptionReason

@Getter
@AllArgsConstructor
public enum BusinessExceptionReason implements BusinessExceptionPolicy
{

    USER_ALREADY_EXIST("1001","User Already Exist.!", HttpStatus.BAD_REQUEST);

    private final String code;
    private final String message;
    private final HttpStatus httpStatus;

}

GlobalApiExceptionHandler


import static com.settodo.settodoapp.error.util.ErrorResponseUtil.build;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import com.settodo.settodoapp.error.dto.ErrorResponseDTO;

@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class GlobalApiExceptionHandler extends ResponseEntityExceptionHandler
{

    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<Object> handleCustomUncaughtBusinessException(final BusinessException ex,
                                                                        final ServletWebRequest request)
    {
        final ErrorResponseDTO errorResponseDto = build(ex.getCode(), ex.getMessage(), ex.getHttpStatus());
        return ResponseEntity.status(ex.getHttpStatus()).body(errorResponseDto);
    }

    @ExceptionHandler(value = {Exception.class})
    public ResponseEntity<Object> handleUncaughtException(final Exception ex, final ServletWebRequest request)
    {
        final ErrorResponseDTO errorResponseDto = build(Exception.class.getSimpleName(),
                HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),
                HttpStatus.INTERNAL_SERVER_ERROR);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponseDto);
    }

}

BusinessException


import static java.lang.String.format;

import org.springframework.http.HttpStatus;

import com.settodo.settodoapp.error.policy.BusinessExceptionPolicy;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class BusinessException extends RuntimeException implements BusinessExceptionPolicy
{

    protected final String code;
    protected final String message;
    protected final HttpStatus httpStatus;

    public BusinessException(final BusinessExceptionReason reason) {
        this.code = reason.getCode();
        this.message = reason.getMessage();
        this.httpStatus = reason.getHttpStatus();
    }

    public BusinessException(final BusinessExceptionReason reason, final HttpStatus overridingHttpStatus) {
        this.code = reason.getCode();
        this.message = reason.getMessage();
        this.httpStatus = overridingHttpStatus;
    }

    public BusinessException(final BusinessExceptionReason reason, final Object... parameters) {
        if (parameters != null) {
            this.message = format(reason.getMessage(), parameters);
        } else {
            this.message = reason.getMessage();
        }

        this.code = reason.getCode();
        this.httpStatus = reason.getHttpStatus();
    }

    @Override
    public String getLocalizedMessage() {
        return getMessage();
    }

    public String toString() {
        return format("BusinessException(code=%s, message=%s, httpStatus=%s)", this.getCode(), this.getMessage(),
                this.getHttpStatus().value());
    }
}

The @ExceptionHandler is functioning, but it is unable to catch the error as desired.

Abdullah Çiçekli
  • 164
  • 1
  • 2
  • 12

0 Answers0