Skip to content

Commit

Permalink
Clone error with all constructor parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
terehov committed Apr 12, 2023
1 parent 28ef27a commit 0e751a5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
10 changes: 10 additions & 0 deletions examples/nodejs/index2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,13 @@ const e = createReadonlyError("message", "property");
logger.error(e);

///////////////////////////

class CustomError extends Error {
constructor(message1: string, message2: string) {
super(message1);
console.log("***", message1, message2);
}
}

const err = new CustomError("a", "b");
logger.error(err);
11 changes: 7 additions & 4 deletions src/BaseLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,14 @@ export class BaseLogger<LogObj> {
}

private _cloneError<T extends Error>(error: T): T {
const ErrorConstructor = error.constructor as new (message?: string) => T;
const newError = new ErrorConstructor(error.message);
const ErrorConstructor = error.constructor as new (...args: any[]) => T;
const errorProperties = Object.getOwnPropertyNames(error);
const errorArgs = errorProperties.map((propName) => error[propName]);

const newError = new ErrorConstructor(...errorArgs);

This comment has been minimized.

Copy link
@mustard-mh

mustard-mh Aug 4, 2023

@terehov Do we have any plan for this fixing release? 🙏

Object.assign(newError, error);
const propertyNames = Object.getOwnPropertyNames(newError);
for (const propName of propertyNames) {

for (const propName of errorProperties) {
const propDesc = Object.getOwnPropertyDescriptor(newError, propName);
if (propDesc) {
propDesc.writable = true;
Expand Down

0 comments on commit 0e751a5

Please sign in to comment.