Skip to content

Instantly share code, notes, and snippets.

@yadomi
Created March 9, 2020 13:03
Show Gist options
  • Save yadomi/662e7572e1bad4af8844b3cabbf09017 to your computer and use it in GitHub Desktop.
Save yadomi/662e7572e1bad4af8844b3cabbf09017 to your computer and use it in GitHub Desktop.
Error to key value message
const escape = value => {
const hasSpace = /\s/.test(value);
const hasNewLine = /\\n/.test(value);
return hasSpace || hasNewLine ? JSON.stringify(value) : value
}
const toKeyValue = (key, value) => {
return `${key}=${escape(value)}`
}
const toString = err => {
const data = [
toKeyValue('time', new Date().toISOString()),
toKeyValue('type', 'error'),
toKeyValue('name', err.name),
toKeyValue('message', err.message),
toKeyValue('stack', err.stack),
]
return data.join(' ')
}
process.on('uncaughtException', (err) => {
const log = toString(err);
console.log(log);
});
function main () {
const a = 42;
a = 69; // to test error
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment