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

View File

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

View File

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

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

View File

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

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
}