Expose API error handling not resulting in expected response
Question

Hi Community,

I am inexperienced in building REST APIs and have therefore made an effort to deal well with error handling. A quick look at the expected response can be found in the screenshot below (this is from the actual REST API i did built with OutSystems). The example response in the documentation is the actual response I'd like to see in the response (ofcourse filled with an actual error code and message).

I am using SoapUI to test the REST API and the response I get can be seen in the screenshot below. This is different then what the example shows me in the documentation and I am not sure why this is different and how to solve this. 

I've added a simplified .oml (and documentation of the rest api can be found here) so you can see what I am trying to do. Hopefully anyone can guide on how to go on from this and solve this issue.

If more information is needed please let me know.

Thanks, 

Bart


RESTexample.oml

mvp_badge
MVP
Solution

Hi Bart,

When an unhandled exception occurs in an exposed REST services method, the Platform produces a default REST error output, with an "Errors" attribute that holds the exception message, and a "StatusCode" attribute that holds the HTTP status code (default 500, unless you set it to something else). There's no way around it, unless you handle the exception in the REST method. You did this, but you end your handler by throwing another Exception! Also, you are JSON serializing the output, I'm not sure why you though you needed to do that. The simple solution is to remove everything after the ListAppend, and end with an End:

This should give you the result you want.

Hi Kilian,

Thanks for the explanation. I did follow your approach and it is working as expected. 

I did read the documentation about exposing a REST API and I did try to find some examples. One of the examples I did find did something way more complex than this simple one and there they we're doing a JSON serialize in the exception handler and that's why I thought this was necessary. I seem to have misinterpreted that.

Best,

Bart

mvp_badge
MVP

I can't think of a good use case to use the JSON Serialize - as long as you've set up your output structures correctly it should work just by assigning them. But I'm glad it works now :).

Hi Bart,

You are throwing exception in your otherwise branch that's why you are getting different output.

If you remove Raise exception from otherwise branch and set the output result as per below screen then you will get result as per your expectation.



Thanks

Vinod


mvp_badge
MVP

Hi Vinod,

Though your answer solves Bart's immediate problem, it doesn't help him understanding what he did wrong. Throwing an exception is a perfectly valid way of handling errors (what if he wanted to throw five exceptions, would you recommend him to duplicate that code five times??), but he needs to fix his exception handler (see my post below).

Hi Vinod,

Thanks for your reply. I think this would solve my problem but I think the way Kilian explained is a better one in this case.

Best,

Bart

UPDATE:

I just found that the Success attribute on my structure was not mandatory. It has to be mandatory since a success status always have to given back. Still not sure on why the success was not shown in my response since I did set the success to false in the exception handler. 

Still not sure on why the output is different.

It should be something similar to this:

{
  "Success": false,
  "ErrorCode": [
    {
      "Code": "",
      "Message": ""
    }
  ]
}

Hi Bart,

You are throwing exception in your otherwise branch that's why you are getting different output.

If you remove Raise exception from otherwise branch and set the output result as per below screen then you will get result as per your expectation.



Thanks

Vinod


mvp_badge
MVP

Hi Vinod,

Though your answer solves Bart's immediate problem, it doesn't help him understanding what he did wrong. Throwing an exception is a perfectly valid way of handling errors (what if he wanted to throw five exceptions, would you recommend him to duplicate that code five times??), but he needs to fix his exception handler (see my post below).

Hi Vinod,

Thanks for your reply. I think this would solve my problem but I think the way Kilian explained is a better one in this case.

Best,

Bart

mvp_badge
MVP
Solution

Hi Bart,

When an unhandled exception occurs in an exposed REST services method, the Platform produces a default REST error output, with an "Errors" attribute that holds the exception message, and a "StatusCode" attribute that holds the HTTP status code (default 500, unless you set it to something else). There's no way around it, unless you handle the exception in the REST method. You did this, but you end your handler by throwing another Exception! Also, you are JSON serializing the output, I'm not sure why you though you needed to do that. The simple solution is to remove everything after the ListAppend, and end with an End:

This should give you the result you want.

Hi Kilian,

Thanks for the explanation. I did follow your approach and it is working as expected. 

I did read the documentation about exposing a REST API and I did try to find some examples. One of the examples I did find did something way more complex than this simple one and there they we're doing a JSON serialize in the exception handler and that's why I thought this was necessary. I seem to have misinterpreted that.

Best,

Bart

mvp_badge
MVP

I can't think of a good use case to use the JSON Serialize - as long as you've set up your output structures correctly it should work just by assigning them. But I'm glad it works now :).

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.