From 391f8e78eb5bb7c4b00bd0ceeda4b9541568ba34 Mon Sep 17 00:00:00 2001 From: Kevin Chen Date: Sat, 13 Jan 2024 15:57:47 -0500 Subject: [PATCH] feat: Use NotFound error codes --- cmd/svc/license/service/service.go | 20 +++++++++++++++++++- cmd/svc/org/service/service.go | 15 +++++++++++++++ cmd/svc/profile/service/service.go | 15 +++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/cmd/svc/license/service/service.go b/cmd/svc/license/service/service.go index c080fcc..555db11 100644 --- a/cmd/svc/license/service/service.go +++ b/cmd/svc/license/service/service.go @@ -4,6 +4,7 @@ package service import ( "context" "database/sql" + "errors" "fmt" licenseConnect "buf.build/gen/go/kevinmichaelchen/licenseapis/connectrpc/go/license/v1beta1/licensev1beta1connect" @@ -47,7 +48,10 @@ func (s *Service) CreateLicense( err = validator.Validate(req.Msg) if err != nil { - return nil, fmt.Errorf("invalid request: %w", err) + return nil, connect.NewError( + connect.CodeInvalidArgument, + fmt.Errorf("unable to validate request: %w", err), + ) } log.Info("Creating License...", @@ -84,6 +88,13 @@ func (s *Service) GetLicense( ) (*connect.Response[licensev1beta1.GetLicenseResponse], error) { record, err := models.FindLicense(ctx, s.db, req.Msg.GetId()) if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, connect.NewError( + connect.CodeNotFound, + fmt.Errorf("unable to retrieve item: %w", err), + ) + } + return nil, fmt.Errorf("unable to retrieve item: %w", err) } @@ -108,6 +119,13 @@ func (s *Service) ListLicenses( items, err := models.Licenses(mods...).All(ctx, s.db) if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, connect.NewError( + connect.CodeNotFound, + fmt.Errorf("unable to retrieve items: %w", err), + ) + } + return nil, fmt.Errorf("unable to retrieve items: %w", err) } diff --git a/cmd/svc/org/service/service.go b/cmd/svc/org/service/service.go index a233af6..fbf45c2 100644 --- a/cmd/svc/org/service/service.go +++ b/cmd/svc/org/service/service.go @@ -4,6 +4,7 @@ package service import ( "context" "database/sql" + "errors" "fmt" orgConnect "buf.build/gen/go/kevinmichaelchen/orgapis/connectrpc/go/org/v1beta1/orgv1beta1connect" @@ -66,6 +67,13 @@ func (s *Service) GetOrg( ) (*connect.Response[orgPB.GetOrgResponse], error) { record, err := models.FindOrg(ctx, s.db, req.Msg.GetId()) if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, connect.NewError( + connect.CodeNotFound, + fmt.Errorf("unable to retrieve item: %w", err), + ) + } + return nil, fmt.Errorf("unable to retrieve item: %w", err) } @@ -87,6 +95,13 @@ func (s *Service) ListOrgs( items, err := models.Orgs(mods...).All(ctx, s.db) if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, connect.NewError( + connect.CodeNotFound, + fmt.Errorf("unable to retrieve items: %w", err), + ) + } + return nil, fmt.Errorf("unable to retrieve items: %w", err) } diff --git a/cmd/svc/profile/service/service.go b/cmd/svc/profile/service/service.go index aeb6921..7d77fd9 100644 --- a/cmd/svc/profile/service/service.go +++ b/cmd/svc/profile/service/service.go @@ -4,6 +4,7 @@ package service import ( "context" "database/sql" + "errors" "fmt" profileConnect "buf.build/gen/go/kevinmichaelchen/profileapis/connectrpc/go/profile/v1beta1/profilev1beta1connect" @@ -68,6 +69,13 @@ func (s *Service) GetProfile( ) (*connect.Response[profilePB.GetProfileResponse], error) { record, err := models.FindProfile(ctx, s.db, req.Msg.GetId()) if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, connect.NewError( + connect.CodeNotFound, + fmt.Errorf("unable to retrieve item: %w", err), + ) + } + return nil, fmt.Errorf("unable to retrieve item: %w", err) } @@ -91,6 +99,13 @@ func (s *Service) ListProfiles( items, err := models.Profiles(mods...).All(ctx, s.db) if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, connect.NewError( + connect.CodeNotFound, + fmt.Errorf("unable to retrieve items: %w", err), + ) + } + return nil, fmt.Errorf("unable to retrieve items: %w", err) }