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,