Skip to content

Commit

Permalink
fix(orchestrator): fix bug in error handling of execute API (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
batzionb authored Jan 7, 2025
1 parent d054768 commit 665947d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
5 changes: 5 additions & 0 deletions workspaces/orchestrator/.changeset/happy-adults-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@red-hat-developer-hub/backstage-plugin-orchestrator-backend': patch
---

fix bug in error handling of execute API
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ describe('SonataFlowService', () => {
const result = await sonataFlowService.createPrefixFetchErrorMessage(
TEST_URL,
mockResponse,
mockResponseJson,
'POST',
);

Expand All @@ -307,6 +308,7 @@ describe('SonataFlowService', () => {
const result = await sonataFlowService.createPrefixFetchErrorMessage(
TEST_URL,
mockResponse,
mockResponseJson,
);

// Then
Expand All @@ -325,6 +327,7 @@ describe('SonataFlowService', () => {
const result = await sonataFlowService.createPrefixFetchErrorMessage(
TEST_URL,
mockResponse,
mockResponseJson,
);

// Then
Expand All @@ -341,6 +344,7 @@ describe('SonataFlowService', () => {
const result = await sonataFlowService.createPrefixFetchErrorMessage(
TEST_URL,
mockResponse,
mockResponseJson,
);

// Then
Expand All @@ -360,6 +364,7 @@ describe('SonataFlowService', () => {
const result = await sonataFlowService.createPrefixFetchErrorMessage(
TEST_URL,
mockResponse,
{},
);

// Then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,22 @@ export class SonataFlowService {
}): Promise<WorkflowInfo | undefined> {
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,
),
);
}

Expand Down Expand Up @@ -113,6 +121,7 @@ export class SonataFlowService {
const errorMessage = await this.createPrefixFetchErrorMessage(
urlToFetch,
response,
json,
'POST',
);
this.logger.error(
Expand Down Expand Up @@ -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',
)}`,
);
Expand Down Expand Up @@ -212,20 +224,23 @@ export class SonataFlowService {
public async createPrefixFetchErrorMessage(
urlToFetch: string,
response: Response,
jsonResponse: any,
httpMethod = 'GET',
): Promise<string> {
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(', ')}`;
Expand Down

0 comments on commit 665947d

Please sign in to comment.