mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
fix(core): Don't swallow connection errors when fetching credentials (#16181)
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import { CredentialsEntity, type CredentialsRepository } from '@n8n/db';
|
||||||
|
import { EntityNotFoundError } from '@n8n/typeorm';
|
||||||
import { mock } from 'jest-mock-extended';
|
import { mock } from 'jest-mock-extended';
|
||||||
import type {
|
import type {
|
||||||
IAuthenticateGeneric,
|
IAuthenticateGeneric,
|
||||||
@@ -12,20 +14,43 @@ import { deepCopy, Workflow } from 'n8n-workflow';
|
|||||||
|
|
||||||
import { CredentialTypes } from '@/credential-types';
|
import { CredentialTypes } from '@/credential-types';
|
||||||
import { CredentialsHelper } from '@/credentials-helper';
|
import { CredentialsHelper } from '@/credentials-helper';
|
||||||
|
import { CredentialNotFoundError } from '@/errors/credential-not-found.error';
|
||||||
import type { LoadNodesAndCredentials } from '@/load-nodes-and-credentials';
|
import type { LoadNodesAndCredentials } from '@/load-nodes-and-credentials';
|
||||||
|
|
||||||
describe('CredentialsHelper', () => {
|
describe('CredentialsHelper', () => {
|
||||||
const nodeTypes = mock<INodeTypes>();
|
const nodeTypes = mock<INodeTypes>();
|
||||||
const mockNodesAndCredentials = mock<LoadNodesAndCredentials>();
|
const mockNodesAndCredentials = mock<LoadNodesAndCredentials>();
|
||||||
|
const credentialsRepository = mock<CredentialsRepository>();
|
||||||
|
|
||||||
const credentialsHelper = new CredentialsHelper(
|
const credentialsHelper = new CredentialsHelper(
|
||||||
new CredentialTypes(mockNodesAndCredentials),
|
new CredentialTypes(mockNodesAndCredentials),
|
||||||
mock(),
|
mock(),
|
||||||
mock(),
|
credentialsRepository,
|
||||||
mock(),
|
mock(),
|
||||||
mock(),
|
mock(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
describe('getCredentials', () => {
|
||||||
|
test('turns `EntityNotFoundError` into `CredentialNotFoundError`s', async () => {
|
||||||
|
credentialsRepository.findOneByOrFail.mockRejectedValueOnce(
|
||||||
|
new EntityNotFoundError(CredentialsEntity, 'foo'),
|
||||||
|
);
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
credentialsHelper.getCredentials({ id: '1', name: 'foo' }, 'bar'),
|
||||||
|
).rejects.toThrow(CredentialNotFoundError);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('passes other error through', async () => {
|
||||||
|
const errorMessage = 'Connection terminated due to connection timeout';
|
||||||
|
credentialsRepository.findOneByOrFail.mockRejectedValueOnce(new Error(errorMessage));
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
credentialsHelper.getCredentials({ id: '1', name: 'foo' }, 'bar'),
|
||||||
|
).rejects.toThrow(errorMessage);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('authenticate', () => {
|
describe('authenticate', () => {
|
||||||
const tests: Array<{
|
const tests: Array<{
|
||||||
description: string;
|
description: string;
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import type { CredentialsEntity, ICredentialsDb } from '@n8n/db';
|
|||||||
import { CredentialsRepository, SharedCredentialsRepository } from '@n8n/db';
|
import { CredentialsRepository, SharedCredentialsRepository } from '@n8n/db';
|
||||||
import { Service } from '@n8n/di';
|
import { Service } from '@n8n/di';
|
||||||
// eslint-disable-next-line n8n-local-rules/misplaced-n8n-typeorm-import
|
// eslint-disable-next-line n8n-local-rules/misplaced-n8n-typeorm-import
|
||||||
import { In } from '@n8n/typeorm';
|
import { EntityNotFoundError, In } from '@n8n/typeorm';
|
||||||
import { Credentials, getAdditionalKeys } from 'n8n-core';
|
import { Credentials, getAdditionalKeys } from 'n8n-core';
|
||||||
import type {
|
import type {
|
||||||
ICredentialDataDecryptedObject,
|
ICredentialDataDecryptedObject,
|
||||||
@@ -257,7 +257,11 @@ export class CredentialsHelper extends ICredentialsHelper {
|
|||||||
type,
|
type,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new CredentialNotFoundError(nodeCredential.id, type);
|
if (error instanceof EntityNotFoundError) {
|
||||||
|
throw new CredentialNotFoundError(nodeCredential.id, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Credentials(
|
return new Credentials(
|
||||||
|
|||||||
Reference in New Issue
Block a user