fix(OpenAi Node): optional chaining for error handling in router (#17412)

This commit is contained in:
Ilia Aphtsiauri
2025-07-21 14:25:58 +04:00
committed by GitHub
parent 4073ce7fb0
commit ba9eacaa6e
2 changed files with 43 additions and 1 deletions

View File

@@ -0,0 +1,42 @@
import { mockDeep } from 'jest-mock-extended';
import type { IExecuteFunctions, INode } from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
import * as audio from './audio';
import { router } from './router';
describe('OpenAI router', () => {
const mockExecuteFunctions = mockDeep<IExecuteFunctions>();
const mockAudio = jest.spyOn(audio.transcribe, 'execute');
beforeEach(() => {
jest.clearAllMocks();
});
it('should handle NodeApiError undefined error chaining', async () => {
const errorNode: INode = {
id: 'error-node-id',
name: 'ErrorNode',
type: 'test.error',
typeVersion: 1,
position: [100, 200],
parameters: {},
};
const nodeApiError = new NodeApiError(
errorNode,
{ message: 'API error occurred', error: { error: { message: 'Rate limit exceeded' } } },
{ itemIndex: 0 },
);
mockExecuteFunctions.getNodeParameter.mockImplementation((parameter) =>
parameter === 'resource' ? 'audio' : 'transcribe',
);
mockExecuteFunctions.getInputData.mockReturnValue([{ json: {} }]);
mockExecuteFunctions.getNode.mockReturnValue(errorNode);
mockExecuteFunctions.continueOnFail.mockReturnValue(false);
mockAudio.mockRejectedValue(nodeApiError);
await expect(router.call(mockExecuteFunctions)).rejects.toThrow(NodeApiError);
});
});

View File

@@ -62,7 +62,7 @@ export async function router(this: IExecuteFunctions) {
if (error instanceof NodeApiError) { if (error instanceof NodeApiError) {
// If the error is a rate limit error, we want to handle it differently // If the error is a rate limit error, we want to handle it differently
const errorCode: string | undefined = (error.cause as any).error?.error?.code; const errorCode: string | undefined = (error.cause as any)?.error?.error?.code;
if (errorCode) { if (errorCode) {
const customErrorMessage = getCustomErrorMessage(errorCode); const customErrorMessage = getCustomErrorMessage(errorCode);
if (customErrorMessage) { if (customErrorMessage) {