Handel raw error in rest response

:bullseye: What is your goal?

Handle 4xx and 5xx errors in my custom app.
In case of 2xx the result should be parsed e.g. documentId from response body.id.

:thinking: What is the problem?

In case of 2xx the API returns application/json and that works fine.
But in case of an error (4xx/5xx) the return is just text/plain, and in this case i get an error.

e.g. REST Body (Status 403): “No entity found by given predicate”
error message: The operation failed with an error. Body is not a valid JSON. Unexpected token ‘N’, "No entity "… is not valid JSON

It should only try to parse JSON in case of 2xx

:test_tube: What have you tried so far?

I tried to add handling like this, but then i get an error, that the response could not be parsed as a json.

{
  "url": "{{connection.baseUrl}}/documents/{{parameters.documentId}}",
  "headers": {
    "Authorization": "Bearer {{parameters.instanceToken}}"
  },
  "type": "json",
  "method": "DELETE",
  "response": {
    "error": {
      "message": "[{{statusCode}}] {{error.output}}"
    },
    "output": {
      "documentId": "{{body.id}}",
      "headers": "{{headers}}",
      "statusCode": "{{statusCode}}"
    }
  }
}

Hello @Jan_Rorthmans,

Have you tried splitting up the error directive like in this specification? Communication | Custom Apps Documentation | Make Developer Hub

  "response": {
    "type": {
      "*": "json",
      "200": "text"
    }
  }
}

You can set the response.type per HTTP status code, but you can also set up an error directive that handles the output differently per HTTP status code. But you should test this.

Setting the type as json at root is unnecessary, as this is is default.

Cheers,
Henk

3 Likes

Perfect that works for me.

It looks like this:

  "response": {
    "type": {
      "*": "text",
      "200": "json"
    },
    "error": {
      "message": "[{{statusCode}}] {{body}}"
    },
    "output": {
      "documentId": "{{body.id}}",
      "headers": "{{headers}}",
      "statusCode": "{{statusCode}}"
    }
  }
1 Like