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

Wrap internal typed errors to help the users better handle errors #15

Open
lezh opened this issue Apr 23, 2021 · 0 comments
Open

Wrap internal typed errors to help the users better handle errors #15

lezh opened this issue Apr 23, 2021 · 0 comments

Comments

@lezh
Copy link
Contributor

lezh commented Apr 23, 2021

This library was created before Go 1.13 where error wrapping was introduced. So, it did not wrap errors, which made error handling at the caller side a bit tricky.

For instance, when opening an unknown bucket, the HTTP 404 error was replaced by a custom "Unknown bucket" error.
https://github.com/jacobsa/gcloud/blob/master/gcs/conn.go#L191-L192

		case http.StatusNotFound:
			err = fmt.Errorf("Unknown bucket %q", b.Name())

This error is hard to be handled by the caller. However, if we wrap the internal http error like this:

		case http.StatusNotFound:
			err = fmt.Errorf("Unknown bucket %q: %w", b.Name(), err)

On the user side, such error can be casted back to an 404, and properly handled:

	if errors.As(err, &apiErr) {
		switch apiErr.Code {
		case http.StatusNotFound:
			return syscall.EINVAL
		}
	}
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

No branches or pull requests

1 participant