diff --git a/src/error/messages/index.ts b/src/error/messages/index.ts index ca50746f..4a2f54cf 100644 --- a/src/error/messages/index.ts +++ b/src/error/messages/index.ts @@ -24,12 +24,12 @@ const errorMessages = { INVALID_CREDENTIALS_FILE_PATH: `${errorPrefix} Initialization failed. Invalid skyflow credentials. Expected file path to exists.`, INVALID_KEY: `${errorPrefix} Initialization failed. Invalid skyflow credentials. Specify a valid api key.`, INVALID_PARSED_CREDENTIALS_STRING: `${errorPrefix} Initialization failed. Invalid skyflow credentials. Specify a valid credentials string.`, - INVALID_BEARER_TOKEN: `${errorPrefix} Initialization failed. Invalid skyflow credentials. Specify a valid token.`, + INVALID_BEARER_TOKEN: `${errorPrefix} Initialization failed. Invalid skyflow credentials. Bearer token is invalid or expired. Specify a valid token.`, INVALID_FILE_PATH_WITH_ID: `${errorPrefix} Initialization failed. Invalid credentials. Expected file path to exists for %s1 with %s2 %s3.`, INVALID_KEY_WITH_ID: `${errorPrefix} Initialization failed. Invalid credentials. Specify a valid api key for %s1 with %s2 %s3.`, INVALID_PARSED_CREDENTIALS_STRING_WITH_ID: `${errorPrefix} Initialization failed. Invalid credentials. Specify a valid credentials string for %s1 with %s2 %s3.`, - INVALID_BEARER_TOKEN_WITH_ID: `${errorPrefix} Initialization failed. Invalid credentials. Specify a valid token for %s1 with %s2 %s3.`, + INVALID_BEARER_TOKEN_WITH_ID: `${errorPrefix} Initialization failed. Invalid credentials. Bearer token is invalid or expired. Specify a valid token for %s1 with %s2 %s3.`, EMPTY_CONNECTION_ID_VALIDATION: `${errorPrefix} Validation error. Invalid connection ID. Specify a valid connection Id.`, EMPTY_CONNECTION_ID: `${errorPrefix} Initialization failed. Invalid connection ID. Specify a valid connection Id.`, diff --git a/src/utils/index.ts b/src/utils/index.ts index 856359e3..f00abcee 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -365,9 +365,9 @@ export function fillUrlWithPathAndQueryParams(url: string, let filledUrl = url; if (pathParams) { Object.entries(pathParams).forEach(([key, value]) => { - filledUrl = url.replace(`{${key}}`, String(value)); + filledUrl = filledUrl.replace(`{${key}}`, String(value)); }); - } + } if (queryParams) { filledUrl += '?'; Object.entries(queryParams).forEach(([key, value]) => { diff --git a/src/vault/controller/connections/index.ts b/src/vault/controller/connections/index.ts index c18abe22..30ef824f 100644 --- a/src/vault/controller/connections/index.ts +++ b/src/vault/controller/connections/index.ts @@ -62,7 +62,21 @@ class ConnectionController { }) .then(async (response) => { if(!response.ok){ - const errorBody = await response.json().catch(() => null); + let errorBody: any = null; + try { + errorBody = await response.json(); + } catch (jsonError) { + // If JSON parsing fails, consume as text to close connection + try { + const text = await response.text(); + errorBody = text ? { message: text } : null; + } catch (textError) { + // If text consumption also fails, ensure body is consumed + if (response.body) { + await response.body.cancel().catch(() => {}); + } + } + } const error = { body: errorBody, @@ -73,6 +87,7 @@ class ConnectionController { throw error; } const headers = response.headers; + // Consume response body - this will close the connection return response.json().then((body) => ({ headers, body })); }) .then(({headers, body}) => { diff --git a/test/vault/utils/utils.test.js b/test/vault/utils/utils.test.js index ffea9c3d..625de549 100644 --- a/test/vault/utils/utils.test.js +++ b/test/vault/utils/utils.test.js @@ -162,7 +162,7 @@ describe('fillUrlWithPathAndQueryParams', () => { const url = '/api/resource/{category}/{id}'; const pathParams = { category: 'books', id: '456' }; const result = fillUrlWithPathAndQueryParams(url, pathParams); - expect(result).toBe('/api/resource/{category}/456'); + expect(result).toBe('/api/resource/books/456'); }); test('should handle query parameters with special characters', () => {