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..57edeb31 100644 --- a/src/vault/controller/connections/index.ts +++ b/src/vault/controller/connections/index.ts @@ -61,25 +61,38 @@ class ConnectionController { headers: { ...invokeRequest.headers, ...sdkHeaders }, }) .then(async (response) => { - if(!response.ok){ - const errorBody = await response.json().catch(() => null); + if (!response.ok) { + let errorBody:unknown = null ; - const error = { + try { + errorBody = await response.json(); + } catch { + try { + const text = await response.text(); + errorBody = text ? { message: text } : null; + } catch { + response.body?.cancel().catch(() => { }); + } + } + + throw { body: errorBody, statusCode: response.status, message: response.statusText, headers: response.headers }; - throw error; } + const headers = response.headers; - return response.json().then((body) => ({ headers, body })); + const body = await response.json(); + return { headers, body }; }) + .then(({headers, body}) => { printLog(logs.infoLogs.INVOKE_CONNECTION_REQUEST_RESOLVED, MessageType.LOG, this.logLevel); const requestId = headers?.get(REQUEST_ID_KEY) || ''; const invokeConnectionResponse = new InvokeConnectionResponse({ - data: body, + data:body, metadata: { requestId }, errors: null }); 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', () => {