> On 2022. Oct 27., at 20:16, Konstantin Khomoutov <kos...@bswap.ru> wrote:
> 
> On Thu, Oct 27, 2022 at 11:01:01AM -0700, Gergely Brautigam wrote:
> 
> [...]
>>> Since Go 1.13 you can also make chains of errors; say, in your second 
>>> example 
>>> you could do 
>>> 
>>> err = fmt.Errorf("doSomething failed with %s while handling %w", 
>>> tmpErr, err) 
>>> 
>>> and then have callers use errors.Is / errors.As or whatever other approach 
>>> to 
>>> learn whether the error of their interest is in the error chain. 
>>> 
>>> Note that in ether approach with returning multiple errors, the callers 
>>> are to 
>>> be prepared to handle multiple errors; otherwise it's engineering for the 
>>> sake 
>>> of engineering which leads to nothing more than code which is convoluted 
>>> for 
>>> nothing. 
> [...]
> 
>> Interesting, the chaining is a nice idea. However, wouldn't that mean that 
>> I would still have to errors.Is a specific error. So I would have to handle 
>> both. Which is fine ofc. Just saying. :)
> 
> Chaining is good in the sense you can still consider a chain to be a single
> error, and only inspect the chain when there is need for that.
> 
> The usual case for this approach is that when some operation involving
> multiple nested calls happens, some deep one fails, then each level wraps the
> error in its own context as the error bubbles up the stack.
> Then at the call site you have an error with properly nested contexts in the
> message - like in "failed to X: error frobincating Y: fizzling of bar
> failed: Z: error while fooing quux: ... and so on", and you can just log it
> and have a full actionable error message in the logs. And at the same time
> you can inspect the chain if you need special processing in case, say,
> fooing of quux failed, but not in other cases. With chaining, you can do this,
> while still passing the whole chain around where the rest of the code does not
> care about whether it is really a chain or a "simple" error.

Nice! I really like this idea. :) Thank you very much for sharing it. :)

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/E76397EE-A9A9-4BF3-B7DF-DC9DF65E25F8%40gmail.com.

Reply via email to