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 characterapi
import ( import (
"context" "context"
character "crossnokaye-interview-assignment/services/character/gen/character" character "crossnokaye-interview-assignment/services/character/gen/character"
"errors" "fmt"
"log" "log"
) )
@ -26,8 +26,8 @@ func (s *charactersrvc) GetCharacter(ctx context.Context, p *character.GetCharac
itemToGet := (*s.characters)[*p.Name] itemToGet := (*s.characters)[*p.Name]
if itemToGet == nil { if itemToGet == nil {
s.logger.Printf("character with id %d not found", &p.Name) s.logger.Printf("character with id '%s' not found", *p.Name)
return nil, errors.New("character not found") return nil, character.MakeNotFound(fmt.Errorf("character with id '%s' not found", *p.Name))
} }
res = (*s.characters)[*p.Name] res = (*s.characters)[*p.Name]
@ -47,8 +47,8 @@ func (s *charactersrvc) CreateCharacter(ctx context.Context, p *character.Charac
existingCharacter := (*s.characters)[p.Name] existingCharacter := (*s.characters)[p.Name]
if existingCharacter != nil { if existingCharacter != nil {
s.logger.Printf("character with name %d already exists", &p.Name) s.logger.Printf("character with name %s already exists", p.Name)
return nil, errors.New("character already exists") return nil, character.MakeAlreadyExists(fmt.Errorf("character with name %s already exists", p.Name))
} }
(*s.characters)[p.Name] = p (*s.characters)[p.Name] = p
@ -62,8 +62,8 @@ func (s *charactersrvc) UpdateCharacter(ctx context.Context, p *character.Charac
itemToUpdate := (*s.characters)[p.Name] itemToUpdate := (*s.characters)[p.Name]
if itemToUpdate == nil { if itemToUpdate == nil {
s.logger.Printf("characters with id %d not found", &p.Name) s.logger.Printf("character with id '%s' not found", p.Name)
return nil, errors.New("characters not found") return nil, character.MakeNotFound(fmt.Errorf("character with id '%s' not found", p.Name))
} }
(*s.characters)[p.Name] = p (*s.characters)[p.Name] = p
res = (*s.characters)[p.Name] res = (*s.characters)[p.Name]
@ -77,11 +77,11 @@ func (s *charactersrvc) DeleteCharacter(ctx context.Context, p *character.Delete
itemToDelete := (*s.characters)[*p.Name] itemToDelete := (*s.characters)[*p.Name]
if itemToDelete == nil { if itemToDelete == nil {
s.logger.Printf("characters with id %d not found", &p.Name) s.logger.Printf("character with id '%s' not found", *p.Name)
return errors.New("characters not found") return character.MakeNotFound(fmt.Errorf("character with id '%s' not found", *p.Name))
} }
delete((*s.characters), *p.Name) delete(*s.characters, *p.Name)
return return
} }

View File

@ -24,12 +24,10 @@ var _ = Service("character", func() {
}) })
Result(Character) Result(Character)
Error("NotFound") Error("NotFound")
Error("BadRequest")
GRPC(func() { GRPC(func() {
Response(CodeOK) Response(CodeOK)
Response("NotFound", CodeNotFound) Response("NotFound", CodeNotFound)
Response("BadRequest", CodeInvalidArgument)
}) })
}) })
@ -45,13 +43,13 @@ var _ = Service("character", func() {
Method("createCharacter", func() { Method("createCharacter", func() {
Payload(Character) Payload(Character)
Result(Character) Result(Character)
Error("BadRequest")
Error("NotFound") Error("NotFound")
Error("AlreadyExists")
GRPC(func() { GRPC(func() {
Response(CodeOK) Response(CodeOK)
Response("NotFound", CodeNotFound) Response("NotFound", CodeNotFound)
Response("BadRequest", CodeInvalidArgument) Response("AlreadyExists", CodeAlreadyExists)
}) })
}) })
@ -59,12 +57,10 @@ var _ = Service("character", func() {
Payload(Character) Payload(Character)
Result(Character) Result(Character)
Error("NotFound") Error("NotFound")
Error("BadRequest")
GRPC(func() { GRPC(func() {
Response(CodeOK) Response(CodeOK)
Response("NotFound", CodeNotFound) Response("NotFound", CodeNotFound)
Response("BadRequest", CodeInvalidArgument)
}) })
}) })
@ -74,12 +70,10 @@ var _ = Service("character", func() {
}) })
Result(Empty) Result(Empty)
Error("NotFound") Error("NotFound")
Error("BadRequest")
GRPC(func() { GRPC(func() {
Response(CodeOK) Response(CodeOK)
Response("NotFound", CodeNotFound) Response("NotFound", CodeNotFound)
Response("BadRequest", CodeInvalidArgument)
}) })
}) })
}) })

