2016-12-02 7 views
0

Ich bin usng GWTP Rest dispath mit ResourceDelegate und ich möchte alle Ausnahmen in Client-REST-Anforderungen abfangen. Meine REST-Backend Rückkehr:GWT-Plattform mit REST-Versand: Holen HTTP-Statuscode aus Ausnahmehandler

  • 401 oder 403-Statuscodes, wenn keine Genehmigung/verboten
  • 500 für andere Fehler

So habe ich gemeinsam Handler RestDispatchAsyncModule hinzugefügt:

new RestDispatchAsyncModule.Builder().exceptionHandler(MyRestExceptionHandler.class); 

MyRestExceptionHandler.java:

public class MyRestExceptionHandler implements ExceptionHandler { 
    @Override 
    public Status onFailure(Throwable e) { 
     if (e instanceof ActionException){ 
      ActionException a = (ActionException)e; 
      // How to get HTTP status code and response body here? 
     } 
     return null; 
    } 
} 

Ich habe festgestellt, dass alle REST-Ausnahme Instanzen der ActionException-Klasse sind. Wie kann ich HTTP-Statuscode und HTTP-Antwortkörper in MyRestExceptionHandler erhalten?

Antwort

0

Die Lösung besteht darin, RestDispatchHooks anstelle von ExceptionHandler zu verwenden.

AppRestDispatchHooks.java:

public class AppRestDispatchHooks implements RestDispatchHooks { 
    @Override 
    public void onExecute(RestAction<?> action) { 
    } 

    @Override 
    public void onSuccess(RestAction<?> action, Response response, Object result) { 
    } 

    @Override 
    public void onFailure(RestAction<?> action, Response response, Throwable caught) { 
     GWT.log("Status code:"+ response.getStatusCode()); 
    } 
} 

Modul einbauen:

install(new RestDispatchAsyncModule.Builder() 
     .dispatchHooks(AppRestDispatchHooks.class) 
     .build()) 
Verwandte Themen