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 last connection error #4155

Closed
avalchev94 opened this issue Jan 15, 2021 · 1 comment
Closed

Wrap last connection error #4155

avalchev94 opened this issue Jan 15, 2021 · 1 comment
Assignees
Labels
Type: Feature New features or improvements in behavior

Comments

@avalchev94
Copy link

avalchev94 commented Jan 15, 2021

Hi all! First, I want to thank you for listening to the community and adding WithReturnConnectionError, it's really useful!
I have an idea how we can make it even better. Currently, the connection error's type is lost and only the error's text is used:

	conn, err = nil, fmt.Errorf("%v: %v", ctx.Err(), err)

At the end we have an error from type errors.errorString and the only way to check the underlying error is string comparison, which is unreliable.

Use case(s) - what problem will this feature solve?

  • checking error type
  • getting additional details from the custom error type

Proposed Solution

  1. Easiest solution is using the %w modifier, which wraps the error. Later, that error can be unwrapped with errors.Unwrap:
	conn, err = nil, fmt.Errorf("%v: %w", ctx.Err(), err)

However, from what I see in go.mod, the minimum supported go version is 1.11 and that's a problem, because above features were added in 1.13

  1. Creating custom error type which implements Unwrap interface from errors package. In that way:
  • if user is using version >=1.13, errors.Unwrap can be used directly;
  • if user is using version < 1.13, anonymous Unwrap interface can be defined and used for obtaining the underlying error;
 type wrapError struct {
     msg string
     err error
}

func (e *wrapError) Unwrap() error {
   return e.msg
}

// func (e *wrapError) Error() string

Sorry for the pseudo code, if that idea is approved, I can do a better version.

@dfawley
Copy link
Member

dfawley commented Mar 25, 2021

This is fixed.

@dfawley dfawley closed this as completed Mar 25, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Type: Feature New features or improvements in behavior
Projects
None yet
Development

No branches or pull requests

2 participants