From 2b5c00f7625cd7f101511e13480180f592b42bc7 Mon Sep 17 00:00:00 2001 From: Philipp Hugenroth Date: Fri, 11 Jun 2021 14:12:36 +0200 Subject: [PATCH 01/10] Add UnhandledErrorForwarder forwarding errors to ErrorApi Signed-off-by: Philipp Hugenroth --- .../ErrorApi/UnhandledErrorForwarder.ts | 37 +++++++++++++++++++ .../apis/implementations/ErrorApi/index.ts | 1 + packages/core/src/api-wrappers/defaultApis.ts | 8 +++- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 packages/core-api/src/apis/implementations/ErrorApi/UnhandledErrorForwarder.ts diff --git a/packages/core-api/src/apis/implementations/ErrorApi/UnhandledErrorForwarder.ts b/packages/core-api/src/apis/implementations/ErrorApi/UnhandledErrorForwarder.ts new file mode 100644 index 0000000000..64fd556057 --- /dev/null +++ b/packages/core-api/src/apis/implementations/ErrorApi/UnhandledErrorForwarder.ts @@ -0,0 +1,37 @@ +import { ErrorApi } from '../../definitions'; + +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +export class UnhandledErrorForwarder { + static forward(errorApi: ErrorApi) { + window.addEventListener( + 'unhandledrejection', + (e: PromiseRejectionEvent) => { + errorApi.post( + { + name: `Unhandled Rejection: ${e.reason.message}`, + message: e.reason.message, + stack: e.reason.stack, + }, + { hidden: true }, + ); + // eslint-disable-next-line no-console + console.error(e.reason); + e.preventDefault(); + }, + ); + } +} diff --git a/packages/core-api/src/apis/implementations/ErrorApi/index.ts b/packages/core-api/src/apis/implementations/ErrorApi/index.ts index 757dfd0d8f..86b9b35b24 100644 --- a/packages/core-api/src/apis/implementations/ErrorApi/index.ts +++ b/packages/core-api/src/apis/implementations/ErrorApi/index.ts @@ -16,3 +16,4 @@ export { ErrorAlerter } from './ErrorAlerter'; export { ErrorApiForwarder } from './ErrorApiForwarder'; +export { UnhandledErrorForwarder } from './UnhandledErrorForwarder'; diff --git a/packages/core/src/api-wrappers/defaultApis.ts b/packages/core/src/api-wrappers/defaultApis.ts index d044b50b7a..662b986e28 100644 --- a/packages/core/src/api-wrappers/defaultApis.ts +++ b/packages/core/src/api-wrappers/defaultApis.ts @@ -47,6 +47,7 @@ import { oneloginAuthApiRef, OneLoginAuth, oidcAuthApiRef, + UnhandledErrorForwarder, } from '@backstage/core-api'; import OAuth2Icon from '@material-ui/icons/AcUnit'; @@ -64,8 +65,11 @@ export const defaultApis = [ createApiFactory({ api: errorApiRef, deps: { alertApi: alertApiRef }, - factory: ({ alertApi }) => - new ErrorAlerter(alertApi, new ErrorApiForwarder()), + factory: ({ alertApi }) => { + const errorApi = new ErrorAlerter(alertApi, new ErrorApiForwarder()); + UnhandledErrorForwarder.forward(errorApi); + return errorApi; + }, }), createApiFactory({ api: storageApiRef, From dc3e7ce68a9f2e2e1506045a978bb4c91ddbe2ce Mon Sep 17 00:00:00 2001 From: Philipp Hugenroth Date: Fri, 11 Jun 2021 14:32:08 +0200 Subject: [PATCH 02/10] Add changeset Signed-off-by: Philipp Hugenroth --- .changeset/fair-knives-relax.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/fair-knives-relax.md diff --git a/.changeset/fair-knives-relax.md b/.changeset/fair-knives-relax.md new file mode 100644 index 0000000000..12c273b2f5 --- /dev/null +++ b/.changeset/fair-knives-relax.md @@ -0,0 +1,6 @@ +--- +'@backstage/core': patch +'@backstage/core-api': patch +--- + +Catch unhandled promise rejections & forward them to the ErrorApi to align with general error handling From 11ff8c952a445c4ef37d99a9fb19d362508a6acb Mon Sep 17 00:00:00 2001 From: Philipp Hugenroth Date: Fri, 18 Jun 2021 16:53:39 +0200 Subject: [PATCH 03/10] Adjust UnhandledErrorForwarder to not silence error - Changes related to revew Signed-off-by: Philipp Hugenroth --- .changeset/fair-knives-relax.md | 2 +- .../src/apis/implementations/ErrorApi/index.ts | 1 - .../ErrorApi/UnhandledErrorForwarder.ts | 14 ++------------ .../src/apis/implementations/ErrorApi/index.ts | 1 + packages/core-app-api/src/app/defaultApis.ts | 8 ++++++-- packages/core/src/api-wrappers/defaultApis.ts | 8 ++------ 6 files changed, 12 insertions(+), 22 deletions(-) rename packages/{core-api => core-app-api}/src/apis/implementations/ErrorApi/UnhandledErrorForwarder.ts (68%) diff --git a/.changeset/fair-knives-relax.md b/.changeset/fair-knives-relax.md index 12c273b2f5..2ab2ec39cf 100644 --- a/.changeset/fair-knives-relax.md +++ b/.changeset/fair-knives-relax.md @@ -3,4 +3,4 @@ '@backstage/core-api': patch --- -Catch unhandled promise rejections & forward them to the ErrorApi to align with general error handling +Introducing new UnhandledErrorForwarder installed by default. For catchin unhandled promise rejections you can override the API to align with general error handling. diff --git a/packages/core-api/src/apis/implementations/ErrorApi/index.ts b/packages/core-api/src/apis/implementations/ErrorApi/index.ts index 86b9b35b24..757dfd0d8f 100644 --- a/packages/core-api/src/apis/implementations/ErrorApi/index.ts +++ b/packages/core-api/src/apis/implementations/ErrorApi/index.ts @@ -16,4 +16,3 @@ export { ErrorAlerter } from './ErrorAlerter'; export { ErrorApiForwarder } from './ErrorApiForwarder'; -export { UnhandledErrorForwarder } from './UnhandledErrorForwarder'; diff --git a/packages/core-api/src/apis/implementations/ErrorApi/UnhandledErrorForwarder.ts b/packages/core-app-api/src/apis/implementations/ErrorApi/UnhandledErrorForwarder.ts similarity index 68% rename from packages/core-api/src/apis/implementations/ErrorApi/UnhandledErrorForwarder.ts rename to packages/core-app-api/src/apis/implementations/ErrorApi/UnhandledErrorForwarder.ts index 64fd556057..b4f9932ee9 100644 --- a/packages/core-api/src/apis/implementations/ErrorApi/UnhandledErrorForwarder.ts +++ b/packages/core-app-api/src/apis/implementations/ErrorApi/UnhandledErrorForwarder.ts @@ -16,21 +16,11 @@ import { ErrorApi } from '../../definitions'; * limitations under the License. */ export class UnhandledErrorForwarder { - static forward(errorApi: ErrorApi) { + static forward(errorApi: ErrorApi, hidden = false) { window.addEventListener( 'unhandledrejection', (e: PromiseRejectionEvent) => { - errorApi.post( - { - name: `Unhandled Rejection: ${e.reason.message}`, - message: e.reason.message, - stack: e.reason.stack, - }, - { hidden: true }, - ); - // eslint-disable-next-line no-console - console.error(e.reason); - e.preventDefault(); + errorApi.post(e.reason as Error, { hidden }); }, ); } diff --git a/packages/core-app-api/src/apis/implementations/ErrorApi/index.ts b/packages/core-app-api/src/apis/implementations/ErrorApi/index.ts index 757dfd0d8f..86b9b35b24 100644 --- a/packages/core-app-api/src/apis/implementations/ErrorApi/index.ts +++ b/packages/core-app-api/src/apis/implementations/ErrorApi/index.ts @@ -16,3 +16,4 @@ export { ErrorAlerter } from './ErrorAlerter'; export { ErrorApiForwarder } from './ErrorApiForwarder'; +export { UnhandledErrorForwarder } from './UnhandledErrorForwarder'; diff --git a/packages/core-app-api/src/app/defaultApis.ts b/packages/core-app-api/src/app/defaultApis.ts index 5d84e27f89..8ebdaa10ed 100644 --- a/packages/core-app-api/src/app/defaultApis.ts +++ b/packages/core-app-api/src/app/defaultApis.ts @@ -30,6 +30,7 @@ import { UrlPatternDiscovery, SamlAuth, OneLoginAuth, + UnhandledErrorForwarder, } from '../apis'; import { @@ -67,8 +68,11 @@ export const defaultApis = [ createApiFactory({ api: errorApiRef, deps: { alertApi: alertApiRef }, - factory: ({ alertApi }) => - new ErrorAlerter(alertApi, new ErrorApiForwarder()), + factory: ({ alertApi }) => { + const errorApi = new ErrorAlerter(alertApi, new ErrorApiForwarder()); + UnhandledErrorForwarder.forward(errorApi); + return errorApi; + }, }), createApiFactory({ api: storageApiRef, diff --git a/packages/core/src/api-wrappers/defaultApis.ts b/packages/core/src/api-wrappers/defaultApis.ts index 662b986e28..d044b50b7a 100644 --- a/packages/core/src/api-wrappers/defaultApis.ts +++ b/packages/core/src/api-wrappers/defaultApis.ts @@ -47,7 +47,6 @@ import { oneloginAuthApiRef, OneLoginAuth, oidcAuthApiRef, - UnhandledErrorForwarder, } from '@backstage/core-api'; import OAuth2Icon from '@material-ui/icons/AcUnit'; @@ -65,11 +64,8 @@ export const defaultApis = [ createApiFactory({ api: errorApiRef, deps: { alertApi: alertApiRef }, - factory: ({ alertApi }) => { - const errorApi = new ErrorAlerter(alertApi, new ErrorApiForwarder()); - UnhandledErrorForwarder.forward(errorApi); - return errorApi; - }, + factory: ({ alertApi }) => + new ErrorAlerter(alertApi, new ErrorApiForwarder()), }), createApiFactory({ api: storageApiRef, From 16b35af2b95e2ec7697f69c2bd43d212de21560e Mon Sep 17 00:00:00 2001 From: Philipp Hugenroth Date: Mon, 21 Jun 2021 11:53:05 +0200 Subject: [PATCH 04/10] Fix ErrorApi types import Signed-off-by: Philipp Hugenroth --- .../apis/implementations/ErrorApi/UnhandledErrorForwarder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core-app-api/src/apis/implementations/ErrorApi/UnhandledErrorForwarder.ts b/packages/core-app-api/src/apis/implementations/ErrorApi/UnhandledErrorForwarder.ts index b4f9932ee9..7ef3cec3dd 100644 --- a/packages/core-app-api/src/apis/implementations/ErrorApi/UnhandledErrorForwarder.ts +++ b/packages/core-app-api/src/apis/implementations/ErrorApi/UnhandledErrorForwarder.ts @@ -1,4 +1,4 @@ -import { ErrorApi } from '../../definitions'; +import { ErrorApi } from '@backstage/core-plugin-api'; /* * Copyright 2020 Spotify AB From 8958e097d934b98af4979c3ad1bbd6ded55f5d2a Mon Sep 17 00:00:00 2001 From: Philipp Hugenroth Date: Mon, 21 Jun 2021 12:01:09 +0200 Subject: [PATCH 05/10] Fix spelling in changeset Signed-off-by: Philipp Hugenroth --- .changeset/fair-knives-relax.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/fair-knives-relax.md b/.changeset/fair-knives-relax.md index 2ab2ec39cf..165793b229 100644 --- a/.changeset/fair-knives-relax.md +++ b/.changeset/fair-knives-relax.md @@ -3,4 +3,4 @@ '@backstage/core-api': patch --- -Introducing new UnhandledErrorForwarder installed by default. For catchin unhandled promise rejections you can override the API to align with general error handling. +Introducing new UnhandledErrorForwarder installed by default. For catching unhandled promise rejections, you can override the API to align with general error handling. From 8b69e8cd3e0b3b24cfc6de2a5d7a5ab7e197a434 Mon Sep 17 00:00:00 2001 From: Philipp Hugenroth Date: Mon, 21 Jun 2021 12:45:28 +0200 Subject: [PATCH 06/10] Updated api report doc Signed-off-by: Philipp Hugenroth --- packages/core-app-api/api-report.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/core-app-api/api-report.md b/packages/core-app-api/api-report.md index 5aa709121a..a45f30b5b2 100644 --- a/packages/core-app-api/api-report.md +++ b/packages/core-app-api/api-report.md @@ -394,6 +394,12 @@ export type SignInResult = { signOut?: () => Promise; }; +// @public (undocumented) +export class UnhandledErrorForwarder { + // (undocumented) + static forward(errorApi: ErrorApi, hidden?: boolean): void; +} + // @public export class UrlPatternDiscovery implements DiscoveryApi { static compile(pattern: string): UrlPatternDiscovery; From 9f8a07864071699b9f1d5704bfb9f060b152284d Mon Sep 17 00:00:00 2001 From: Philipp Hugenroth Date: Mon, 21 Jun 2021 13:35:24 +0200 Subject: [PATCH 07/10] Adjust tests to cover update utility api Signed-off-by: Philipp Hugenroth --- packages/core-app-api/src/index.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/core-app-api/src/index.test.ts b/packages/core-app-api/src/index.test.ts index 654c547fca..4a8e08aad4 100644 --- a/packages/core-app-api/src/index.test.ts +++ b/packages/core-app-api/src/index.test.ts @@ -37,6 +37,7 @@ describe('index', () => { ConfigReader: expect.any(Function), ErrorAlerter: expect.any(Function), ErrorApiForwarder: expect.any(Function), + UnhandledErrorForwarder: expect.any(Function), FeatureFlagged: expect.any(Function), GithubAuth: expect.any(Function), GitlabAuth: expect.any(Function), From de77f03f01935bcb3f1c6a0ff8207ea9cdad5d1c Mon Sep 17 00:00:00 2001 From: Philipp Hugenroth Date: Tue, 22 Jun 2021 12:57:08 +0200 Subject: [PATCH 08/10] Adjust docs & parameters toto fit the API Signed-off-by: Philipp Hugenroth --- docs/api/utility-apis.md | 7 +++++-- .../ErrorApi/UnhandledErrorForwarder.ts | 10 +++++++--- packages/core-app-api/src/app/defaultApis.ts | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/docs/api/utility-apis.md b/docs/api/utility-apis.md index 308544e0f1..56dd46794a 100644 --- a/docs/api/utility-apis.md +++ b/docs/api/utility-apis.md @@ -71,8 +71,11 @@ For example, this is the default `ApiFactory` for the `ErrorApi`: createApiFactory({ api: errorApiRef, deps: { alertApi: alertApiRef }, - factory: ({ alertApi }) => - new ErrorAlerter(alertApi, new ErrorApiForwarder()), + factory: ({ alertApi }) => { + const errorApi = new ErrorAlerter(alertApi, new ErrorApiForwarder()); + UnhandledErrorForwarder.forward(errorApi, { hidden: false }); + return errorApi; + }, }); ``` diff --git a/packages/core-app-api/src/apis/implementations/ErrorApi/UnhandledErrorForwarder.ts b/packages/core-app-api/src/apis/implementations/ErrorApi/UnhandledErrorForwarder.ts index 7ef3cec3dd..b859148e8e 100644 --- a/packages/core-app-api/src/apis/implementations/ErrorApi/UnhandledErrorForwarder.ts +++ b/packages/core-app-api/src/apis/implementations/ErrorApi/UnhandledErrorForwarder.ts @@ -1,4 +1,4 @@ -import { ErrorApi } from '@backstage/core-plugin-api'; +import { ErrorApi, ErrorContext } from '@backstage/core-plugin-api'; /* * Copyright 2020 Spotify AB @@ -15,12 +15,16 @@ import { ErrorApi } from '@backstage/core-plugin-api'; * See the License for the specific language governing permissions and * limitations under the License. */ + export class UnhandledErrorForwarder { - static forward(errorApi: ErrorApi, hidden = false) { + /** + * Add event listener, such that unhandled errors can be forwarded using an given `ErrorApi` instance + */ + static forward(errorApi: ErrorApi, errorContext: ErrorContext) { window.addEventListener( 'unhandledrejection', (e: PromiseRejectionEvent) => { - errorApi.post(e.reason as Error, { hidden }); + errorApi.post(e.reason as Error, errorContext); }, ); } diff --git a/packages/core-app-api/src/app/defaultApis.ts b/packages/core-app-api/src/app/defaultApis.ts index 8ebdaa10ed..f0657f2092 100644 --- a/packages/core-app-api/src/app/defaultApis.ts +++ b/packages/core-app-api/src/app/defaultApis.ts @@ -70,7 +70,7 @@ export const defaultApis = [ deps: { alertApi: alertApiRef }, factory: ({ alertApi }) => { const errorApi = new ErrorAlerter(alertApi, new ErrorApiForwarder()); - UnhandledErrorForwarder.forward(errorApi); + UnhandledErrorForwarder.forward(errorApi, { hidden: false }); return errorApi; }, }), From 791a36f845678b7baf19f995eabf690dbff7e32e Mon Sep 17 00:00:00 2001 From: Philipp Hugenroth Date: Tue, 22 Jun 2021 13:16:09 +0200 Subject: [PATCH 09/10] Regenerate API reports Signed-off-by: Philipp Hugenroth --- packages/core-app-api/api-report.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/core-app-api/api-report.md b/packages/core-app-api/api-report.md index a45f30b5b2..ce85078a88 100644 --- a/packages/core-app-api/api-report.md +++ b/packages/core-app-api/api-report.md @@ -396,8 +396,7 @@ export type SignInResult = { // @public (undocumented) export class UnhandledErrorForwarder { - // (undocumented) - static forward(errorApi: ErrorApi, hidden?: boolean): void; + static forward(errorApi: ErrorApi, errorContext: ErrorContext): void; } // @public From 4a1c93a78ab5165ec15a1931f9892a8d3a5a3b41 Mon Sep 17 00:00:00 2001 From: Philipp Hugenroth Date: Tue, 22 Jun 2021 15:40:35 +0200 Subject: [PATCH 10/10] Adjust changeset to updated PR Signed-off-by: Philipp Hugenroth --- .changeset/fair-knives-relax.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.changeset/fair-knives-relax.md b/.changeset/fair-knives-relax.md index 165793b229..bc1357d841 100644 --- a/.changeset/fair-knives-relax.md +++ b/.changeset/fair-knives-relax.md @@ -1,6 +1,5 @@ --- -'@backstage/core': patch -'@backstage/core-api': patch +'@backstage/core-app-api': patch --- Introducing new UnhandledErrorForwarder installed by default. For catching unhandled promise rejections, you can override the API to align with general error handling.