You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems there is a convention in Go for temporary errors:
interface {
Temporary() bool
}
However there is no "recommended" way to know if an error is temporary (or not) if it's wrapped.
In my application, I'm using this helper function:
// IsTemporary returns true if an error is temporary, false otherwise.// By default, an error is temporary.funcIsTemporary(errerror) bool {
varwerrinterface {
Temporary() bool
}
ok:=As(err, &werr)
ifok {
returnwerr.Temporary()
}
returntrue
}
It looks for the first error in the chain that implements the interface, and return the result of the method call
If no error implement the interface, it returns true, which means that all errors are temporary by default (which seems a good default from my point of view)
I encountered a very strange issue related to this.
In my application, I'm doing an outgoing HTTP request, which may result in a i/o timeout error.
Usually, the chain of error types is:
*net.timeoutError => always return true
net.*OpError => in this case it returns the same result as the wrapped error
*url.Error => returns the same result as the wrapped error
In *url.Error, the logic is implemented like this:
So, if the wrapped error doesn't implement the interface, it returns false.
But in my application, I've got a custom "transport", which wraps the error between net.*OpError and *url.Error.
My custom error type implements the Unwrap() error interface, however it doesn't implement the Temporary() bool interface.
So, even if net.*OpError.Temporary() returns true, *url.Error.Temporary() will return false, because my custom error type doesn't implement Temporary() bool.
What is the correct way of managing temporary errors ?
Does my helper function IsTemporary() make sense ? Is it OK to consider that all errors are temporary by default ?
Personally I think that error types like *url.Error shouldn't implement Temporary() bool at all, and we should simply use errors.As()as I did (but I guess it's too late to change it).
What should I do in my application ?
not wrap the error between net.*OpError and *url.Error ?
implement Temporary() bool in my custom error type, in the same way as *url.Error ? (which implies that I should do the same in all custom error types across all my projects) ?
The text was updated successfully, but these errors were encountered:
However there is no "recommended" way to know if an error is temporary (or not) if it's wrapped.
url.Error.Temporary() assumes that the wrapped error implements the Temporary() bool interface. (e.g. net.OpError)
However if a custom transport/round tripper wraps the error, the value returned by url.Error.Temporary() is wrong.
It seems there is a convention in Go for temporary errors:
However there is no "recommended" way to know if an error is temporary (or not) if it's wrapped.
In my application, I'm using this helper function:
I encountered a very strange issue related to this.
In my application, I'm doing an outgoing HTTP request, which may result in a
i/o timeout
error.Usually, the chain of error types is:
*net.timeoutError
=> always returntrue
net.*OpError
=> in this case it returns the same result as the wrapped error*url.Error
=> returns the same result as the wrapped errorIn
*url.Error
, the logic is implemented like this:So, if the wrapped error doesn't implement the interface, it returns
false
.But in my application, I've got a custom "transport", which wraps the error between
net.*OpError
and*url.Error
.My custom error type implements the
Unwrap() error
interface, however it doesn't implement theTemporary() bool
interface.So, even if
net.*OpError.Temporary()
returnstrue
,*url.Error.Temporary()
will returnfalse
, because my custom error type doesn't implementTemporary() bool
.What is the correct way of managing temporary errors ?
Does my helper function
IsTemporary()
make sense ? Is it OK to consider that all errors are temporary by default ?Personally I think that error types like
*url.Error
shouldn't implementTemporary() bool
at all, and we should simply useerrors.As()
as I did (but I guess it's too late to change it).What should I do in my application ?
net.*OpError
and*url.Error
?Temporary() bool
in my custom error type, in the same way as*url.Error
? (which implies that I should do the same in all custom error types across all my projects) ?The text was updated successfully, but these errors were encountered: