2017-09-02 2 views
0

Meine Service-Methode:RxJava- globale Fehler im Frühjahr Boot Handhabung

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()); 
      }); 
} 

Globale Exception-Handler:

@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; 
} 

}

Das Problem ist, dass selbst die, wenn die Bedingung erfüllt ist, und vielleicht .error (...) wird getroffen, der Fehler wird nicht von meinem globalen Hanlder behandelt. Ich frage mich, was ich vermisst habe oder warum es passiert ist? Ich weiß nicht, ob es von Handler oder von rxJava verursacht wird. Vielleicht habe ich etwas Wichtiges verpasst.

Antwort

0

Maybe.error(new AuthenticationException()); - dies erzeugt nichts anderes als ein neues Maybe Objekt, das dann verworfen wird. Sie müssen es zurückgeben, damit der Fehler weitergegeben wird.