java - Retrofit error handling -



java - Retrofit error handling -

i wrapped retrofit code in class below. if it's not clear code i'm posting it's interacting restful service oauth.

what way error handling? rest server returns error message in json format. deed on message throwing exceptions class. i'm trying below. design? mixing callbacks , exception throwing idea? there improve way?

with approach below i18l messages within custom exceptions , toast them user.

public class restclient implements irestclient { private irestapi api; /** * * @param accesstoken */ public restclient(final string accesstoken) { requestinterceptor requestinterceptor = new requestinterceptor() { @override public void intercept(requestfacade request) { request.addheader("authorization", "bearer " + accesstoken); } }; restadapter restadapter = new restadapter.builder() .setendpoint(config.endpoint) .setrequestinterceptor(requestinterceptor) .build(); api = restadapter.create(irestapi.class); } @override public void requestsomething(final callback callback) { api.getsomething(new callback<something>() { @override public void success(something something, response response) { callback.success(something, response); } @override public void failure(retrofiterror error) { if(error.getmessage().getid().euqals(access_token_expired)) { throw new accesstokenexpired(); } else if(error.getmessage().getid().euqals(user_not_found)) { throw new usernamepasswordnotfound(); } else // else happened... { throw error; } } }); } @override public void deletesomething(final callback callback) { api.deletesomething(new callback<something>() { @override public void success(something something, response response) { callback.success(something, response); } @override public void failure(retrofiterror error) { if(error.getmessage().getid().euqals(something_not_found)) { ... ... different exceptions } ... } }); } }

naturally have create own phone call interface success method.

when build restadapter, can provide error handler maps out custom exceptions, bypasses phone call failure in callback<t> on 4xx/5xx. contrived example:

public class scratch { public static void main(string[] args) { endpoints e = new restadapter.builder() .setendpoint("http://google.com") .setloglevel(restadapter.loglevel.full) .seterrorhandler(new errorhandler() { @override public throwable handleerror(retrofiterror cause) { switch (cause.getresponse().getstatus()) { case 400: /* handle expected body format */ cause.getbody(); throw new runtimeexception("bad request"); default: /* things , stuff */ throw new runtimeexception(""); } } }) .build() .create(endpoints.class); e.getgoogle(new callback<response>() { @override public void success(response response, response response2) { system.out.println("got it"); } @override public void failure(retrofiterror error) { system.err.println("this won't ever seen due error handler."); } }); } private static interface endpoints { @get("/foo/bar") void getgoogle(callback<response> callback); } }

edit: doing this, however, you're potentially sacrificing big reason why you'd want utilize callback interface begin with. if mutual utilize need, may create more sense utilize sync calls , homecoming object type. don't know utilize that's necessary, seems though may more appropriate.

java android error-handling retrofit

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -