-
Notifications
You must be signed in to change notification settings - Fork 46
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
errors: update documentation #598
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,35 @@ | ||
/* | ||
Package errors implements custom error interfaces for weave. | ||
|
||
The idea is to reuse as many errors from this package as possible and define custom package | ||
errors when absolutely necessary. It is best to define a new error here if you feel it's going to | ||
be somewhat package-agnostic. | ||
|
||
x/multisig is a good package to take a look at in terms of usage with predefined strings with/without formatting. | ||
x/validators and x/sigs define some custom errors. | ||
|
||
If you want to register a custom error - use Register(code, description). | ||
For reusing errors - use Errxxx.New and Errxxx.Newf. | ||
Code stands for ABCI error code, which allows to distinguish types of errors | ||
on the client side and act accordingly. | ||
|
||
There is also support for stacktraces. Please ensure you create the custom error using | ||
ErrXyz.New("...") or errors.Wrap(err, "...") at the point of creation to ensure we attach | ||
a stacktrace. If you wrap multiple times, we only record the first wrap with the stacktrace. | ||
(And don't do this as a global `var ErrFoo = errors.ErrNotFound.New("foo")` or you will get a | ||
useless stacktrace). | ||
|
||
Once you have an error, you can use `fmt.Printf/Sprintf` to get more context for the error | ||
%s is just the error message | ||
%+v is the full stack trace | ||
%v appends a compressed [filename:line] where the error was created | ||
(source is wrappedError.Format) | ||
Or call `err.StackTrace()` to get the raw call stack of the creation point | ||
Error declarations should be generic and cover broad range of cases. Each | ||
returned error instance can wrap a generic error declaration to provide more | ||
details. | ||
This package provides a broad range of errors declared that fits all common | ||
cases. If an error is very specific for an extension (ie ErrInvalidSequence in | ||
x/sigs) it can be registered outside of the errors package. Instead of | ||
registering an extension error consider registering it in the errors package. | ||
To create a new error istance use Register function. You must provide a unique, | ||
non zero error code and a short description, for example: | ||
|
||
var ErrZeroDivision = errors.Register(9241, "zero division") | ||
|
||
When returning an error, you can attach to it an additional context | ||
information by using Wrap function, for example: | ||
|
||
func safeDiv(val, div int) (int, err) { | ||
if div == 0 { | ||
return 0, errors.Wrapf(ErrZeroDivision, "cannot divide %d", val) | ||
} | ||
return val / div, nil | ||
} | ||
|
||
The first time an error instance is wrapped a stacktrace is attached as well. | ||
Stacktrace information can be printed using %+v and %v formats. | ||
|
||
%s is just the error message | ||
%+v is the full stack trace | ||
%v appends a compressed [filename:line] where the error was created | ||
|
||
*/ | ||
|
||
package errors |
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.
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.
I think nice to include this info (unless no longer valid). Possibly formatted differently
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.
Done in 5496a17