-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stringify-error: allows stringify on error objects #21
Conversation
src/utils.js
Outdated
const stringify = msg => { | ||
if (R.is(String, msg)) return msg | ||
return JSON.stringify(msg) | ||
const buildErrorObject = ({ message, stack }) => ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use
const buildErrorObject = pick(['message', 'stack'])
src/utils.js
Outdated
|
||
const stringify = log => { | ||
if (R.is(String, log)) return log | ||
if (log.message instanceof Error) log.message = buildErrorObject(log.message) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use is(Error, log.message)
src/utils.js
Outdated
} | ||
|
||
const pickProperties = (message, propsToLog) => ( | ||
pokeprop(propsToLog, message) | ||
const pickProperties = (log, propsToLog) => ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use R.flip here
const pickProperties = flip(pokeprop)
src/utils.js
Outdated
) | ||
|
||
const stringify = log => { | ||
if (R.is(String, log)) return log |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove R.
to improve readability by using destructuring on require
const { is } = require('ramda')
src/utils.js
Outdated
const stringify = log => { | ||
if (R.is(String, log)) return log | ||
if (log.message instanceof Error) log.message = buildErrorObject(log.message) | ||
return JSON.stringify(log) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(optional) Go full ramda!
const stringifyMessage = ifElse(
is(Error),
pick(['message', 'stack']),
identity
)
const stringify = ifElse(
is(String),
identity,
pipe(
over(lensProp('message'), stringifyMessage),
JSON.stringify
)
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dá para simplificar com when
e unless
:)
const stringifyMessage = when(
is(Error),
pick(['message', 'stack'])
)
const stringify = unless(
is(String),
pipe(
over(lensProp('message'), stringifyMessage),
JSON.stringify
)
)
2e6c2c3
to
bb2f5db
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nao acha mais limpo importar o ramda desestruturado?
Sim, vou fazer isso em seguida em um PR de refactoring. Tenho que tirar os parênteses dos retornos implícitos também. |
f99e157
to
99c97e6
Compare
Changes Unknown when pulling 99c97e6 on fix/stringify-error-object into ** on master**. |
The native method JSON.stringify doesn't stringify properties from prototypes. For this reason we need to check if a object is a instance of an error, clone it to flat its props to finally use stringify.
Add tests to check if strinfigy can handle error object
99c97e6
to
01aa989
Compare
Changes Unknown when pulling 01aa989 on fix/stringify-error-object into ** on master**. |
closes #19