feat: declare API errors and authentication with developer tooling
This commit is contained in:
@@ -3,11 +3,12 @@ package user
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"example.com/userService/internal/apierrors"
|
||||
"example.com/userService/internal/model"
|
||||
repo "example.com/userService/internal/repository/user"
|
||||
"example.com/userService/internal/servicecontext"
|
||||
"example.com/userService/internal/types"
|
||||
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
omnierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
"git.yuntuo.hk/k2safe/go-omni/logx"
|
||||
"net/mail"
|
||||
"strings"
|
||||
@@ -34,19 +35,19 @@ func (s *Service) failure(ctx context.Context, err error) error {
|
||||
case err == nil:
|
||||
return nil
|
||||
case errors.Is(err, model.ErrUserNotFound):
|
||||
return apierrors.NotFound("USER_NOT_FOUND", "user not found")
|
||||
return apierrors.UserNotFound()
|
||||
case errors.Is(err, model.ErrEmailTaken):
|
||||
return apierrors.New(409, "EMAIL_TAKEN", "email already in use")
|
||||
return apierrors.EmailTaken()
|
||||
case errors.Is(err, model.ErrInvalidUser):
|
||||
return apierrors.BadRequest("INVALID_USER", "invalid user")
|
||||
return apierrors.InvalidUser()
|
||||
case errors.Is(err, model.ErrInvalidPage):
|
||||
return apierrors.BadRequest("INVALID_PAGE", "invalid pagination")
|
||||
return apierrors.InvalidPage()
|
||||
default:
|
||||
// Do not log SQL/DSN or user input; requestId correlates the failed operation.
|
||||
s.deps.Logger.ErrorContext(ctx, "user.operationFailed", "category", "persistence")
|
||||
// HTTP access logging also sees the returned error. Keep raw driver
|
||||
// errors out of that path because their messages may contain input.
|
||||
return apierrors.New(500, "INTERNAL_ERROR", "internal server error")
|
||||
return omnierrors.New(500, "INTERNAL_ERROR", "internal server error")
|
||||
}
|
||||
}
|
||||
func (s *Service) mutate(ctx context.Context, operation string, fn func(repo.UserRepository) (model.User, error)) (*types.UserResp, error) {
|
||||
|
||||
Reference in New Issue
Block a user