Using generated error types
This commit is contained in:
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user