View File

@ -27,8 +27,7 @@ var _ = Service("front", func() {
HTTP(func() { HTTP(func() {
GET("/item/{name}") GET("/item/{name}")
Response(StatusOK) Response(StatusOK)
Response(StatusBadRequest) Response("NotFound", StatusNotFound)
Response(StatusNotFound)
}) })
}) })
@ -39,20 +38,18 @@ var _ = Service("front", func() {
HTTP(func() { HTTP(func() {
GET("/item") GET("/item")
Response(StatusOK) Response(StatusOK)
Response(StatusBadRequest)
Response(StatusNotFound)
}) })
}) })
Method("createItem", func() { Method("createItem", func() {
Payload(Item) Payload(Item)
Result(Item) Result(Item)
Error("BadRequest") Error("AlreadyExists")
HTTP(func() { HTTP(func() {
POST("/item") POST("/item")
Response(StatusOK) Response(StatusOK)
Response(StatusBadRequest) Response("AlreadyExists", StatusConflict)
}) })
}) })
@ -60,13 +57,11 @@ var _ = Service("front", func() {
Payload(Item) Payload(Item)
Result(Item) Result(Item)
Error("NotFound") Error("NotFound")
Error("BadRequest")
HTTP(func() { HTTP(func() {
PUT("/item/{name}") PUT("/item/{name}")
Response(StatusOK) Response(StatusOK)
Response(StatusBadRequest) Response("NotFound", StatusNotFound)
Response(StatusNotFound)
}) })
}) })
@ -74,13 +69,11 @@ var _ = Service("front", func() {
Payload(String) Payload(String)
Result(Empty) Result(Empty)
Error("NotFound") Error("NotFound")
Error("BadRequest")
HTTP(func() { HTTP(func() {
DELETE("/item/{name}") DELETE("/item/{name}")
Response(StatusOK) Response(StatusOK)
Response(StatusBadRequest) Response("NotFound", StatusNotFound)
Response(StatusNotFound)
}) })
}) })
@ -88,13 +81,11 @@ var _ = Service("front", func() {
Payload(String) Payload(String)
Result(Character) Result(Character)
Error("NotFound") Error("NotFound")
Error("BadRequest")
HTTP(func() { HTTP(func() {
GET("/character/{name}") GET("/character/{name}")
Response(StatusOK) Response(StatusOK)
Response(StatusBadRequest) Response("NotFound", StatusNotFound)
Response(StatusNotFound)
}) })
}) })
@ -105,22 +96,18 @@ var _ = Service("front", func() {
HTTP(func() { HTTP(func() {
GET("/character") GET("/character")
Response(StatusOK) Response(StatusOK)
Response(StatusBadRequest)
Response(StatusNotFound)
}) })
}) })
Method("createCharacter", func() { Method("createCharacter", func() {
Payload(Character) Payload(Character)
Result(Character) Result(Character)
Error("BadRequest")
Error("NotFound") Error("NotFound")
HTTP(func() { HTTP(func() {
POST("/character") POST("/character")
Response(StatusBadRequest) Response(StatusBadRequest)
Response(StatusOK) Response("NotFound", StatusNotFound)
Response(StatusInternalServerError)
}) })
}) })
@ -128,13 +115,11 @@ var _ = Service("front", func() {
Payload(Character) Payload(Character)
Result(Character) Result(Character)
Error("NotFound") Error("NotFound")
Error("BadRequest")
HTTP(func() { HTTP(func() {
PUT("/character/{name}") PUT("/character/{name}")
Response(StatusOK) Response(StatusOK)
Response(StatusBadRequest) Response("NotFound", StatusNotFound)
Response(StatusNotFound)
}) })
}) })
@ -142,13 +127,11 @@ var _ = Service("front", func() {
Payload(String) Payload(String)
Result(Empty) Result(Empty)
Error("NotFound") Error("NotFound")
Error("BadRequest")
HTTP(func() { HTTP(func() {
DELETE("/character/{name}") DELETE("/character/{name}")
Response(StatusOK) Response(StatusOK)
Response(StatusBadRequest) Response("NotFound", StatusNotFound)
Response(StatusNotFound)
}) })
}) })
@ -156,12 +139,11 @@ var _ = Service("front", func() {
Payload(InventoryRecord) Payload(InventoryRecord)
Result(Empty) Result(Empty)
Error("NotFound") Error("NotFound")
Error("BadRequest")
HTTP(func() { HTTP(func() {
POST("/character/{characterName}/item") POST("/character/{characterName}/item")
Response(StatusOK) Response(StatusOK)
Response(StatusBadRequest) Response("NotFound", StatusNotFound)
}) })
}) })
@ -169,12 +151,11 @@ var _ = Service("front", func() {
Payload(InventoryRecord) Payload(InventoryRecord)
Result(Empty) Result(Empty)
Error("NotFound") Error("NotFound")
Error("BadRequest")
HTTP(func() { HTTP(func() {
DELETE("/character/{characterName}/item/{itemName}") DELETE("/character/{characterName}/item/{itemName}")
Response(StatusOK) Response(StatusOK)
Response(StatusBadRequest) Response("NotFound", StatusNotFound)
}) })
}) })
@ -183,12 +164,12 @@ var _ = Service("front", func() {
Field(1, "characterName", String) Field(1, "characterName", String)
}) })
Result(ArrayOf(Item)) Result(ArrayOf(Item))
Error("NotFound")
HTTP(func() { HTTP(func() {
GET("/character/{characterName}/item") GET("/character/{characterName}/item")
Response(StatusOK) Response(StatusOK)
Response(StatusBadRequest) Response("NotFound", StatusNotFound)
Response(StatusNotFound)
}) })
}) })

