"Fossies" - the Fresh Open Source Software Archive

Member "cli-1.1280.1/src/lib/unexpected-error.ts" (20 Feb 2024, 2254 Bytes) of package /linux/misc/snyk-cli-1.1280.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /**
    2  * Ensures a given function does not throw any errors, including unexpected ones
    3  * outside of its chain of execution.
    4  *
    5  * This function can only be used once in the same process. If you have multiple
    6  * callables needing this, compose them into a single callable.
    7  */
    8 export async function callHandlingUnexpectedErrors(
    9   callable: () => Promise<unknown>,
   10   exitCode: number,
   11 ): Promise<void> {
   12   function handleUnexpectedError(reason: unknown): never {
   13     console.error('Something unexpected went wrong:', reason);
   14     console.error('Exit code:', exitCode);
   15     process.exit(exitCode);
   16   }
   17 
   18   process.on('uncaughtException', handleUnexpectedError);
   19 
   20   /**
   21    * Since Node 15, 'unhandledRejection' without a handler causes an
   22    * 'uncaughtException'. However, we also support Node 14 (as of writing)
   23    * which doesn't have that behaviour. So we still need this handler for now.
   24    */
   25   process.on('unhandledRejection', handleUnexpectedError);
   26 
   27   try {
   28     await callable();
   29   } catch (e) {
   30     handleUnexpectedError(e);
   31   }
   32 
   33   /**
   34    * Do NOT remove any 'uncaughtException' and 'unhandledRejection' handlers.
   35    * It may seem like we should after callable has done its thing. However,
   36    * there's never a point when everything's "done". There's no guarantee that
   37    * a callable hasn't created a chain of execution outside of its chain which
   38    * might trigger an unexpected error.
   39    *
   40    * For example, we want to avoid this scenario:
   41    *
   42    * - Add Handler L for 'uncaughtException' and 'unhandledRejection'
   43    * - Call Function A
   44    * - Function A calls Function B, which returns Promise P.
   45    * - Function A does not await Promise P
   46    * - Function A returns.
   47    * - Remove Handler L. <- Don't do this! Otherwise...
   48    * - Some time passes...
   49    * - Promise P rejects.
   50    * - NodeJS has nothing left to execute.
   51    * - NodeJS gathers all unhandled rejected promises.
   52    * - There are no 'unhandledRejection' handlers.
   53    * - For Node 15 and above:
   54    *   - NodeJS triggers 'uncaughtException'
   55    *   - There are no 'uncaughtException' handlers.
   56    *   - NodeJS defaults to Exit Code 1. <- We want a different exit code.
   57    * - For Node 14 and below:
   58    *   - NodeJS logs a warning. <- We want the same behaviour across versions.
   59    */
   60 }