RxJava- Spring启动时的全局错误处理

时间:2017-09-02 11:50:39

标签: spring rx-java2

我的服务方式:

public Single<AuthenticationResponse> askForToken(UserRequest user){
    return Maybe.fromCallable(()->
            Unirest.post("https://lawyers-supervisor.eu.auth0.com/oauth/token")
                    .header("content-type", "application/json")
                    .body(String.format("{\"grant_type\": \"password\","+
                                    "\"username\": \"%s\","+
                                    "\"password\": \"%s\","+
                                    "\"audience\": \"%s\","+
                                    "\"scope\": \"offline_access\","+
                                    "\"client_id\":\"%s\","+
                                    "\"client_secret\": \"%s\" }",
                            user.getEmail(),
                            user.getPassword(),
                            audience,
                            clientId,
                            clientSecret))
                    .asJson())
            .flatMapSingle(httpResponse->{
                UserEntity userEntity=userService.getByEmail(user.getEmail()).blockingGet();
                if(Objects.isNull(userEntity))
                    Maybe.error(new UserNotExistsException());
                if(httpResponse.getStatus() != HttpStatus.SC_OK)
                    Maybe.error(new AuthenticationException());
                return Mapper.mapHttpResponseToAuthenticationResponse(httpResponse,user.getEmail(),userEntity.getUserId(),userEntity.getRole());
            });
}

全局异常处理程序:

@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler implements ErrorController {

@ExceptionHandler(UserDuplicateEmailException.class)
public ResponseEntity<Object> handleDuplicatedEmailEexception(UserDuplicateEmailException ex, WebRequest request) {
    HttpStatus status = HttpStatus.CONFLICT;
    return handleExceptionInternal(ex, new ErrorTemplate(status, status.value(),
                    "User with email :" + ex.getEmail() + " already exists in database",
                    ex.getMessage(),
                    new Timestamp(System.currentTimeMillis())),
            new HttpHeaders(), status, request);
}

@ExceptionHandler(AuthenticationException.class)
public ResponseEntity<Object> handleAuthenticationException(AuthenticationException ex, WebRequest request) {
    HttpStatus status = HttpStatus.UNAUTHORIZED;
    return handleExceptionInternal(ex, new ErrorTemplate(status, status.value(),
                    "Bad password or email",
                    null,
                    new Timestamp(System.currentTimeMillis())),
            new HttpHeaders(), status, request);
}

@ExceptionHandler(UserNotExistsException.class)
public ResponseEntity<Object> userNotExistsException(AuthenticationException ex, WebRequest request) {
    HttpStatus status = HttpStatus.UNAUTHORIZED;
    return handleExceptionInternal(ex, new ErrorTemplate(status, status.value(),
                    "User doesn't exists",
                    null,
                    new Timestamp(System.currentTimeMillis())),
            new HttpHeaders(), status, request);
}
@Override
public String getErrorPath() {
    return "error";
}

@Override
public final ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    return null;
}

@Override
protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders
        headers, HttpStatus status, WebRequest request) {
    return null;
}

@Override
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    return null;
}

}

问题是即使if条件已满,而且遇到了Maybe.error(...),我的全局hanlder也不会处理错误。我想知道我错过了什么或为什么会这样?我不知道它是由处理程序还是由rxJava引起的。也许我错过了一些重要的事情。

1 个答案:

答案 0 :(得分:-1)

Maybe.error(new AuthenticationException()); - 这只会创建一个新的Maybe对象,然后将其丢弃。您需要将其返回以传播错误。