From a966ed8385324dde95de2a17041bee046ebb7e6e Mon Sep 17 00:00:00 2001 From: Tim Jacomb Date: Fri, 16 Sep 2022 10:47:29 +0100 Subject: [PATCH] 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 + } }, );