Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Typed error for Not Found (404) responses #163

Merged
merged 2 commits into from
Oct 4, 2023
Merged

Typed error for Not Found (404) responses #163

merged 2 commits into from
Oct 4, 2023

Conversation

joanlopez
Copy link
Contributor

From the library's consumer point of view, the error for all HTTP responses where status code is >= 400 look the same. However, certain errors, like Not Found (404) require specific error handling (for instance, from Grizzly), and with the current approach that would require parsing the error message (bad idea!).

So, here I suggest to create a typed error for this specific case. We can add more typed errors later, if needed.

I don't think exposing this will hurt, but if there's any concern, I'd be happy to expose it as a behavioral error.

type errNotFound interface {
        NotFound() bool
}
 
// IsErrNotFound returns true if err is not found.
func IsErrNotFound(err error) bool {
        nf, ok := err.(errNotFound)
        return ok && nf.NotFound()
}

Thanks!

Copy link
Contributor

@inkel inkel left a comment

Choose a reason for hiding this comment

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

I love the idea! And I believe it will definitely be very useful. I'm approving because it LGTM to me as it is, but I've left some suggestions that would make it even better IMNSHO.

errors.go Outdated Show resolved Hide resolved
errors.go Outdated
}

func (e ErrNotFound) Error() string {
return fmt.Sprintf("status: %d, body: %v", e.StatusCode, string(e.BodyContents))
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be simplified:

Suggested change
return fmt.Sprintf("status: %d, body: %v", e.StatusCode, string(e.BodyContents))
return fmt.Sprintf("status: %d, body: %s", e.StatusCode, e.BodyContents)

Which when combined with my other comment, would end up being:

Suggested change
return fmt.Sprintf("status: %d, body: %v", e.StatusCode, string(e.BodyContents))
return fmt.Sprintf("status: 404 Not Found, body: %s", e.BodyContents)

I honestly would even remove the status: 404 prefix, however, that would break backwards compatibility with consumers of this package that are looking for the prefix (like here).

Copy link
Contributor Author

@joanlopez joanlopez Oct 4, 2023

Choose a reason for hiding this comment

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

Thanks @inkel, I applied your feedback at 110dc04, because it's indeed a nice idea!

However, I think there was an extra "Not Found" in your last suggestion, cause the former error was just printing the status code (integer), so I've just added the 404.

Could you confirm it looks good now, please? Thanks! 🙏🏻

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice, thanks! Yeah, the extra not found was there on purpose, so in case the user prints the error message it shows indeed that it's not found (status: 404 says the same, but you require some technical knowledge); but I'm ok without having it in the message for now. Re-approving and merging!

@joanlopez joanlopez requested a review from inkel October 4, 2023 08:54
Co-authored-by: Leandro López <leandro.lopez@grafana.com>
@inkel inkel merged commit 393a4f4 into master Oct 4, 2023
2 checks passed
@inkel inkel deleted the err-not-found branch October 4, 2023 13:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
2 participants