From a966ed8385324dde95de2a17041bee046ebb7e6e Mon Sep 17 00:00:00 2001 From: Tim Jacomb Date: Fri, 16 Sep 2022 10:47:29 +0100 Subject: [PATCH 1/2] Unwrap error message when getting projects in Jenkins plugin Signed-off-by: Tim Jacomb --- .changeset/ninety-knives-knock.md | 5 +++++ .../jenkins-backend/src/service/jenkinsApi.ts | 2 +- plugins/jenkins-backend/src/service/router.ts | 20 +++++++++++++++---- 3 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 .changeset/ninety-knives-knock.md diff --git a/.changeset/ninety-knives-knock.md b/.changeset/ninety-knives-knock.md new file mode 100644 index 0000000000..19797e472c --- /dev/null +++ b/.changeset/ninety-knives-knock.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-jenkins-backend': patch +--- + +Unwrap error message when getting projects diff --git a/plugins/jenkins-backend/src/service/jenkinsApi.ts b/plugins/jenkins-backend/src/service/jenkinsApi.ts index ae2fa0f7b5..37aaf0ac3b 100644 --- a/plugins/jenkins-backend/src/service/jenkinsApi.ts +++ b/plugins/jenkins-backend/src/service/jenkinsApi.ts @@ -100,7 +100,7 @@ export class JenkinsApiImpl { // Filter only be the information we need, instead of loading all fields. // Limit to only show the latest build for each job and only load 50 jobs // at all. - // Whitespaces are only included for readablity here and stripped out + // Whitespaces are only included for readability here and stripped out // before sending to Jenkins tree: JenkinsApiImpl.jobsTreeSpec.replace(/\s/g, ''), }); diff --git a/plugins/jenkins-backend/src/service/router.ts b/plugins/jenkins-backend/src/service/router.ts index bc0e978396..395efffb9c 100644 --- a/plugins/jenkins-backend/src/service/router.ts +++ b/plugins/jenkins-backend/src/service/router.ts @@ -27,6 +27,7 @@ import { } from '@backstage/plugin-permission-common'; import { getBearerTokenFromAuthorizationHeader } from '@backstage/plugin-auth-node'; import { stringifyEntityRef } from '@backstage/catalog-model'; +import { stringifyError } from "@backstage/errors"; /** @public */ export interface RouterOptions { @@ -90,11 +91,22 @@ export async function createRouter( }, backstageToken: token, }); - const projects = await jenkinsApi.getProjects(jenkinsInfo, branches); - response.json({ - projects: projects, - }); + + try { + const projects = await jenkinsApi.getProjects(jenkinsInfo, branches); + + response.json({ + projects: projects, + }); + } catch (err) { + // Promise.any, used in the getProjects call returns an Aggregate error message with a useless error message 'AggregateError: All promises were rejected' + // extract useful information ourselves + if (err.errors) { + throw new Error(`Unable to fetch projects, for ${jenkinsInfo.jobFullName}: ${stringifyError(err.errors)}`) + } + throw err + } }, ); From 50577161a4e3d82fdd6bc96f4e1b5ec082d64c9d Mon Sep 17 00:00:00 2001 From: Tim Jacomb Date: Mon, 19 Sep 2022 08:48:35 +0100 Subject: [PATCH 2/2] Prettier Signed-off-by: Tim Jacomb --- plugins/jenkins-backend/src/service/router.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/plugins/jenkins-backend/src/service/router.ts b/plugins/jenkins-backend/src/service/router.ts index 395efffb9c..df4c3b5a11 100644 --- a/plugins/jenkins-backend/src/service/router.ts +++ b/plugins/jenkins-backend/src/service/router.ts @@ -27,7 +27,7 @@ import { } from '@backstage/plugin-permission-common'; import { getBearerTokenFromAuthorizationHeader } from '@backstage/plugin-auth-node'; import { stringifyEntityRef } from '@backstage/catalog-model'; -import { stringifyError } from "@backstage/errors"; +import { stringifyError } from '@backstage/errors'; /** @public */ export interface RouterOptions { @@ -92,7 +92,6 @@ export async function createRouter( backstageToken: token, }); - try { const projects = await jenkinsApi.getProjects(jenkinsInfo, branches); @@ -103,9 +102,13 @@ export async function createRouter( // Promise.any, used in the getProjects call returns an Aggregate error message with a useless error message 'AggregateError: All promises were rejected' // extract useful information ourselves if (err.errors) { - throw new Error(`Unable to fetch projects, for ${jenkinsInfo.jobFullName}: ${stringifyError(err.errors)}`) + throw new Error( + `Unable to fetch projects, for ${ + jenkinsInfo.jobFullName + }: ${stringifyError(err.errors)}`, + ); } - throw err + throw err; } }, );