From a80d403d0b88259c837564f9f0cd1b7969c5d4ca Mon Sep 17 00:00:00 2001 From: kanishkkatara Date: Mon, 17 Jun 2024 18:50:54 +0530 Subject: [PATCH] chore: changes in transformation error handling --- src/util/ivmFactory.js | 11 +++++++---- src/util/prometheus.js | 6 ++++++ test/__tests__/user_transformation.test.js | 8 ++++---- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/util/ivmFactory.js b/src/util/ivmFactory.js index 50bf4caa49..9bb5606b6f 100644 --- a/src/util/ivmFactory.js +++ b/src/util/ivmFactory.js @@ -243,12 +243,15 @@ async function createIvm(code, libraryVersionIds, versionId, credentials, secret }), ); - await jail.set('_credential', function (key) { + await jail.set('_credential', function (...args) { if (_.isNil(credentials) || !_.isObject(credentials)) { - throw new Error('Credentials in incorrect format'); + logger.error('Error fetching credentials map'); + stats.increment('credential_error', { versionId }); + return undefined; } - if (key.length > 1 || _.isNil(key[0]) || !_.isString(key[0])) { - throw new Error('Key should be a string'); + const key = args[0][0]; + if (_.isNil(key)) { + throw new Error('Key should be valid and defined'); } return credentials[key]; }); diff --git a/src/util/prometheus.js b/src/util/prometheus.js index bc4c6f2eb9..e37fe249ae 100644 --- a/src/util/prometheus.js +++ b/src/util/prometheus.js @@ -569,6 +569,12 @@ class Prometheus { 'module', ], }, + { + name: 'credentials_error', + help: 'Error in fetching credentials count', + type: 'counter', + labelNames: ['versionId'], + }, // Gauges { diff --git a/test/__tests__/user_transformation.test.js b/test/__tests__/user_transformation.test.js index 710b21e6d6..e3c6122c0c 100644 --- a/test/__tests__/user_transformation.test.js +++ b/test/__tests__/user_transformation.test.js @@ -1171,7 +1171,7 @@ describe("User transformation", () => { expect(fetch).toHaveBeenCalledWith( `https://api.rudderlabs.com/transformation/getByVersionId?versionId=${versionId}` ); - expect(output[0].error).toMatch(/Key should be a string/); + expect(output[0].error).toMatch(/Key should be valid and defined/); }); it(`Simple ${name} Test with credentials with multiple arguements for codeVersion 1`, async () => { @@ -1185,7 +1185,7 @@ describe("User transformation", () => { name, code: ` export function transformEvent(event, metadata) { - event.credentialValue = credential('arg1', 'arg2'); + event.credentialValue = credential('key1', 'key2'); return event; } ` @@ -1200,7 +1200,7 @@ describe("User transformation", () => { expect(fetch).toHaveBeenCalledWith( `https://api.rudderlabs.com/transformation/getByVersionId?versionId=${versionId}` ); - expect(output[0].error).toMatch(/Key should be a string/); + expect(output[0].transformedEvent.credentialValue).toEqual("value1"); }); it(`Simple ${name} Test with credentials with non string key for codeVersion 1`, async () => { @@ -1229,7 +1229,7 @@ describe("User transformation", () => { expect(fetch).toHaveBeenCalledWith( `https://api.rudderlabs.com/transformation/getByVersionId?versionId=${versionId}` ); - expect(output[0].error).toMatch(/Key should be a string/); + expect(output[0].transformedEvent.credentialValue).toEqual(undefined); }); it(`Simple ${name} Test with credentials without value for codeVersion 1`, async () => {