LF-3881 Return better errors by default from the API #3023
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Context
I wrongly maligned our logger, Winston, as responsible for eating the missing message and stack trace properties on the Error object, because I did not know that those two properties on JS Error objects were not enumerable! (See this StackOverflow: Winston not displaying error details)
The reason I thought Winston was responsible was the addition of the level and services properties after logging, and indeed Winston v3 does by design mutate the original Error object. See this issue on the Winston repo issue tracker -- sorry linking to a search to avoid automatic cross-linking to that repo 😁
Fixing our useless error messages
As of Winston v3, there is a built-in format called
error()
that parses the non-enumerable Error properties for use with the Winston transports.However, this does not make them available on the Error object for passing back in the response. To make message available to
.send({ error })
or.json({ error })
there are two options:Move from:
To:
This has the advantage of being explicit, but would require doing this in every controller.
server.js
) using one of the built-in Winston methods that has access toinfo
, the Winston reference to the Error object. I used a custom format function, but a custom transport could be used the same way.This will require
console.log(error)
orconsole.error(error)
to be invoked in eachcatch()
block before theres.send()
, but this needs to be done anyway for the error to be included in our logfiles so it should always be done.In this PR I therefore added it to the handful of catch blocks that were missing such logging. Although I didn't correct existing blocks, I added new logs at the error level (
console.error
), because this gives the messages the correct level to end up in the error log file, and makes more sense to me in a catch block.Bonus content
The ticket was given a bonus task of logging these catch-block caught errors to Sentry. It is absolutely possible, but since it requires iterative testing on beta I will give it its own ticket and branch.
Jira link: https://lite-farm.atlassian.net/browse/LF-3881
Type of change
How Has This Been Tested?
I created a Model Validation Error situation and also looked at the error from LF-3884, which is adding a task targeting a wild crop. I made sure the messages were now visible in
Checklist: