www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Best way for handle missing args in REST interface in vibed

reply Suliman <evermind live.ru> writes:
I am doing REST interface with vibed. And thinking about handling 
errors, if users forgot to pass all expected args in function.

For example:
foo(int x, int y) // get request
{

}

/api/foo?x=111

And if user is forgot to pass `y` we will get error in the 
browser. What is the right way to handle curch cases?

Wrap is with try-catch looks wrong.
May 29
next sibling parent Suliman <evermind live.ru> writes:
On Monday, 29 May 2017 at 12:23:59 UTC, Suliman wrote:
 I am doing REST interface with vibed. And thinking about 
 handling errors, if users forgot to pass all expected args in 
 function.

 For example:
 foo(int x, int y) // get request
 {

 }

 /api/foo?x=111

 And if user is forgot to pass `y` we will get error in the 
 browser. What is the right way to handle curch cases?

 Wrap is with try-catch looks wrong.
It's seems I found how to do it http://vibed.org/api/vibe.web.web/errorDisplay
May 29
prev sibling parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Monday, 29 May 2017 at 12:23:59 UTC, Suliman wrote:
 I am doing REST interface with vibed. And thinking about 
 handling errors, if users forgot to pass all expected args in 
 function.

 For example:
 foo(int x, int y) // get request
 {

 }

 /api/foo?x=111

 And if user is forgot to pass `y` we will get error in the 
 browser. What is the right way to handle curch cases?

 Wrap is with try-catch looks wrong.
That depends on wether or not there is a sensible default for y, if so foo(int x, int y=0) ought to work.
May 29
parent reply Suliman <evermind live.ru> writes:
I wrote next code:

         void foo(string _error = null)
         {
             writeln("Error");
         }
     override:
          errorDisplay!foo
         Json doTrackSpeedAnalyze(int trackid, string 
startDateTime, string endDateTime) // 
/api/mytrack?trackid=123&startDateTime=2000&endDateTime=2010
         {
             if(endDateTime.length == 0)
             {
                 throw new Exception("End Date must not be empty");
             }
             return Json.emptyObject;
         }

If I access to url:  /api/mytrack?trackid=123&startDateTime=2000 
(witout endDateTime) I am expecting execution foo() block, but it 
does not happens. Why?

And what do Exception here? Why should handle it?
May 29
parent reply Suliman <evermind live.ru> writes:
 rootPathFromName
interface API
{
      path("mytrack")            method(HTTPMethod.GET)    Json 
doTrackSpeedAnalyze(int trackid, string startDateTime, string 
endDateTime);
}

class MyRouter : API
{
    Config config;
    Database database;
    this(Config config, Database database)
    {
     this.config = config;
     this.database = database;
    }

          path("/")
         void foo(string _error = null) // I expect that this will 
be called if exception happen in doTrackSpeedAnalyze
         {
             writeln("Error in console"); // does not writing on 
console
         }
      override:
          errorDisplay!foo
         Json doTrackSpeedAnalyze(int trackid, string 
startDateTime, string endDateTime) // 
/api/mytrack?trackid=123&startDateTime=2000&endDateTime=2010
         {
             if(endDateTime == "0")
             {
                 throw new Exception("Some fields are empty");
             }
             return Json.emptyObject;
         }

}


What I am doing wrong? Should I do  override for foo? Should foo 
present in `interface`?
May 29
parent Suliman <evermind live.ru> writes:
I had post question here 
http://forum.rejectedsoftware.com/groups/rejectedsoftware.vibed/thread/43511/
May 30