/**
 * Definitely- and possibly-unhandled rejection handling.
 *
 * Copyright (C) 2017 Martin Poelstra
 * License: MIT
 */ import BaseError from "./BaseError" ; import Promise from "./Promise" ; import Trace from "./Trace" ; import { getGlobal } from "./util" ; declare var PromiseRejectionEvent ; /**
 * Base class for errors thrown when a (possibly) rejected promise is detected.
 */ export class BaseUnhandledRejection extends BaseError { /**
     * Original promise rejection reason.
     */ public reason : any ; constructor ( name , message , reason ) { super ( name , `${ message }: ${ reason }` ) ; this . reason = reason ; let stack = this . reason && typeof this . reason === "object" && this . reason . stack ; if ( typeof stack !== "string" ) { stack = String ( this . reason ) ; } this . stack = `${ this . name }: ${ stack }` ; } } /**
 * Thrown when a rejected promise is explicitly terminated with `.done()`.
 */ export class UnhandledRejection extends BaseUnhandledRejection { /**
     * Trace of rejected promise or .done() handler.
     */ public trace : Trace ; constructor ( reason , trace ) { super ( "UnhandledRejection" , "unhandled rejection" , reason ) ; this . trace = trace ; } } see Promise . onPossiblyUnhandledRejection export class PossiblyUnhandledRejection extends BaseUnhandledRejection { /**
     * Rejected promise.
     */ public promise : Promise < any > ; constructor ( promise ) { super ( "PossiblyUnhandledRejection" , "possibly unhandled rejection" , promise . reason ( ) ) ; this . promise = promise ; } } param type param reason param promise return function emitRejectionEvent ( type , reason , promise ) { const event = new PromiseRejectionEvent ( type , { cancelable : true , promise : true , reason , } ) ; Object . defineProperty ( event , "promise" , { value : promise , } ) ; const global = getGlobal ( ) ; if ( global . dispatchEvent && ! global . dispatchEvent ( event ) ) { return true ; } return false ; } /**
 * Default handler for an`UnhandledRejection` error, which contains the
 * original reason of the rejection.
 * In Node, if you don't have an `unhandledException` event handler, that will cause your
 * program to terminate after printing the error.
 * When overriding the default handler, it is recommended to keep a similar behavior,
 * as your program is likely in an unknown state.
 */ export function defaultUnhandledRejectionHandler ( reason , doneTrace ) { const unhandledRejection = new UnhandledRejection ( reason , doneTrace ) ; throw unhandledRejection ; } /**
 * Default handler for possibly unhandled rejection. It will:
 * - emit Node's `unhandledRejection` event if present, or
 * - emit an `unhandledrejection` (note small R) `PromiseRejectionEvent` on `window` or `self` if present, or
 * - log the rejection using `console.warn()`.
 *
 * Note: when attaching an `unhandledrejection` handler in the browser, make sure to
 * call `event.preventDefault()` to prevent ts-promise's default fallback logging.
 */ export function defaultPossiblyUnhandledRejectionHandler ( promise ) { let log = true ; if ( typeof process !== "undefined" && typeof process . emit === "function" ) { if ( process . emit ( "unhandledRejection" , promise . reason ( ) , promise as any ) ) { log = false ; } } else if ( typeof PromiseRejectionEvent === "function" ) { if ( emitRejectionEvent ( "unhandledrejection" , promise . reason ( ) , promise ) ) { log = false ; } } if ( log ) { const possiblyUnhandledRejection = new PossiblyUnhandledRejection ( promise ) ; console . warn ( possiblyUnhandledRejection . stack ) ; } } /**
 * Default handler for handled rejections.
 * It will emit Node's `rejectionHandled` event if present, or emit a
 * `rejectionhandled` (note small R) event on `window` (or `self`) if present.
 */ export function defaultPossiblyUnhandledRejectionHandledHandler ( promise ) { if ( typeof process !== "undefined" && typeof process . emit === "function" ) { process . emit ( "rejectionHandled" , promise as any ) ; } else if ( typeof PromiseRejectionEvent === "function" ) { emitRejectionEvent ( "rejectionhandled" , promise . reason ( ) , promise ) ; } }