Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use NotFound error codes #99

Merged
merged 1 commit into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion cmd/svc/license/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package service
import (
"context"
"database/sql"
"errors"
"fmt"

licenseConnect "buf.build/gen/go/kevinmichaelchen/licenseapis/connectrpc/go/license/v1beta1/licensev1beta1connect"
Expand Down Expand Up @@ -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...",
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down
15 changes: 15 additions & 0 deletions cmd/svc/org/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package service
import (
"context"
"database/sql"
"errors"
"fmt"

orgConnect "buf.build/gen/go/kevinmichaelchen/orgapis/connectrpc/go/org/v1beta1/orgv1beta1connect"
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down
15 changes: 15 additions & 0 deletions cmd/svc/profile/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package service
import (
"context"
"database/sql"
"errors"
"fmt"

profileConnect "buf.build/gen/go/kevinmichaelchen/profileapis/connectrpc/go/profile/v1beta1/profilev1beta1connect"
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down
Loading