From 32bcb894ee29859b0abe36684e5cd0fa105818c0 Mon Sep 17 00:00:00 2001 From: ari melody Date: Fri, 31 Jul 2026 10:40:50 +0100 Subject: [PATCH] make service handling of non-error-not-founds explicit --- repository/account/interface.go | 2 ++ service/account/account.go | 25 ++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/repository/account/interface.go b/repository/account/interface.go index fda2b1a..ff2ef7d 100644 --- a/repository/account/interface.go +++ b/repository/account/interface.go @@ -5,6 +5,8 @@ import "arimelody-web/model" type AccountRepository interface { GetAll() ([]*model.Account, error) GetCount() (int, error) + // Fetches an account by ID, returning an error if one was encountered. + // If the account does not exist, both response fields are nil. GetByID(id string) (*model.Account, error) GetByUsername(username string) (*model.Account, error) GetByEmail(email string) (*model.Account, error) diff --git a/service/account/account.go b/service/account/account.go index 5e86dc6..09e2430 100644 --- a/service/account/account.go +++ b/service/account/account.go @@ -4,6 +4,7 @@ import ( "arimelody-web/model" repository "arimelody-web/repository/account" "errors" + "fmt" "log" ) @@ -28,15 +29,33 @@ func (s *AccountService) GetCount() (int, error) { } func (s *AccountService) GetByID(id string) (*model.Account, error) { - return s.repo.GetByID(id) + if account, err := s.repo.GetByID(id); err != nil { + return nil, err + } else if account == nil { + return nil, fmt.Errorf("Account does not exist: %s", id) + } else { + return account, nil + } } func (s *AccountService) GetByUsername(username string) (*model.Account, error) { - return s.repo.GetByUsername(username) + if account, err := s.repo.GetByUsername(username); err != nil { + return nil, err + } else if account == nil { + return nil, fmt.Errorf("Account does not exist: %s", username) + } else { + return account, nil + } } func (s *AccountService) GetByEmail(email string) (*model.Account, error) { - return s.repo.GetByEmail(email) + if account, err := s.repo.GetByEmail(email); err != nil { + return nil, err + } else if account == nil { + return nil, fmt.Errorf("Account does not exist with email: %s", email) + } else { + return account, nil + } } func (s *AccountService) Create(