yarpc / yarpc-go

A message passing platform for Go
MIT License
404 stars 101 forks source link

Allow wraping application error with yarpcerrors.Status to access error chain in middleware #2069

Closed kvuchkov closed 3 years ago

kvuchkov commented 3 years ago

Problem Statement

I want to register a YARPC middleware that implements non-functional error handling logic, common for all service handlers, while still providing the YARPC status from the concrete handler.

I noticed that yarpcerrors.Status implements the idiomatic error wrapping. However, I am unable to instantiate a yarpcerrors.Status while also settings the status code.

Proposal

We can expose a yarpcerrors.Wrap very similar to yarpcerrors.Newf that wraps an external error with a status code instead of creating a new error chain.

kvuchkov commented 3 years ago

One can actually useyarpcerrors.Newf(yarpcerrors.CodeInvalidArgument, "%w", e) to both wrap an internal error in YARPC status and set the desired code, since yarpcerrors.Newf uses fmt.Errorf internally when provided with arguments.

Thanks to @Dogild for the tip!