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

fix: custom error wrapped around error and commitment meta #134

Merged
merged 4 commits into from
Sep 20, 2024

Conversation

hopeyen
Copy link
Collaborator

@hopeyen hopeyen commented Sep 19, 2024

Added a HandlerError type to contain error and commitment meta (mode and version) for metrics

@hopeyen hopeyen requested a review from samlaf September 19, 2024 10:55
@hopeyen hopeyen force-pushed the hope/metrics-and-status-code-fix branch from 1b63a97 to 89078cd Compare September 19, 2024 12:23
Copy link
Collaborator

@samlaf samlaf left a comment

Choose a reason for hiding this comment

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

I like this! Just leaving a few nits

server/server_test.go Outdated Show resolved Hide resolved
server/server.go Outdated
Comment on lines 69 to 76
if err.Err != nil {
if err.Meta != nil {
recordDur(w.Header().Get("status"), string(err.Meta.Mode), string(err.Meta.CertVersion))
} else {
recordDur(w.Header().Get("status"), string("NoCommitmentModeSet"), string("NoCertVersionSet"))
}
return err.Err
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

when can the meta not be set? Would be good to document this.
Actually thinking more not a big fan of this. We should only return a metaError if the meta fields are set. If they aren't then we can return a simple error. And then we can switch on the error type instead of doing "duck-typing" like this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was thinking this would take care of the case when readCommitmentMode has returned error, but I realized that now that commitmentMeta {} won't match to nil:(
I've taken a different approach now by matching like if meta, ok := err.(MetaError); ok {...}. let me know if you think it could be improved further

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yep this is almost there! Just want to use go's native error library's is function (https://pkg.go.dev/errors#Is) instead of type checking like this, which allows us to wrap a MetaError in another error if ever we need that, and will still work.

I recommend https://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully if you want to learn more about golang's error handling philosophy. :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thank you for the resources! Isn't the current errors.As an native error function just like errors.Is? My interpretation between these two options were that errors.Is checks for exact values of the error while errors.As checks for the type of the error, and both options are recursive for wrapped types.

Using As made more sense to me, but per your suggestion, if I were to move from As to Is, it seems I will have to define a function like func (m MetaError) Is(target error) bool { return target.Err != nil && target.Meta != nil}. is this what you had in mind?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh nvm errors.As is the right option here. errors.Is/As are the same, just that is only checks the type, whereas As unwraps it into a struct that is then usable. Since we want to use that struct, As is perfect here. I was probably still looking at the old code that was just type casting instead of using errors.As.

server/server.go Outdated Show resolved Hide resolved
server/server.go Outdated Show resolved Hide resolved
server/server.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@samlaf samlaf left a comment

Choose a reason for hiding this comment

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

Thanks for the changes! Almost there, just a few more things to follow golang's error standards, and then we're good to merge :)

server/errors.go Outdated Show resolved Hide resolved
server/errors.go Outdated Show resolved Hide resolved
server/server.go Outdated
Comment on lines 69 to 76
if err.Err != nil {
if err.Meta != nil {
recordDur(w.Header().Get("status"), string(err.Meta.Mode), string(err.Meta.CertVersion))
} else {
recordDur(w.Header().Get("status"), string("NoCommitmentModeSet"), string("NoCertVersionSet"))
}
return err.Err
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yep this is almost there! Just want to use go's native error library's is function (https://pkg.go.dev/errors#Is) instead of type checking like this, which allows us to wrap a MetaError in another error if ever we need that, and will still work.

I recommend https://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully if you want to learn more about golang's error handling philosophy. :)

Copy link
Collaborator

@samlaf samlaf left a comment

Choose a reason for hiding this comment

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

Thanks for these changes :) Good to merge

@hopeyen hopeyen merged commit 39a9758 into main Sep 20, 2024
7 checks passed
@hopeyen hopeyen deleted the hope/metrics-and-status-code-fix branch September 20, 2024 18:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants