mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 01:56:46 +00:00
fix(OpenAi Node): optional chaining for error handling in router (#17412)
This commit is contained in:
42
packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/actions/router.test.ts
vendored
Normal file
42
packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/actions/router.test.ts
vendored
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user