Skip to content
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

dex: general purpose go 1.13 error type #484

Merged
merged 1 commit into from
Jun 18, 2020
Merged

Conversation

chappjc
Copy link
Member

@chappjc chappjc commented Jun 15, 2020

Resolving #483

Based on the DetailedError type

// DetailedError pairs an Error with details.
type DetailedError struct {
wrapped error
detail string
}
// Error satisfies the error interface, combining the wrapped error message with
// the details.
func (e DetailedError) Error() string {
return e.wrapped.Error() + ": " + e.detail
}
// Unwrap returns the wrapped error, allowing errors.Is and errors.As to work.
func (e DetailedError) Unwrap() error {
return e.wrapped
}
// NewDetailedError wraps the provided Error with details in a DetailedError,
// facilitating the use of errors.Is and errors.As via errors.Unwrap.
func NewDetailedError(err error, detail string) DetailedError {
return DetailedError{
wrapped: err,
detail: detail,
}
}

Comment on lines +8 to +18
type testErr string

func (te testErr) Error() string {
return string(te)
}

const (
err0 = testErr("other error")
err1 = ErrorKind("error 1")
err2 = ErrorKind("error 2")
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Illustrating the purpose of ErrorKind (to avoid the boiler plate of creating a type that defines Error() string), while allowing a const, which errors.New prevents.

@chappjc chappjc marked this pull request as ready for review June 16, 2020 16:41
@chappjc
Copy link
Member Author

chappjc commented Jun 16, 2020

Ready for review, in concept at least, since the new types are not being applied repo-wide.

dex/asset.go Outdated Show resolved Hide resolved
dex/errors.go Show resolved Hide resolved
@chappjc chappjc requested a review from JoeGruffins June 16, 2020 16:54
@chappjc chappjc changed the title dex: DexError dex: general purpose go 1.13 error type Jun 16, 2020
Copy link
Member

@JoeGruffins JoeGruffins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this be used to clean up https://github.com/decred/dcrdex/blob/master/dex/msgjson/types.go ? Can this also help errors used with the rpc calls, or are int codes %100 necessary?

@JoeGruffins
Copy link
Member

If the only thing to wrap is a description, I think fmt.Errorf("%w: description", errFoo) is the same as NewError(errFoo, "description"), but I can see where having other fields would make a separate type needed.

@chappjc
Copy link
Member Author

chappjc commented Jun 17, 2020

Will this be used to clean up https://github.com/decred/dcrdex/blob/master/dex/msgjson/types.go ? Can this also help errors used with the rpc calls, or are int codes %100 necessary?

No, I think the msgjson package has different requirements, so I wouldn't change that.

@chappjc chappjc added server server components client labels Jun 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
server server components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants