Merge pull request #6013 from tudi2d/4782-forward-unhandled-errors
Catch unhandled promise rejections & forward them to the ErrorApi
This commit is contained in:
@@ -394,6 +394,11 @@ export type SignInResult = {
|
||||
signOut?: () => Promise<void>;
|
||||
};
|
||||
|
||||
// @public (undocumented)
|
||||
export class UnhandledErrorForwarder {
|
||||
static forward(errorApi: ErrorApi, errorContext: ErrorContext): void;
|
||||
}
|
||||
|
||||
// @public
|
||||
export class UrlPatternDiscovery implements DiscoveryApi {
|
||||
static compile(pattern: string): UrlPatternDiscovery;
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { ErrorApi, ErrorContext } from '@backstage/core-plugin-api';
|
||||
|
||||
/*
|
||||
* 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 {
|
||||
/**
|
||||
* 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, errorContext);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -16,3 +16,4 @@
|
||||
|
||||
export { ErrorAlerter } from './ErrorAlerter';
|
||||
export { ErrorApiForwarder } from './ErrorApiForwarder';
|
||||
export { UnhandledErrorForwarder } from './UnhandledErrorForwarder';
|
||||
|
||||
@@ -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, { hidden: false });
|
||||
return errorApi;
|
||||
},
|
||||
}),
|
||||
createApiFactory({
|
||||
api: storageApiRef,
|
||||
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user