View File

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

View File

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

View File

@ -27,6 +27,7 @@ var _ = Service("item", func() {
GRPC(func() { GRPC(func() {
Response(CodeOK) Response(CodeOK)
Response("NotFound", CodeNotFound)
}) })
}) })
@ -44,10 +45,11 @@ var _ = Service("item", func() {
Method("createItem", func() { Method("createItem", func() {
Payload(Item) Payload(Item)
Result(Item) Result(Item)
Error("BadRequest") Error("AlreadyExists")
GRPC(func() { GRPC(func() {
Response(CodeOK) Response(CodeOK)
Response("AlreadyExists", CodeAlreadyExists)
}) })
}) })
@ -55,10 +57,10 @@ var _ = Service("item", func() {
Payload(Item) Payload(Item)
Result(Item) Result(Item)
Error("NotFound") Error("NotFound")
Error("BadRequest")
GRPC(func() { GRPC(func() {
Response(CodeOK) Response(CodeOK)
Response("NotFound", CodeNotFound)
}) })
}) })
@ -68,12 +70,10 @@ var _ = Service("item", func() {
}) })
Result(Empty) Result(Empty)
Error("NotFound") Error("NotFound")
Error("BadRequest")
GRPC(func() { GRPC(func() {
Response(CodeOK) Response(CodeOK)
Response("NotFound", CodeNotFound) Response("NotFound", CodeNotFound)
Response("BadRequest", CodeInvalidArgument)
}) })
}) })
}) })

View File

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