Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(OpenAI Node): Descriptive errors #6270

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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