vitess.io/vitess@v0.16.2/web/vtadmin/src/errors/bugsnag.ts (about) 1 /** 2 * Copyright 2021 The Vitess Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 import BugsnagJS from '@bugsnag/js'; 17 import { ErrorHandler } from './errorTypes'; 18 import { env } from '../util/env'; 19 20 const { REACT_APP_BUGSNAG_API_KEY } = env(); 21 22 /** 23 * If using Bugsnag, Bugsnag.start() will automatically capture and report 24 * unhandled exceptions and unhandled promise rejections, as well as 25 * initialize it for capturing handled errors. 26 */ 27 export const initialize = () => { 28 if (typeof REACT_APP_BUGSNAG_API_KEY === 'string' && REACT_APP_BUGSNAG_API_KEY.length) { 29 BugsnagJS.start(REACT_APP_BUGSNAG_API_KEY); 30 } 31 }; 32 33 export const isEnabled = () => typeof REACT_APP_BUGSNAG_API_KEY === 'string'; 34 35 export const notify = (error: Error, env: object, metadata?: object) => { 36 // See https://docs.bugsnag.com/platforms/javascript/reporting-handled-errors/ 37 BugsnagJS.notify(error, (event) => { 38 event.addMetadata('env', env); 39 40 if (!!metadata) { 41 event.addMetadata('metadata', metadata); 42 } 43 }); 44 }; 45 46 export const Bugsnag: ErrorHandler = { 47 initialize, 48 isEnabled, 49 notify, 50 };