From 8fdfa3b6b83c6a008d1be778dadcc2172e8a708d Mon Sep 17 00:00:00 2001 From: Michael Kret <88898367+michael-radency@users.noreply.github.com> Date: Thu, 18 May 2023 17:20:56 +0300 Subject: [PATCH] fix(OpenAI Node): Descriptive errors (#6270) --- .../nodes-base/nodes/OpenAi/ChatDescription.ts | 2 ++ .../nodes/OpenAi/GenericFunctions.ts | 18 ++++++++++++++++++ .../nodes/OpenAi/ImageDescription.ts | 2 ++ .../nodes-base/nodes/OpenAi/OpenAi.node.ts | 1 + .../nodes-base/nodes/OpenAi/TextDescription.ts | 4 ++++ 5 files changed, 27 insertions(+) create mode 100644 packages/nodes-base/nodes/OpenAi/GenericFunctions.ts diff --git a/packages/nodes-base/nodes/OpenAi/ChatDescription.ts b/packages/nodes-base/nodes/OpenAi/ChatDescription.ts index fad611fd35..a4748cc420 100644 --- a/packages/nodes-base/nodes/OpenAi/ChatDescription.ts +++ b/packages/nodes-base/nodes/OpenAi/ChatDescription.ts @@ -1,4 +1,5 @@ import type { INodeExecutionData, INodeProperties } from 'n8n-workflow'; +import { sendErrorPostReceive } from './GenericFunctions'; export const chatOperations: INodeProperties[] = [ { @@ -22,6 +23,7 @@ export const chatOperations: INodeProperties[] = [ method: 'POST', url: '/v1/chat/completions', }, + output: { postReceive: [sendErrorPostReceive] }, }, }, ], diff --git a/packages/nodes-base/nodes/OpenAi/GenericFunctions.ts b/packages/nodes-base/nodes/OpenAi/GenericFunctions.ts new file mode 100644 index 0000000000..4ef01acd49 --- /dev/null +++ b/packages/nodes-base/nodes/OpenAi/GenericFunctions.ts @@ -0,0 +1,18 @@ +import type { + IExecuteSingleFunctions, + IN8nHttpFullResponse, + INodeExecutionData, + JsonObject, +} from 'n8n-workflow'; +import { NodeApiError } from 'n8n-workflow'; + +export async function sendErrorPostReceive( + this: IExecuteSingleFunctions, + data: INodeExecutionData[], + response: IN8nHttpFullResponse, +): Promise { + if (String(response.statusCode).startsWith('4') || String(response.statusCode).startsWith('5')) { + throw new NodeApiError(this.getNode(), response as unknown as JsonObject); + } + return data; +} diff --git a/packages/nodes-base/nodes/OpenAi/ImageDescription.ts b/packages/nodes-base/nodes/OpenAi/ImageDescription.ts index 012fc329c0..9e552144ee 100644 --- a/packages/nodes-base/nodes/OpenAi/ImageDescription.ts +++ b/packages/nodes-base/nodes/OpenAi/ImageDescription.ts @@ -1,4 +1,5 @@ import type { INodeExecutionData, INodeProperties } from 'n8n-workflow'; +import { sendErrorPostReceive } from './GenericFunctions'; export const imageOperations: INodeProperties[] = [ { @@ -22,6 +23,7 @@ export const imageOperations: INodeProperties[] = [ method: 'POST', url: '/v1/images/generations', }, + output: { postReceive: [sendErrorPostReceive] }, }, }, ], diff --git a/packages/nodes-base/nodes/OpenAi/OpenAi.node.ts b/packages/nodes-base/nodes/OpenAi/OpenAi.node.ts index 51499ad644..4f7fcc4672 100644 --- a/packages/nodes-base/nodes/OpenAi/OpenAi.node.ts +++ b/packages/nodes-base/nodes/OpenAi/OpenAi.node.ts @@ -24,6 +24,7 @@ export class OpenAi implements INodeType { }, ], requestDefaults: { + ignoreHttpStatusErrors: true, baseURL: 'https://api.openai.com', }, properties: [ diff --git a/packages/nodes-base/nodes/OpenAi/TextDescription.ts b/packages/nodes-base/nodes/OpenAi/TextDescription.ts index 83b42a993f..36a80b1116 100644 --- a/packages/nodes-base/nodes/OpenAi/TextDescription.ts +++ b/packages/nodes-base/nodes/OpenAi/TextDescription.ts @@ -1,4 +1,5 @@ import type { INodeExecutionData, INodeProperties } from 'n8n-workflow'; +import { sendErrorPostReceive } from './GenericFunctions'; export const textOperations: INodeProperties[] = [ { @@ -22,6 +23,7 @@ export const textOperations: INodeProperties[] = [ method: 'POST', url: '/v1/completions', }, + output: { postReceive: [sendErrorPostReceive] }, }, }, { @@ -34,6 +36,7 @@ export const textOperations: INodeProperties[] = [ method: 'POST', url: '/v1/edits', }, + output: { postReceive: [sendErrorPostReceive] }, }, }, { @@ -46,6 +49,7 @@ export const textOperations: INodeProperties[] = [ method: 'POST', url: '/v1/moderations', }, + output: { postReceive: [sendErrorPostReceive] }, }, }, ],