I'm consuming a REST API that's not documented in Outsystems itself. This API returns errors ("400 Bad Request") if an error occurs.

{
    "code":"400",
    "message":"400 Bad Request"
    "responsetext":"{""message"":""Credentials of specified user are not valid."",""code"":""user_credentials_invalid""}"
}

Above is the Json that is returned upon an error. The 400 Error has a few different responses in the response text, by which we can see what the actual cause of the Bad Request is.

My question is, what is the best practice (Or best way) to process this error. I don't want to return "Bad Request" to my user, I want to show my user the specific issue, since there's a difference between "credentials of specified user are not valid" or "user has been temporary locked out".

To do so, I tried going into the OnResponse event, to raise a user exception with the message set to the code in the responsetext. However, what happens, is that this error is raised, but in the function that uses the API call, it still raises the generic "400 Bad Request" error. Can I change this somehow?

Solution

IN the OnAfterResponse  - you can completely change and handle error. You need to make sure to change the response, otherwise you will just continue to pass the response with the 400 error back tot he user.

Solution

Stacey Levine wrote:

IN the OnAfterResponse  - you can completely change and handle error. You need to make sure to change the response, otherwise you will just continue to pass the response with the 400 error back tot he user.

Ah I will try that, if I understand correctly, I need to 'create' a new response that doesn't trigger an error.


Yes. Handle the error however you want, but create a new response that doesn’t have the error state.

Yes, the existing response 400 has to be replaced with a new response.