Skip to content

Commit d7f8a0f

Browse files
authored
refactor: change error definition in auth PKG to Bucketeer error (#2133)
1 parent 70baf53 commit d7f8a0f

File tree

5 files changed

+46
-102
lines changed

5 files changed

+46
-102
lines changed

pkg/auth/api/api.go

Lines changed: 11 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
accountclient "github.com/bucketeer-io/bucketeer/v2/pkg/account/client"
3636
"github.com/bucketeer-io/bucketeer/v2/pkg/account/domain"
3737
accountstotage "github.com/bucketeer-io/bucketeer/v2/pkg/account/storage/v2"
38+
"github.com/bucketeer-io/bucketeer/v2/pkg/api/api"
3839
"github.com/bucketeer-io/bucketeer/v2/pkg/auth"
3940
"github.com/bucketeer-io/bucketeer/v2/pkg/auth/google"
4041
envdomain "github.com/bucketeer-io/bucketeer/v2/pkg/environment/domain"
@@ -186,14 +187,7 @@ func (s *authService) GetAuthenticationURL(
186187
zap.String("state", req.State),
187188
zap.String("redirect_url", req.RedirectUrl),
188189
)
189-
dt, err := auth.StatusInternal.WithDetails(&errdetails.LocalizedMessage{
190-
Locale: localizer.GetLocale(),
191-
Message: localizer.MustLocalize(locale.InternalServerError),
192-
})
193-
if err != nil {
194-
return nil, auth.StatusInternal.Err()
195-
}
196-
return nil, dt.Err()
190+
return nil, api.NewGRPCStatus(err).Err()
197191
}
198192
loginURL, err := authenticator.Login(ctx, req.State, req.RedirectUrl)
199193
if err != nil {
@@ -203,14 +197,7 @@ func (s *authService) GetAuthenticationURL(
203197
zap.String("state", req.State),
204198
zap.String("redirect_url", req.RedirectUrl),
205199
)
206-
dt, err := auth.StatusInternal.WithDetails(&errdetails.LocalizedMessage{
207-
Locale: localizer.GetLocale(),
208-
Message: localizer.MustLocalize(locale.InternalServerError),
209-
})
210-
if err != nil {
211-
return nil, auth.StatusInternal.Err()
212-
}
213-
return nil, dt.Err()
200+
return nil, api.NewGRPCStatus(err).Err()
214201
}
215202
return &authproto.GetAuthenticationURLResponse{Url: loginURL}, nil
216203
}
@@ -237,14 +224,7 @@ func (s *authService) ExchangeToken(
237224
zap.String("code", req.Code),
238225
zap.String("redirect_url", req.RedirectUrl),
239226
)
240-
dt, err := auth.StatusInternal.WithDetails(&errdetails.LocalizedMessage{
241-
Locale: localizer.GetLocale(),
242-
Message: localizer.MustLocalize(locale.InternalServerError),
243-
})
244-
if err != nil {
245-
return nil, auth.StatusInternal.Err()
246-
}
247-
return nil, dt.Err()
227+
return nil, api.NewGRPCStatus(err).Err()
248228
}
249229
userInfo, err := authenticator.Exchange(ctx, req.Code, req.RedirectUrl)
250230
if err != nil {
@@ -254,14 +234,7 @@ func (s *authService) ExchangeToken(
254234
zap.String("code", req.Code),
255235
zap.String("redirect_url", req.RedirectUrl),
256236
)
257-
dt, err := auth.StatusInternal.WithDetails(&errdetails.LocalizedMessage{
258-
Locale: localizer.GetLocale(),
259-
Message: localizer.MustLocalize(locale.InternalServerError),
260-
})
261-
if err != nil {
262-
return nil, auth.StatusInternal.Err()
263-
}
264-
return nil, dt.Err()
237+
return nil, api.NewGRPCStatus(err).Err()
265238
}
266239

