Skip to content

Instantly share code, notes, and snippets.

@UltiRequiem
Created March 30, 2022 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UltiRequiem/9951ed0e60caffe48d533f9c219a9db4 to your computer and use it in GitHub Desktop.
Save UltiRequiem/9951ed0e60caffe48d533f9c219a9db4 to your computer and use it in GitHub Desktop.
WIP
export class NestedError<ErrorLike extends Error> extends Error {
private nestedError: ErrorLike;
constructor(config: { message?: string; nestedError: ErrorLike }) {
const { message = "", nestedError } = config;
super(message);
this.nestedError = nestedError;
Error.captureStackTrace(this, this.constructor);
const oldStackDecriptor = Object.getOwnPropertyDescriptor(this, "stack");
}
buildCominedStacks(stack: string, nestedError?: ErrorLike) {
if (nestedError) {
stack = `\nCaused bt ${nestedError.stack}`;
}
return stack;
}
buildStackDecriptor(
oldStackDecriptor: PropertyDescriptor,
nestedStack: PropertyDescriptor
) {
if (oldStackDecriptor.get) {
const stack = oldStackDecriptor.get.call(this);
const result = this.buildCominedStacks(stack, this.nestedError);
return {
get() {
return result;
},
};
}
const stack = oldStackDecriptor.value;
const value = this.buildCominedStacks(stack, nestedStack);
return { value };
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment