Hello and welcome to this week's OutSystems Advocacy Team video. My name is
Jay Santos and today we will be talking about how to handle HTTP status codes in
rest services today we'll be using the rest HTTP code component available on
the forge this component was developed by Rui Barbosa from OutSystems and
it's a great way to get started on how to handle http codes on res services so
thank you very much way before we jump in to service studio let's have a look
at the page that the forge component offers us to show how we're handling
HTTP errors on our service so first of all we're here in the top right I have
the exception behavior the way I want my page to behave in case of an exception
so I have default handle and silent let's have a look first at how default
behaves so if I have a request that returns a 200 code on the response I
have a successful type of responsible without the details populated from the
response itself so nowhere ever happened let's say I have a 401 error code so if
I click on send requests by default I'm just using a standard out systems error
message same goes for Faro three or five or 404 and whatever now if I switch to
handle and just close the window here if I switch to handle if the rest service
returns a 401 error message now I am handling the error and formatting it and
showing showing it here in this window instead of having the default error
message of course I could do whatever I wanted with this information just show
you that I'm treating it and formatting it same thing down here if I say send an
XYZ request I have the exception calls because I couldn't parse XYZ to an
integer right and same thing here on this exception I had
this request for ABC I have the exact same message if I switch XYZ to say four
one eight I get that this response type is a client error and the start is four
one eight now let's jump into service studio and see how this is working so
over here you have the page that we are checking out and the list of requests
with which with each one of the HTTP errors our web blocks that I have added
to my page and over here on the right I have the response the response content
which I update with the error information now on the rest service I
have a known after response callback which is of course called once I have
response from the service but Before we jump into that let's get back to our web
block or request web block and depending on the button that you are using to send
requests or the handle exception button we call two different screen actions so
for the send request we call the go exception screen action and for a handle
exception we have to go trap exception screen action so if we check the go
exception what I do here is I call the rest service and then I trigger the
response event now the response event is set to call the event request response
screen action on our page basically what these actions do is simply get the
response from the rest service set it to a variable and refresh the response part
of the page which is this right side here and that's basically it and of
course on the go exception I have a standard general exception handler that
shows us the standard error messages that would have seen when we were
demoing the page now the trap exception which is called once again when we have
the handle exception button meaning when we are handling the exceptions is very
similar to the dough exception but it also have a customized exception handler
called rest what this exception handler do is the serial is to deserialize the
JSON wiki response and populate the error message with the exception card
line break and the error information and then once again called the response
event which is going to populate the which is going to populate the right
side of our page so basically this is let's say this is where the magic is
happening right but how do we trigger this exception how do we trigger the
rest exception what we do is now we jump into the logic tab and we go to our
after response call back from our rest service so if I double click on this so
what I have here is my response the response for the rest service the first
thing I check is if I have an error so did I return an error and to do that I
simply check if the status code of my response is above 399 so of course any
HTTP error from 400 onwards our error messages so if I have an error I then
check if I'm handling these exceptions if my if my session variable called
behavior is set to deal with exception handling if it is then I finally trigger
the rest exception and this is how I would do HTTP error codes on rest
services on our case here we also have a silent handling which I haven't shown
but we can jump here real quick and I can show you so if I select silent and I
get a 401 message I don't get the standard error message
but I am handling it and certainly the information on the response page as well
because what I'm doing is I am manually setting the status code of my response
to 200 meaning I am not triggering an error message what I'm simply doing this
is before sending the response the rest service response to my page I'm setting
the error code to 200 so I am faking correct a correct return and once again
when I get back to my request response I get the response information and
populate the right side but essentially once again if I want to handle an HTTP
error code with a rest service I check if I have an error and only to do is
trigger the rest exception which I once again handling on the on the screen
action attached to the button so that's it for today's video as always I hope it
was useful and interesting my name is jay santos my email is jade
centers are out systems comm i'm on twitter @jrfromptc