267240
organizations, err := s.getOrganizationsByEmail(ctx, userInfo.Email, localizer)
@@ -360,14 +333,7 @@ func (s *authService) RefreshToken(
360333
zap.Any("organizations", organizations),
361334
zap.Any("refresh_token", refreshToken),
362335
)
363-
dt, err := auth.StatusInternal.WithDetails(&errdetails.LocalizedMessage{
364-
Locale: localizer.GetLocale(),
365-
Message: localizer.MustLocalize(locale.InternalServerError),
366-
})
367-
if err != nil {
368-
return nil, auth.StatusInternal.Err()
369-
}
370-
return nil, dt.Err()
336+
return nil, api.NewGRPCStatus(err).Err()
371337
}
372338
return &authproto.RefreshTokenResponse{Token: newToken}, nil
373339
}
@@ -444,14 +410,7 @@ func (s *authService) SwitchOrganization(
444410
zap.String("email", accessToken.Email),
445411
zap.String("organizationID", newOrganizationID),
446412
)
447-
dt, err := auth.StatusInternal.WithDetails(&errdetails.LocalizedMessage{
448-
Locale: localizer.GetLocale(),
449-
Message: localizer.MustLocalize(locale.InternalServerError),
450-
})
451-
if err != nil {
452-
return nil, auth.StatusInternal.Err()
453-
}
454-
return nil, dt.Err()
413+
return nil, api.NewGRPCStatus(err).Err()
455414
}
456415
accountDomain := domain.AccountV2{AccountV2: account.Account}
457416
if account.Account.Disabled {
@@ -528,14 +487,7 @@ func (s *authService) getOrganizationsByEmail(
528487
zap.Error(err),
529488
zap.String("email", email),
530489
)
531-
dt, err := auth.StatusInternal.WithDetails(&errdetails.LocalizedMessage{
532-
Locale: localizer.GetLocale(),
533-
Message: localizer.MustLocalize(locale.InternalServerError),
534-
})
535-
if err != nil {
536-
return nil, auth.StatusInternal.Err()
537-
}
538-
return nil, dt.Err()
490+
return nil, api.NewGRPCStatus(err).Err()
539491
}
540492
if len(orgResp.Organizations) == 0 {
541493
s.logger.Error(
@@ -727,14 +679,7 @@ func (s *authService) generateToken(
727679
"Failed to sign access token",
728680
zap.Error(err),
729681
)
730-
dt, err := auth.StatusInternal.WithDetails(&errdetails.LocalizedMessage{
731-
Locale: localizer.GetLocale(),
732-
Message: localizer.MustLocalize(locale.InternalServerError),
733-
})
734-
if err != nil {
735-
return nil, auth.StatusInternal.Err()
736-
}
737-
return nil, dt.Err()
682+
return nil, api.NewGRPCStatus(err).Err()
738683
}
739684

740685
// Create refresh token
@@ -755,14 +700,7 @@ func (s *authService) generateToken(
755700
"Failed to sign refresh token",
756701
zap.Error(err),
757702
)
758-
dt, err := auth.StatusInternal.WithDetails(&errdetails.LocalizedMessage{
759-
Locale: localizer.GetLocale(),
760-
Message: localizer.MustLocalize(locale.InternalServerError),
761-
})
762-
if err != nil {
763-
return nil, auth.StatusInternal.Err()
764-
}
765-
return nil, dt.Err()
703+
return nil, api.NewGRPCStatus(err).Err()
766704
}
767705

