rpc - How to throw an exception from a server API code in Dart? -


i'm developing client-server application in dart , have been following tutorial. server code based on it.

in server api code, when goes wrong, want throw exception, example:

void checkeverything() {   if(somethingwrong)     throw new rpcerror(400, "something wrong", "something went wrong!"); }  @apimethod(path: 'myservice/{arg}') future<string> myservice(string arg) async {   checkeverything();   // ...   return myserviceresponse; } 

and exception should processed in main server, e.g.

// ... var apiresponse; try {   var apirequest = new httpapirequest.fromhttprequest(request);   apiresponse = await _apiserver.handlehttpapirequest(apirequest); } catch (error, stack) {   var exception = error error ? new exception(error.tostring()) : error;   if((error rpcerror && error.statuscode==400) {     // code creating http response     apiresponse = new httpapiresponse.error(         httpstatus.bad_request, "something went wrong", exception, stack);   }   else {     // standard error processing dart tutorial     apiresponse = new httpapiresponse.error(         httpstatus.internal_server_error, exception.tostring(),         exception, stack);   } } 

(snippet, see tutorial complete code sans error handling).

however, exception never reaches above catch clause. instead, seems caught in _apiserver.handlehttpapirequest(apirequest);, which, in turns, throws internal_server_error (500):

[warning] rpc: method myservice returned null instead of valid return value [warning] rpc: response   status code: 500   headers:     access-control-allow-credentials: true     access-control-allow-origin: *     cache-control: no-cache, no-store, must-revalidate     content-type: application/json; charset=utf-8     expires: 0     pragma: no-cache   exception:     rpc error status: 500 , message: method non-void return type returned 'null'  unhandled exception: rpc error status: 400 , message: went wrong! #0      myapi.myservice (package:mypackage/server/myapi.dart:204:24) [...] 

this not specific client. i'd communicate error has happened, not return good-looking response. proper way of handling server-side exceptions in dart , passing information client?

ok, think solved problem. throw clause apparently has in api method itself, , not in subordinate method. i.e.:

@apimethod(path: 'myservice/{arg}') future<string> myservice(string arg) async {   if(somethingwrong)     throw new rpcerror(400, "something wrong", "something went wrong!");   // ...   return myserviceresponse; } 

and not:

void checkeverything() {   if(somethingwrong)     throw new rpcerror(400, "something wrong", "something went wrong!"); }  @apimethod(path: 'myservice/{arg}') future<string> myservice(string arg) async {   checkeverything();   // ...   return myserviceresponse; } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -