-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
caddyfile: keep error chain info in Dispenser.Errf #4233
Conversation
Thanks for this. What is the use case for this? (There's no context / issue associated.) And why do we have to define our own error type instead of using |
This API In the codebase at this time, I returned an error object as follows: func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
// ...
return nil, h.Errf("invalid sign_key")
} And in the corresponding test, I wrote: func TestParsingCaddyfileError(t *testing.T) {
// ...
_, err := parseCaddyfile(helper)
assert.Contains(t, err.Error(), "sign_key") // fragile, broken when error content changed
} Assertion statements checking some error information by string comparison can be fragile. If var ErrInvalidSignKey = errors.New("invalid sign_key")
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
// ...
return nil, h.Errf("%w", ErrInvalidSignKey)
}
func TestParsingCaddyfileError(t *testing.T) {
// ...
_, err := parseCaddyfile(helper)
assert.ErrorIs(t, err, ErrInvalidSignKey) // more stable
}
|
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.
Gotcha, thanks for the explanation.
If using %w would achieve the same result, I think I'd rather use that just for simplicity's sake; I'm not sure what about that is harder to read than having a whole new type defintion to manually wrap the error. But if this is the only way to get the result you want, then that's OK I guess.
caddyconfig/caddyfile/dispenser.go
Outdated
// Err generates a custom parse-time error with a message of msg. | ||
func (d *Dispenser) Err(msg string) error { | ||
msg = fmt.Sprintf("%s:%d - Error during parsing: %s", d.File(), d.Line(), msg) | ||
return errors.New(msg) | ||
return parseError{d.File(), d.Line(), errors.New(msg)} |
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.
If Err could use Errf, or vice-versa, that might be nice to reduce a little bit of code duplication / repetition.
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 refactored it and I think it's more concise now.
68eb732
to
b4efccc
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.
LGTM now -- thanks!
Use
fmt.Errorf
instead offmt.Sprintf
inDispenser.Errf
method to reserve the error chain information. Make the underlying error unwrapable by usingerrors.Is
orerrors.As
helpers.