37
Views
4
Comments
Solved
How to see the result of a 3rd party REST API call error?
Question
Platform Version
11.18.0 (Build 37468)

I have an Outsystems app that makes an HTTP POST request (REST call) to a third party service. That service returns a 422 error, along with an HTTP body that contains more information about the error.

Outsystems does not seem to log the body of the response anywhere. Instead, all I can access is the HTTP status of the error, which isn't enough in that case.

The context that I can't access, but is very important, is the one documented below, the body of the response when such error happens. How to see the body of the HTTP response?

2021-10-09 07-57-44
Stefan Weber
 
MVP
Solution

Hi, 

a good pattern is to check the status code in the OnAfterResponse handler. If the Status Code is greater or equal than 400 then you deserialize the ResponseText of your error to a structure and also assign the status code to a structure attribute. Then you serialize the whole structure to a string and raise an exception (eg. ApiException) with the serialized error as exception message.

In your REST wrapping server action you add two additional output attributes

* IsSuccess - Boolean return true if the REST call was successful, otherwise false

* Error - structure returning StatusCode, StatusCode, Message and any other attribute you need from the error response of the API

In the regular flow you just return IsSuccess true. In the same server action handle the ApiException. Deserialize the error message to an Error structure and set IsSuccess to False and the Error output to the deserialized error message.

You can download one of my connectors like this one https://www.outsystems.com/forge/component-overview/13106/microsoft-graph-to-do-tasks to see how this is all done.

Best

Stefan


2021-12-30 19-28-37
Alain Chautard

Thanks, that did the trick.

2021-01-19 14-07-32
Tom Zhao
Solution

Hi

Addition to @Stefan Weber 's answer,  You can get the response body on the OnAfterResponse event.

Please also refer to the link bellow.

https://success.outsystems.com/ja-jp/Documentation/11/Extensibility_and_Integration/REST/Consume_REST_APIs/Handling_REST_Errors

Regards,

2021-10-09 07-57-44
Stefan Weber
 
MVP
Solution

Hi, 

a good pattern is to check the status code in the OnAfterResponse handler. If the Status Code is greater or equal than 400 then you deserialize the ResponseText of your error to a structure and also assign the status code to a structure attribute. Then you serialize the whole structure to a string and raise an exception (eg. ApiException) with the serialized error as exception message.

In your REST wrapping server action you add two additional output attributes

* IsSuccess - Boolean return true if the REST call was successful, otherwise false

* Error - structure returning StatusCode, StatusCode, Message and any other attribute you need from the error response of the API

In the regular flow you just return IsSuccess true. In the same server action handle the ApiException. Deserialize the error message to an Error structure and set IsSuccess to False and the Error output to the deserialized error message.

You can download one of my connectors like this one https://www.outsystems.com/forge/component-overview/13106/microsoft-graph-to-do-tasks to see how this is all done.

Best

Stefan


2021-12-30 19-28-37
Alain Chautard

Thanks, that did the trick.

2021-01-19 14-07-32
Tom Zhao
Solution

Hi

Addition to @Stefan Weber 's answer,  You can get the response body on the OnAfterResponse event.

Please also refer to the link bellow.

https://success.outsystems.com/ja-jp/Documentation/11/Extensibility_and_Integration/REST/Consume_REST_APIs/Handling_REST_Errors

Regards,

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

What I typically do in cases like this is add the error structure to the success output (assuming that's a JSON object too, not a list), and set the status code in the OnAfterResponse to 200. Thus you can check whether there's an error in your normal code flow, instead of catching exceptions.

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