Parse error message from HTTPS Response

New to n8n and I’m trying to just get the “errorDetails” section of the below Error message from an HTTPS response. Not sure why it’s not being automatically broken out into it’s own fields like it would in all the other ETL tools I’ve used in the past but would like it split into it’s own fields so I can pull them into an Email and Slack message.

“error”: {
“message”: “404 - “{"type":"RFC 9110: HTTP Semantics Found","status":404,"o:errorDetails":[{"detail":"The record instance does not exist. Provide a valid record instance ID.","o:errorCode":"NONEXISTENT_ID"}]}\n””,

I include this whole message in an email as plain text but if I try to put it in my slack node message then it throws JSON unexpected token error.

Appreciate any help!

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

@Justin_B , I think HTTP Request node expects the response from the server to return HTTP code 2xx. Anything else is considered as a failure. In your example the error code returned is 404 (resource no found). Therefore the workflow fails.

If you want to process the error, you would need to configure HTTP Request node to output the error. Then you would need to parse it with RegEx. The reason why you get “JSON unexpected token error” is because the message itself as produced by the node (due to error-ed state) is not a valid JSON.

To output the error, set up the node as below

Then transform error.message with the expression {{ $json.error.message.match(/\"(.*)\"/)[1] }}

That message string could be used in Slack.

Thank you for that. That worked to clean the message up but is there also a way now to split it out into individual fields so I’d be able to select the detail field only to send in a message?

“error_message”:
“{"type":"RFC 9110: HTTP Semantics Found","status":404,"o:errorDetails":[{"detail":"The record instance does not exist. Provide a valid record instance ID.","o:errorCode":"NONEXISTENT_ID"}]}\n”

Into something like →
“error_message”:
“{ type : RFC 9110: HTTP Semantics,
title : Not Found,
status : 404,
o:errorDetails : [{
detail : The record instance does not exist. Provide a valid record instance ID.,
errorCode : NONEXISTENT_ID}]
}”

Thanks again for your assistance!

@Justin_B , sure, there is. As in your initial example I can see a new line character (\n), we need to remove it first, and then that string could be converted to JSON with the help of expression {{ $json.error.message.match(/"(.*)"/)[1].replace('\\n', '') }} and selecting the value type as Object.

Thanks @ihortom
I tried this but when I switch Value Type to Object it’s throwing this error:
[
{
“error”: “‘error.details’ expects a object but we got 'type”:“URL - removing for post”,“title”:“Not Found”,“status”:404,“o:errorDetails”:[{“detail”:“The record instance does not exist. Provide a valid record instance ID.”,“o:errorCode”:“NONEXISTENT_ID’ [item 0]”,

“pairedItem”: {

“item”: 0

}
}
]

not sure I pasted the correct input the first time because now I’m not seeing that new line \n. This is the input I’m seeing right now.

“error”:
{
“message”:
“404 - {“type”:“https: url-removing for post”,“title”:“Not Found”,“status”:404,“o:errorDetails”:[{“detail”:“The record instance does not exist. Provide a valid record instance ID.”,“o:errorCode”:“NONEXISTENT_ID”}]}”,
“name”:
“Error”
}

@Justin_B , in that case use this expression, {{ $json.error.message.match(/(\{.*\})/)[1] }}

3 Likes

THANK YOU! That did it. Really appreciate your help with this.