11import { parseRegexSingle } from "@foxxmd/regex-buddy-core" ;
22import mergeErrorCause from 'merge-error-cause' ;
3- import { findCauseByFunc , findCauseByReference } from "../../utils/ErrorUtils.js" ;
3+ import { findCauseByFunc , findCauseByReference , isAbortReasonErrorLike } from "../../utils/ErrorUtils.js" ;
44import { UpstreamError , UpstreamErrorOptions } from "./UpstreamError.js" ;
5+ import { isAbortError } from "abort-controller-x" ;
56
67export abstract class NamedError extends Error {
78 public abstract name : string ;
@@ -39,21 +40,29 @@ export class SimpleError extends Error implements HasSimpleError {
3940 simple : boolean ;
4041 name = 'Error' ;
4142
43+ stackShortened : boolean = false ;
44+
45+ shortenStack ( ) {
46+ const atIndex = parseRegexSingle ( STACK_AT_REGEX , this . stack ) ;
47+ if ( atIndex !== undefined ) {
48+ const firstn = this . stack . indexOf ( '\n' , atIndex . index + atIndex . match . length ) ;
49+ if ( firstn !== - 1 ) {
50+ this . stack = this . stack . slice ( 0 , firstn ) ;
51+ this . stackShortened = true ;
52+ }
53+ }
54+ }
55+
4256 public constructor ( msg : string , options ?: ErrorOptions & { simple ?: boolean , shortStack ?: boolean } ) {
4357 super ( msg , options ) ;
4458 const {
4559 simple = true ,
4660 shortStack = false
4761 } = options || { } ;
4862 this . simple = simple ;
63+ Error . captureStackTrace ( this , this . constructor ) ;
4964 if ( shortStack ) {
50- const atIndex = parseRegexSingle ( STACK_AT_REGEX , this . stack ) ;
51- if ( atIndex !== undefined ) {
52- const firstn = this . stack . indexOf ( '\n' , atIndex . index + atIndex . match . length ) ;
53- if ( firstn !== - 1 ) {
54- this . stack = this . stack . slice ( 0 , firstn ) ;
55- }
56- }
65+ this . shortenStack ( ) ;
5766 }
5867 }
5968}
@@ -104,4 +113,19 @@ export class ScrobbleSubmitError<T extends (object | string) = object> extends U
104113 super ( message , options ) ;
105114 this . payload = options ?. payload ;
106115 }
116+ }
117+
118+ export class AbortedError extends SimpleError {
119+ name = 'Aborted Operation' ;
120+ }
121+ export const generateLoggableAbortReason = ( msg : string , signal : AbortSignal ) : AbortedError => {
122+ const reason = signal . reason ;
123+ let err : AbortedError ;
124+ if ( isAbortReasonErrorLike ( signal ) ) {
125+ err = new AbortedError ( msg , { cause : reason } ) ;
126+ } else {
127+ err = new AbortedError ( `${ msg } => ${ reason ?? 'No Reason Given' } ` , { simple : true , shortStack : true } ) ;
128+ }
129+ Error . captureStackTrace ( err , generateLoggableAbortReason ) ;
130+ return err ;
107131}
0 commit comments