Stop And Error: Can't parse Error object

Describe the problem/error/question

I’m using the Stop And Error node. Here’s the json that get inputed to the node:

    "refresh_token": "invalid_token",
    "error": {
      "message": "401 - "{\"summary\":\"Invalid token\",\"detail\":\"Token is invalid or expired\",\"status_code\":401}"",
      "name": "AxiosError",
      "stack": "AxiosError: Request failed with status code 401 at settle (/usr/local/lib/node_modules/n8n/node_modules/axios/lib/core/settle.js:19:12) at RedirectableRequest.handleResponse (/usr/local/lib/node_modules/n8n/node_modules/axios/lib/adapters/http.js:537:9) at RedirectableRequest.emit (node:events:529:35) at RedirectableRequest.emit (node:domain:489:12) at RedirectableRequest._processResponse (/usr/local/lib/node_modules/n8n/node_modules/follow-redirects/index.js:398:10) at ClientRequest.RedirectableRequest._onNativeResponse (/usr/local/lib/node_modules/n8n/node_modules/follow-redirects/index.js:91:12) at Object.onceWrapper (node:events:632:26) at ClientRequest.emit (node:events:517:28) at ClientRequest.emit (node:domain:489:12) at HTTPParser.parserOnIncomingClient (node:_http_client:700:27) at Axios.request (/usr/local/lib/node_modules/n8n/node_modules/axios/lib/core/Axios.js:45:41) at processTicksAndRejections (node:internal/process/task_queues:95:5) at requestFn (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/NodeExecuteFunctions.js:565:33) at proxyRequestToAxios (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/NodeExecuteFunctions.js:568:26) at Object.request (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/NodeExecuteFunctions.js:1957:50)",
      "code": "ERR_BAD_REQUEST",
      "status": 401

What is the error message (if any)?

Unexpected token A in JSON at position 0

Please share your workflow

Information on your n8n setup

  • n8n version: 1.34.1 (but tried with previous versions with the same error)
  • Database (default: SQLite): sqlite
  • n8n EXECUTIONS_PROCESS setting (default: own, main): own
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: NixOS

@sweenu , $json.error is not a valid JSON because $json.error.message contains unescaped quote character inside quotes. Hence the error you are getting.

What are you trying to achieve? To get the error message you would have to use RegEx to extract it and then convert it to JSON. There are quite a few example how to do it in this forum.

Here’s the json response that the previous node receives when I do the same exact request with curl.
As you can see, the json is totally valid. Therefore there is a problem with what n8n does. Why does it take valid json and outputs invalid json?

$ curl -X GET "" \
   -H  "accept: application/json" \
   -H  "Authorization: Bearer bad_token"
{"summary":"Invalid token","detail":"Token is invalid or expired","status_code":401}

Yes, it seems JSON doesn’t work there

Maybe it should have some specific format, but I can’t find anything in the docs.

@sweenu , you provided an example of curl but you probably use HTTP Request in n8n workflow. Unfortunately, that is how this node outputs any response if the status code is other than 2xx. In your case it is 401.

Again, you would have to treat the output yourself with RegEx and conversion to JSON if you want to reuse it after HTTP Request node.

I’m confused. The HTTP Request Node does output json here. Just malformed and therefore invalid json but still json.
Clearly there is a problem in how "message" is generated. It seems like, in pseudo-code, it’s something like: message = String(status) + '"' + String(body) + '"' and those quotes are problematic.

It seems to me that I should open an issue. message should just be the json from the body, it’d be way more usable this way.

@sweenu , yes, you are right. I would also prefer to see a different output to avoid string manipulation trying to extract the actual message returned with the status code other than 2xx. As far as I know, there is a reason why HTTP Request node behaves this way, which is different from other nodes when an error occurs. However, I am not familiar with the details.