interface MaybeHasCallContext { callContext ? : string ; } export class DocumentedError extends Error { _documented = 'yes' ; public callContext ? : string ; public errorDoc : string ; public stack ? : string ; public originalError ? : Error ; constructor ( message , doc , callContext ? , originalError ? ) { super ( message ) ; this . errorDoc = doc ; this . callContext = callContext ; this . originalError = originalError ; Error . captureStackTrace ( this , this . constructor ) ; } static documented ( err , message , doc , callContext ? ) { if ( callContext === undefined && ( err as MaybeHasCallContext ) . callContext !== undefined ) { callContext = ( err as MaybeHasCallContext ) . callContext ; } return new DocumentedError ( message , doc , callContext , err ) . copyStackFromOriginalError ( ) ; } copyStackFromOriginalError ( ) { if ( this . originalError ) { this . stack = this . originalError . stack ; } return this ; } static liftAddingCallContext ( err , callCtx ) { if ( ( err as DocumentedError ) . _documented === 'yes' ) { ; ( err as DocumentedError ) . callContext = callCtx ; return err ; } else { return DocumentedError . wrapUnhandledError ( err , err . message , callCtx ) ; } } static wrapUnhandledError ( err , message ? , callContext ? ) { return new DocumentedError ( message || err . message , 'Documentation needed! Please report this at https://github.com/flood-io/element/issues so that we can improve Element!' , callContext , err ) ; } }