Skip to content

Instantly share code, notes, and snippets.

@lefam
Created November 13, 2020 00:21
Show Gist options
  • Save lefam/b4ceb647e0b44c0e32ba5ee40c3053db to your computer and use it in GitHub Desktop.
Save lefam/b4ceb647e0b44c0e32ba5ee40c3053db to your computer and use it in GitHub Desktop.
Function to use with JSON.stringify to print Error objects (typescript)
function replaceErrors(key:any, value:any) {
if (value instanceof Error) {
var error : {[k: string]: any } = {};
Object.getOwnPropertyNames(value).forEach(function (key) {
error[key] = (value as {[k: string]: any })[key];
});
return error;
}
return value;
}
@lefam
Copy link
Author

lefam commented Nov 13, 2020

To use:

  try {
    // ...
  } catch(e) }
    console.log(JSON.stringify(e, replaceErrors));
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment