Created
March 9, 2020 13:03
-
-
Save yadomi/662e7572e1bad4af8844b3cabbf09017 to your computer and use it in GitHub Desktop.
Error to key value message
This file contains 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
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