Skip to content

Commit

Permalink
fix(OpenAI Node): Descriptive errors (#6270)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-radency committed May 18, 2023
1 parent 4219490 commit 8fdfa3b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/nodes-base/nodes/OpenAi/ChatDescription.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { INodeExecutionData, INodeProperties } from 'n8n-workflow';
import { sendErrorPostReceive } from './GenericFunctions';

export const chatOperations: INodeProperties[] = [
{
Expand All @@ -22,6 +23,7 @@ export const chatOperations: INodeProperties[] = [
method: 'POST',
url: '/v1/chat/completions',
},
output: { postReceive: [sendErrorPostReceive] },
},
},
],
Expand Down
18 changes: 18 additions & 0 deletions packages/nodes-base/nodes/OpenAi/GenericFunctions.ts
Original file line number Diff line number Diff line change
@@ -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<INodeExecutionData[]> {
if (String(response.statusCode).startsWith('4') || String(response.statusCode).startsWith('5')) {
throw new NodeApiError(this.getNode(), response as unknown as JsonObject);
}
return data;
}
2 changes: 2 additions & 0 deletions packages/nodes-base/nodes/OpenAi/ImageDescription.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { INodeExecutionData, INodeProperties } from 'n8n-workflow';
import { sendErrorPostReceive } from './GenericFunctions';

export const imageOperations: INodeProperties[] = [
{
Expand All @@ -22,6 +23,7 @@ export const imageOperations: INodeProperties[] = [
method: 'POST',
url: '/v1/images/generations',
},
output: { postReceive: [sendErrorPostReceive] },
},
},
],
Expand Down
1 change: 1 addition & 0 deletions packages/nodes-base/nodes/OpenAi/OpenAi.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class OpenAi implements INodeType {
},
],
requestDefaults: {
ignoreHttpStatusErrors: true,
baseURL: 'https://api.openai.com',
},
properties: [
Expand Down
4 changes: 4 additions & 0 deletions packages/nodes-base/nodes/OpenAi/TextDescription.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { INodeExecutionData, INodeProperties } from 'n8n-workflow';
import { sendErrorPostReceive } from './GenericFunctions';

export const textOperations: INodeProperties[] = [
{
Expand All @@ -22,6 +23,7 @@ export const textOperations: INodeProperties[] = [
method: 'POST',
url: '/v1/completions',
},
output: { postReceive: [sendErrorPostReceive] },
},
},
{
Expand All @@ -34,6 +36,7 @@ export const textOperations: INodeProperties[] = [
method: 'POST',
url: '/v1/edits',
},
output: { postReceive: [sendErrorPostReceive] },
},
},
{
Expand All @@ -46,6 +49,7 @@ export const textOperations: INodeProperties[] = [
method: 'POST',
url: '/v1/moderations',
},
output: { postReceive: [sendErrorPostReceive] },
},
},
],
Expand Down

0 comments on commit 8fdfa3b

Please sign in to comment.