Declarative custom node - Reponse body

Hi,

I’ve built a custom declarative node, I am trying to find a way to catch the response body when I have errors.

I’ve tried a few things and a lot of searches without much success. If it’s possible, could you maybe point me to an example that does it well ?

Here is an example of response:

I catch the response code but not the response body.

Thanks!
Alex

That is possible with a postReceive. Here is an example not how to do exactly what you want but where a node as it configured:

1 Like

Hi,

Thanks a lot for the prompt answer, that gave me the clue to go look into postReceive.

I’ve done it a bit differently, where I define postReceive: [sendErrorPostReceive]

I’ve done it like that:

export async function sendErrorPostReceive(
	this: IExecuteSingleFunctions,
	data: INodeExecutionData[],
	response: IN8nHttpFullResponse,
): Promise<INodeExecutionData[]> {
	if(response.statusCode === 200)
	{return data;}
	else
	{throw new NodeApiError(
			this.getNode(),
			{},
			{
				message: 'Sending failed',
				description:
					'test',
				httpCode: '500',
			},
		);
	}
}

If it’s all fine, I return data, and that works fine, if not, I throw a NodeApiError. That seemed to work just fine, and when I test the NodeApiError (by ignoring the if), it works.

However, when I run my node and do an error on purpose, I still get the same answer as before (as in my initial message), as if the “postReceive” wasn’t even called. Is that possible ? Should I change a parameter to force it’s execution ?

Here’s the setup:

		{	name: 'GET - Find Item',
			value: 'finditem',
			action: 'Perform a GET request',
			routing: {
				request: {
					method: 'GET',
					url: '=/item/{{$parameter.id}}',
					returnFullResponse: true,
				},
				output: {
					postReceive: [sendErrorPostReceive],
				},
			},
		}

Thanks in advance !

Best,
Alex

So in the end I found the solution.

I just added the “ignoreHttpStatusErrors: true” to the request, so that n8n doesn’t throw node errors itself, and now I handle them in post receive:

				request: {
					method: 'GET',
					url: '=url: '=/item/{{$parameter.id}}',
					returnFullResponse: true,
					ignoreHttpStatusErrors: true,
				},

Thanks again for the clue on postReceive!
Alex

4 Likes

Great to hear that you found the solution and thanks for sharing!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.