Skip to content

Instantly share code, notes, and snippets.

@mk-pmb
Last active November 9, 2019 16:06
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 mk-pmb/07f2f1674e99882bbd63ac8fcc04b2b9 to your computer and use it in GitHub Desktop.
Save mk-pmb/07f2f1674e99882bbd63ac8fcc04b2b9 to your computer and use it in GitHub Desktop.
// -*- 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