vitess.io/vitess@v0.16.2/web/vtadmin/src/errors/errorHandler.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  
    17  import { pick } from 'lodash';
    18  import { getHandlers } from './errorHandlers';
    19  import { env } from '../util/env';
    20  
    21  /**
    22   * Initializes error handling for both unhandled and handled exceptions.
    23   * This should be called as early as possible.
    24   */
    25  export const initialize = () => {
    26      getHandlers().forEach((h) => h.initialize());
    27  };
    28  
    29  /**
    30   * Manually notify error handlers of an error. Also known as
    31   * a "handled error".
    32   *
    33   * @param error - The Error that was thrown/captured.
    34   * @param metadata - Additional key/value metadata to log. Note that
    35   * additional metadata from `error` will be added to the metadata
    36   * object under the key "errorMetadata" before it is passed along
    37   * to the active ErrorHandler clients(s).
    38   */
    39  export const notify = (error: Error, metadata?: object) => {
    40      const env = sanitizeEnv();
    41      const errorMetadata = Object.getOwnPropertyNames(error).reduce((acc, propertyName) => {
    42          // Only annotate the `metadata` object with properties beyond the standard instance
    43          // properties. (Bugsnag, for example, does not log additional `Error` properties:
    44          // they have to be logged as additional metadata.)
    45          if (propertyName !== 'stack' && propertyName !== 'message') {
    46              acc[propertyName] = (error as any)[propertyName];
    47          }
    48  
    49          return acc;
    50      }, {} as { [k: string]: any });
    51  
    52      getHandlers().forEach((h) =>
    53          h.notify(error, env, {
    54              errorMetadata,
    55              ...metadata,
    56          })
    57      );
    58  };
    59  
    60  /**
    61   * sanitizeEnv serializes process.env into an object that's sent to
    62   * configured error handlers, for extra debugging context.
    63   * Implemented as an allow list, rather than as a block list, to avoid
    64   * leaking sensitive environment variables, like API keys.
    65   */
    66  const sanitizeEnv = () =>
    67      pick(env(), [
    68          'REACT_APP_BUILD_BRANCH',
    69          'REACT_APP_BUILD_SHA',
    70          'REACT_APP_ENABLE_EXPERIMENTAL_TABLET_DEBUG_VARS',
    71          'REACT_APP_FETCH_CREDENTIALS',
    72          'REACT_APP_VTADMIN_API_ADDRESS',
    73      ]);