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

@ -30,8 +30,6 @@ var _ = Service("inventory", func() {
Method("addItem", func() {
Payload(design.InventoryRecord)
Result(Empty)
Error("NotFound")
Error("BadRequest")
GRPC(func() {
Response(CodeOK)
@ -42,12 +40,10 @@ var _ = Service("inventory", func() {
Payload(design.InventoryRecord)
Result(Empty)
Error("NotFound")
Error("BadRequest")
GRPC(func() {
Response(CodeOK)
Response("NotFound", CodeNotFound)
Response("BadRequest", CodeInvalidArgument)
})
})
})

View File

@ -3,7 +3,7 @@ package inventoryapi
import (
"context"
inventory "crossnokaye-interview-assignment/services/inventory/gen/inventory"
"errors"
"fmt"
"log"
)
@ -40,8 +40,8 @@ func (s *inventorysrvc) RemoveItem(ctx context.Context, p *inventory.InventoryRe
itemList := (*s.inventories)[p.CharacterName]
if itemList == nil {
s.logger.Printf("inventory for character with id %d not found", p.CharacterName)
return errors.New("item not found")
s.logger.Printf("inventory for character with name '%s' not found", p.CharacterName)
return inventory.MakeNotFound(fmt.Errorf("inventory for character with name '%s' not found", p.CharacterName))
}
idx := indexOf(p.ItemName, *itemList)
@ -49,8 +49,9 @@ func (s *inventorysrvc) RemoveItem(ctx context.Context, p *inventory.InventoryRe
newItemList := remove(*itemList, idx)
(*s.inventories)[p.CharacterName] = &newItemList
} else {
s.logger.Printf("character with id %d does not have item %d in inventory", p.CharacterName, p.ItemName)
return errors.New("item not found for character")
s.logger.Printf("character with name '%s' does not have item '%s' in inventory", p.CharacterName, p.ItemName)
return inventory.MakeNotFound(fmt.Errorf("character with name '%s' does not have item '%s' in inventory",
p.CharacterName, p.ItemName))
}
return