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
}