Last active
November 9, 2019 16:06
-
-
Save mk-pmb/07f2f1674e99882bbd63ac8fcc04b2b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -*- coding: utf-8, tab-width: 2 -*- | |
// https://github.com/mk-pmb/is-error-js/issues/5 | |
const isError = require('is-error'); | |
const FakeError = function() { | |
this[Symbol.toStringTag] = 'Error'; | |
}; | |
function preview(x) { | |
if (!x) { return x; } | |
if (!x.slice) { return x; } | |
return x.slice(0, 64).concat('…'); | |
} | |
function inspect(x) { | |
const { | |
name, | |
message, | |
stack, | |
} = x; | |
console.log({ | |
obj2str: Object.prototype.toString.call(x), | |
isError: isError(x), | |
keys: Object.keys(x), | |
message, | |
name, | |
stack: preview(stack), | |
}); | |
} | |
inspect(new FakeError('fake')); | |
inspect(new Error('legit')); | |
// { obj2str: '[object Error]', | |
// isError: true, | |
// keys: [], | |
// message: undefined, | |
// name: undefined, | |
// stack: undefined } | |
// { obj2str: '[object Error]', | |
// isError: true, | |
// keys: [], | |
// message: 'legit', | |
// name: 'Error', | |
// stack: 'Error: legit\n at Object.<anonymous> (/tmp/test.js:34:9)\n a…' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment