Skip to content

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

Description

@lezh

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
		}
	}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions