Using generated error types

This commit is contained in:
Brandon Watson
2023-08-15 14:45:40 -05:00
parent 3511a4f022
commit 9dc9e6b031
7 changed files with 45 additions and 73 deletions

View File

@ -3,7 +3,7 @@ package itemapi
import (
"context"
item "crossnokaye-interview-assignment/services/item/gen/item"
"errors"
"fmt"
"log"
)
@ -26,8 +26,8 @@ func (s *itemsrvc) GetItem(ctx context.Context, p *item.GetItemPayload) (res *it
itemToGet := (*s.items)[*p.Name]
if itemToGet == nil {
s.logger.Printf("item with id %d not found", &p.Name)
return nil, errors.New("item not found")
s.logger.Printf("item with name '%s' not found", *p.Name)
return nil, item.MakeNotFound(fmt.Errorf("item with name '%s' not found", *p.Name))
}
res = (*s.items)[*p.Name]
@ -55,8 +55,8 @@ func (s *itemsrvc) CreateItem(ctx context.Context, p *item.Item) (res *item.Item
existingItem := (*s.items)[name]
if existingItem != nil {
s.logger.Printf("item with name %d already exists", &p.Name)
return nil, errors.New("item already exists")
s.logger.Printf("item with name '%s' already exists", p.Name)
return nil, item.MakeAlreadyExists(fmt.Errorf("item with name '%s' already exists", p.Name))
}
(*s.items)[name] = p
@ -70,8 +70,8 @@ func (s *itemsrvc) UpdateItem(ctx context.Context, p *item.Item) (res *item.Item
s.logger.Print("itemToGet.updateItem")
itemToUpdate := (*s.items)[p.Name]
if itemToUpdate == nil {
s.logger.Printf("item with id %d not found", &p.Name)
return nil, errors.New("item not found")
s.logger.Printf("item with name '%s' not found", p.Name)
return nil, item.MakeNotFound(fmt.Errorf("item with name '%s' not found", p.Name))
}
(*s.items)[p.Name] = p
res = (*s.items)[p.Name]
@ -84,11 +84,11 @@ func (s *itemsrvc) DeleteItem(ctx context.Context, p *item.DeleteItemPayload) (e
s.logger.Print("item.deleteItem")
itemToDelete := (*s.items)[*p.Name]
if itemToDelete == nil {
s.logger.Printf("item with id %d not found", &p.Name)
return errors.New("item not found")
s.logger.Printf("item with name '%s' not found", *p.Name)
return item.MakeNotFound(fmt.Errorf("item with name '%s' not found", *p.Name))
}
delete((*s.items), *p.Name)
delete(*s.items, *p.Name)
return
}