Improved error handling with pkg/errors
#108
Open
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.
This issue closes #107
Instead of using the default
errors
orfmt
package of golang, we are usingpkg/errors
.At a high level, the goals are to (1) capture the execution stack at the origin of a new error, (2) do not override the error stack while moving upstream and (3) add contextual messages wherever necessary.
Here are the new rules when dealing with error handling:
err
from a third party package (e.g.json.Marshal
) then there are two options: You can either (a) useerrors.Wrap(msg, err)
to wrap the err with an additional message and add a stack trace or (b) useerrors.WithStack(err)
to just add the stack. Do NOT just return back the error.medusa
then there are two options: You can either (a) useerrors.New(msg)
for an error that does not need any more parameters or (b)errors.Errorf(msg, args...)
for any errors that need parameters for more context.err
from a downstream source (either custom error or third-party) then there are three options: You can either (a) justreturn err
because you can assume that the error had a stack attached to it downstream, (b) useerrors.WithMessage(err, msg)
to add more context to the error with no additional args or (c)errors.WithMessagef(err, msg, args...)
to add more context and arguments.This is the first step towards a more robust error handling solution. The next step in this process is to create a standardized framework for error handling so that we have guidelines to identify when / where to contextualize an error versus just returning it directly.