-
Notifications
You must be signed in to change notification settings - Fork 700
PROPOSAL: Add support for domain errors #130
Comments
Use Cause, that's why it's there.
…On Wed, 30 Aug 2017, 18:00 José Carlos Chávez ***@***.***> wrote:
As of now, when wrapping an error we keep the cause of the error meaning
that if we want to handle the error in a upper layer we need to know
details about the lower layers. For example:
package users
var ErrUserDoesNotExist = errors.New("user does not exist")
type Repository interface {
GetUserById(id string) (*users.User, error)
}
package mysql
type mysqlRepository struct {
ds datastore
}
func (mr *mysqlRepository) GetUserById (id string) (*users.User, error) {
record, err := ds.exec(fmt.Sprintf("SELECT * FROM `users` WHERE id = %s", id))
if err != nil {
return nil, errors.Wrap(err, "could not retrieve user from persistence")
// another option is to return ErrUserDoesNotExist
}
}
Meaning that if I want to deal with the error, the handler should do
something like:
package handlers
func (h *handler) UserProfile (c web.C, w http.ResponseWriter, r *http.Request) {
uid := c.URLParams["uid"]
u, err := h.userRepository.GetUserById(uid)
*Option 1:*
The handler should now about the implementation details of the database.
if err.Cause() == mysql.NoRecordsFound {
w.WriteHeader(http.StatusNotFound)
}
*Option 2:*
We do a string comparation
if err.Error() == "could not retrieve user from persistence"{
w.WriteHeader(http.StatusNotFound)
}
IMO this could be solved with another function:
func Map(causeErr error, meaningErr error) error {
if causeErr == nil {
return nil
}
err := &withDecoration{
cause: causeErr,
meaning: meaningErr,
}
return &withStack{
err,
callers(),
}
}
*Option 3:*
We do a string comparation
if err.Meaning() == ErrUserDoesNotExist {
w.WriteHeader(http.StatusNotFound)
}
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#130>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAAcA-2PLmsoVNPeyMil1xNOOEADdcuLks5sdRaZgaJpZM4PHAba>
.
|
Side note: Also the way |
I agree with the spirit of https://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully 's "Assert errors for behaviour, not type" section. but in practice, creating an interface for each category of errors is a bit too onerous :( I found a comfortable middle ground that's more or less in the spirit of @jcchavezs' proposal in the sense that you can still get the domain error (I have named it would that be something that could find its way into this repository? |
With the go team developing their own errors package, it’s unlikely this feature would be added to this package.
You can just use omegaups version, they are already building on this package.
… On 17 Dec 2018, at 12:05, lhchavez ***@***.***> wrote:
I agree with the spirit of https://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully 's "Assert errors for behaviour, not type" section. but in practice, creating an interface for each category of errors is a bit too onerous :( I found a comfortable middle ground that's more or less in the spirit of @jcchavezs' proposal in the sense that you can still get the domain error (I have named it Category inspired by C++), and it does provide Cause: https://github.com/omegaup/go-base/blob/master/errors.go
would that be something that could find its way into this repository?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
|
FYI we have built domain errors in https://github.com/cockroachdb/errors |
As of now, when wrapping an error we keep the cause of the error meaning that if we want to handle the error in a upper layer we need to know details about the lower layers. For example:
Meaning that if I want to deal with the error, the handler should do something like:
Option 1:
The handler should now about the implementation details of the database.
Option 2:
We do a string comparation
IMO this could be solved with another function:
Option 3:
We do a string comparation
I could open a PR for this.
The text was updated successfully, but these errors were encountered: