From 665947dd17e9a5363264d510426df2bf4ffa433a Mon Sep 17 00:00:00 2001 From: Bat-Zion Rotman Date: Tue, 7 Jan 2025 14:36:40 +0200 Subject: [PATCH] fix(orchestrator): fix bug in error handling of execute API (#247) --- .../.changeset/happy-adults-smash.md | 5 +++ .../src/service/SonataFlowService.test.ts | 5 +++ .../src/service/SonataFlowService.ts | 35 +++++++++++++------ 3 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 workspaces/orchestrator/.changeset/happy-adults-smash.md diff --git a/workspaces/orchestrator/.changeset/happy-adults-smash.md b/workspaces/orchestrator/.changeset/happy-adults-smash.md new file mode 100644 index 000000000..1602ef49e --- /dev/null +++ b/workspaces/orchestrator/.changeset/happy-adults-smash.md @@ -0,0 +1,5 @@ +--- +'@red-hat-developer-hub/backstage-plugin-orchestrator-backend': patch +--- + +fix bug in error handling of execute API diff --git a/workspaces/orchestrator/plugins/orchestrator-backend/src/service/SonataFlowService.test.ts b/workspaces/orchestrator/plugins/orchestrator-backend/src/service/SonataFlowService.test.ts index ca7c8ad03..759d35f08 100644 --- a/workspaces/orchestrator/plugins/orchestrator-backend/src/service/SonataFlowService.test.ts +++ b/workspaces/orchestrator/plugins/orchestrator-backend/src/service/SonataFlowService.test.ts @@ -287,6 +287,7 @@ describe('SonataFlowService', () => { const result = await sonataFlowService.createPrefixFetchErrorMessage( TEST_URL, mockResponse, + mockResponseJson, 'POST', ); @@ -307,6 +308,7 @@ describe('SonataFlowService', () => { const result = await sonataFlowService.createPrefixFetchErrorMessage( TEST_URL, mockResponse, + mockResponseJson, ); // Then @@ -325,6 +327,7 @@ describe('SonataFlowService', () => { const result = await sonataFlowService.createPrefixFetchErrorMessage( TEST_URL, mockResponse, + mockResponseJson, ); // Then @@ -341,6 +344,7 @@ describe('SonataFlowService', () => { const result = await sonataFlowService.createPrefixFetchErrorMessage( TEST_URL, mockResponse, + mockResponseJson, ); // Then @@ -360,6 +364,7 @@ describe('SonataFlowService', () => { const result = await sonataFlowService.createPrefixFetchErrorMessage( TEST_URL, mockResponse, + {}, ); // Then diff --git a/workspaces/orchestrator/plugins/orchestrator-backend/src/service/SonataFlowService.ts b/workspaces/orchestrator/plugins/orchestrator-backend/src/service/SonataFlowService.ts index e97fb96da..e4683c832 100644 --- a/workspaces/orchestrator/plugins/orchestrator-backend/src/service/SonataFlowService.ts +++ b/workspaces/orchestrator/plugins/orchestrator-backend/src/service/SonataFlowService.ts @@ -43,14 +43,22 @@ export class SonataFlowService { }): Promise { const urlToFetch = `${args.serviceUrl}/management/processes/${args.definitionId}`; const response = await fetch(urlToFetch); - + const jsonResponse = await response.json(); if (response.ok) { - const json = await response.json(); - this.logger.debug(`Fetch workflow info result: ${JSON.stringify(json)}`); - return json; + this.logger.debug( + `Fetch workflow info result: ${JSON.stringify(jsonResponse)}`, + ); + return jsonResponse; } + this.logger.error( + `Fetch workflow info failed with: ${JSON.stringify(jsonResponse)}`, + ); throw new Error( - await this.createPrefixFetchErrorMessage(urlToFetch, response), + await this.createPrefixFetchErrorMessage( + urlToFetch, + response, + jsonResponse, + ), ); } @@ -113,6 +121,7 @@ export class SonataFlowService { const errorMessage = await this.createPrefixFetchErrorMessage( urlToFetch, response, + json, 'POST', ); this.logger.error( @@ -141,10 +150,13 @@ export class SonataFlowService { }); if (!response.ok) { + const json = await response.json(); + this.logger.error(`Retrigger failed with: ${JSON.stringify(json)}`); throw new Error( `${await this.createPrefixFetchErrorMessage( urlToFetch, response, + json, 'POST', )}`, ); @@ -212,20 +224,23 @@ export class SonataFlowService { public async createPrefixFetchErrorMessage( urlToFetch: string, response: Response, + jsonResponse: any, httpMethod = 'GET', ): Promise { - const res = await response.json(); const errorInfo = []; let errorMsg = `Request ${httpMethod} ${urlToFetch} failed with: StatusCode: ${response.status}`; if (response.statusText) { errorInfo.push(`StatusText: ${response.statusText}`); } - if (res?.details) { - errorInfo.push(`Details: ${res?.details}`); + if (jsonResponse?.details) { + errorInfo.push(`Details: ${jsonResponse?.details}`); + } + if (jsonResponse?.stack) { + errorInfo.push(`Stack: ${jsonResponse?.stack}`); } - if (res?.stack) { - errorInfo.push(`Stack: ${res?.stack}`); + if (jsonResponse?.message) { + errorInfo.push(`Message: ${jsonResponse?.message0}`); } if (errorInfo.length > 0) { errorMsg += ` ${errorInfo.join(', ')}`;