768706
return &authproto.Token{
@@ -812,14 +750,7 @@ func (s *authService) checkAccountStatus(
812750
// So, to avoid false errors, we ignore them if the account wasn't found in non-system admin organizations.
813751
continue
814752
}
815-
dt, err := auth.StatusInternal.WithDetails(&errdetails.LocalizedMessage{
816-
Locale: localizer.GetLocale(),
817-
Message: localizer.MustLocalize(locale.InternalServerError),
818-
})
819-
if err != nil {
820-
return nil, auth.StatusInternal.Err()
821-
}
822-
return nil, dt.Err()
753+
return nil, api.NewGRPCStatus(err).Err()
823754
}
824755
if !resp.Account.Disabled {
825756
// The account must have at least one account enabled

pkg/auth/error.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,34 @@
1515
package auth
1616

1717
import (
18-
"google.golang.org/grpc/codes"
19-
gstatus "google.golang.org/grpc/status"
18+
"github.com/bucketeer-io/bucketeer/v2/pkg/api/api"
19+
pkgErr "github.com/bucketeer-io/bucketeer/v2/pkg/error"
2020
)
2121

2222
var (
23-
StatusInternal = gstatus.New(codes.Internal, "auth: internal")
24-
StatusMissingCode = gstatus.New(codes.InvalidArgument, "auth: code must not be empty")
25-
StatusMissingState = gstatus.New(codes.InvalidArgument, "auth: state must not be empty")
26-
StatusMissingAuthType = gstatus.New(codes.InvalidArgument, "auth: missing authType")
27-
StatusUnknownAuthType = gstatus.New(codes.InvalidArgument, "auth: unknown authType")
28-
StatusMissingRedirectURL = gstatus.New(codes.InvalidArgument, "auth: missing redirectURL")
29-
StatusUnregisteredRedirectURL = gstatus.New(codes.InvalidArgument, "auth: unregistered redirectURL")
30-
StatusMissingRefreshToken = gstatus.New(codes.InvalidArgument, "auth: refreshToken must not be empty")
31-
StatusInvalidCode = gstatus.New(codes.InvalidArgument, "auth: invalid code")
32-
StatusInvalidRefreshToken = gstatus.New(codes.InvalidArgument, "auth: invalid refresh token")
33-
StatusUnapprovedAccount = gstatus.New(codes.PermissionDenied, "auth: unapproved account")
34-
StatusAccessDeniedEmail = gstatus.New(codes.PermissionDenied, "auth: access denied email")
35-
StatusUnauthenticated = gstatus.New(codes.Unauthenticated, "auth: not authenticated")
36-
StateMissingUsername = gstatus.New(codes.InvalidArgument, "auth: missing username")
37-
StateMissingPassword = gstatus.New(codes.InvalidArgument, "auth: missing password")
38-
StatusAccessDenied = gstatus.New(codes.PermissionDenied, "auth: access denied")
39-
StatusInvalidOrganization = gstatus.New(codes.InvalidArgument, "auth: invalid organization")
23+
StatusInternal = api.NewGRPCStatus(pkgErr.NewErrorInternal(pkgErr.AuthPackageName, "internal"))
24+
StatusMissingCode = api.NewGRPCStatus(
25+
pkgErr.NewErrorInvalidArgEmpty(pkgErr.AuthPackageName, "code must not be empty", "code"))
26+
StatusMissingState = api.NewGRPCStatus(
27+
pkgErr.NewErrorInvalidArgEmpty(pkgErr.AuthPackageName, "state must not be empty", "state"))
28+
StatusMissingAuthType = api.NewGRPCStatus(
29+
pkgErr.NewErrorInvalidArgNotMatchFormat(pkgErr.AuthPackageName, "missing authType", "authType"))
30+
StatusUnknownAuthType = api.NewGRPCStatus(
31+
pkgErr.NewErrorInvalidArgUnknown(pkgErr.AuthPackageName, "unknown authType", "authType"))
32+
StatusMissingRedirectURL = api.NewGRPCStatus(
33+
pkgErr.NewErrorInvalidArgEmpty(pkgErr.AuthPackageName, "missing redirectURL", "redirectURL"))
34+
StatusMissingRefreshToken = api.NewGRPCStatus(
35+
pkgErr.NewErrorInvalidArgEmpty(pkgErr.AuthPackageName, "refreshToken must not be empty", "refreshToken"))
36+
StatusUnapprovedAccount = api.NewGRPCStatus(
37+
pkgErr.NewErrorPermissionDenied(pkgErr.AuthPackageName, "unapproved account"))
38+
StatusAccessDeniedEmail = api.NewGRPCStatus(
39+
pkgErr.NewErrorPermissionDenied(pkgErr.AuthPackageName, "access denied email"))
40+
StatusUnauthenticated = api.NewGRPCStatus(
41+
pkgErr.NewErrorUnauthenticated(pkgErr.AuthPackageName, "not authenticated"))
42+
StateMissingUsername = api.NewGRPCStatus(
43+
pkgErr.NewErrorInvalidArgEmpty(pkgErr.AuthPackageName, "missing username", "username"))
44+
StateMissingPassword = api.NewGRPCStatus(
45+
pkgErr.NewErrorInvalidArgEmpty(pkgErr.AuthPackageName, "missing password", "password"))
46+
StatusAccessDenied = api.NewGRPCStatus(
47+
pkgErr.NewErrorPermissionDenied(pkgErr.AuthPackageName, "access denied"))
4048
)

pkg/auth/google/authenticator.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@ package google
1717
import (
1818
"context"
1919
"encoding/json"
20-
"errors"
2120
"io"
2221

2322
"go.uber.org/zap"
2423
"golang.org/x/oauth2"
2524
"golang.org/x/oauth2/google"
2625

2726
"github.com/bucketeer-io/bucketeer/v2/pkg/auth"
27+
pkgErr "github.com/bucketeer-io/bucketeer/v2/pkg/error"
2828
)
2929

3030
var (
31-
ErrUnregisteredRedirectURL = errors.New("google: unregistered redirectURL")
31+
ErrUnregisteredRedirectURL = pkgErr.NewErrorInvalidArgEmpty(
32+
pkgErr.AuthPackageName,
33+
"unregistered redirectURL",
34+
"redirectURL",
35+
)
3236
)
3337

3438
var (

pkg/error/error.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const (
3333
CoderefPackageName = "coderef"
3434
TeamPackageName = "team"
3535
ExperimentPackageName = "experiment"
36+
AuthPackageName = "auth"
3637

3738
invalidTypeUnknown = "unknown"
3839
invalidTypeEmpty = "empty"

test/e2e/experiment/experiment_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ func TestDeleteGoal(t *testing.T) {
499499
Id: goalID,
500500
EnvironmentId: *environmentID,
501501
})
502-
assert.Equal(t, err.Error(), "rpc error: code = NotFound desc = experiment: not found")
502+
assert.Equal(t, "rpc error: code = NotFound desc = experiment:not found, experiment", err.Error())
503503
}
504504

505505
func TestStatusUpdateFromWaitingToRunning(t *testing.T) {

0 commit comments

Comments
 (0)