Skip to content

Commit

Permalink
Use errors.Is for nested error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
maxekman committed Jan 28, 2021
1 parent dc0f6b1 commit 59477d5
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion aggregatestore/model/aggregatestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewAggregateStore(repo eh.ReadWriteRepo, eventHandler eh.EventHandler) (*Ag
// Load implements the Load method of the eventhorizon.AggregateStore interface.
func (r *AggregateStore) Load(ctx context.Context, aggregateType eh.AggregateType, id uuid.UUID) (eh.Aggregate, error) {
item, err := r.repo.Find(ctx, id)
if rrErr, ok := err.(eh.RepoError); ok && rrErr.Err == eh.ErrEntityNotFound {
if errors.Is(err, eh.ErrEntityNotFound) {
// Create the aggregate.
if item, err = eh.CreateAggregate(aggregateType, id); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion examples/guestlist/domains/guestlist/projectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (p *GuestListProjector) HandleEvent(ctx context.Context, event eh.Event) er
// Load or create the guest list.
var g *GuestList
m, err := p.repo.Find(ctx, p.eventID)
if rrErr, ok := err.(eh.RepoError); ok && rrErr.Err == eh.ErrEntityNotFound {
if errors.Is(err, eh.ErrEntityNotFound) {
g = &GuestList{
ID: p.eventID,
}
Expand Down
3 changes: 2 additions & 1 deletion httputils/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package httputils

import (
"encoding/json"
"errors"
"net/http"
"path"

Expand Down Expand Up @@ -53,7 +54,7 @@ func QueryHandler(repo eh.ReadRepo) http.Handler {
}

if data, err = repo.Find(r.Context(), id); err != nil {
if rrErr, ok := err.(eh.RepoError); ok && rrErr.Err == eh.ErrEntityNotFound {
if errors.Is(err, eh.ErrEntityNotFound) {
http.Error(w, "could not find item", http.StatusNotFound)
return
}
Expand Down

0 comments on commit 59477d5

Please sign in to comment.