cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

How to access error body of a custom SDK action?

jason1
Deputy Chef I
Deputy Chef I

When an SDK action fails (e.g. non-200 response code), it's unclear how to access the JSON body of the response to then turn this into a meaningful error message. The "Error Message" pill available through the On Error block does not contain enough information.


I know for HTTP connectors, you can choose to mark non-200 codes as success and parse the response data in subsequent steps. How can this be achieved easily for custom SDK actions?

3 REPLIES 3

mroldanvega
Executive Chef I
Executive Chef I

Hi,

I use .after_response do |code,body,headers| block in my http request to handle this. Here's an example of how I get the http code and then use it in the data pills.


chandra
Workato employee
Workato employee

Hi Jason,


Do you want to show the error message received from the target

application instead of suppressing the error (sometimes Workato

show an Internal Server error for the failed job)?


we have a method after_error_response which helps to monitor for

all http codes(other than 2XX).



For example, the below method monitors for the 404 error code

and displays the error message received from the target application.



after_error_response(404) do |code, body, header, message|

error("#{message}: #{body}")

end


the method has 4 args:

code: Error code e.g. 404, 403

body: the response body

headers: header details(response headers)

message: error message



Also, you can use regex to monitor multiple error codes with the same method.

for example, after_error_response(/.*/) monitors all error codes(other

than success status codes 2xx).


when we receive an API error occurs the code inside the method

i.e. error("#{message}: #{body}") will be executed message is displayed on the

job report.


We recommend using the error method to show the error message on the job,

this method marks the job as a failed transaction on the recipe and

shows the error under the job report.



Otherwise, If you want to use the error message in subsequent steps,

then you should able to do that as well.


For example:

after_error_response(/.*/) do |code, body, header, message|

{

code: code,

body: parse_josn(body),

headers: headers,

message: message

}

end


With this approach, the job is marked as success and but you can use error in subsequent steps.

(this works the same as, in HTTP connectors we can choose to mark non-200 codes as success)


You should able to use the error message in the downstream application, and then stop the job with the error after logging. with this, you know which job is failed on the job report.


But we suggest marking the job as failed when there is an error on API call.



Please find the documentation for more details:

https://docs.workato.com/developing-connectors/sdk/guides/error-handling.html#after-error-response

jason1
Deputy Chef I
Deputy Chef I

Thank you Manuel Roldan-Vega and Chandra V ! Great examples and I believe this is exactly what I needed.