Skip to content

Commit 5f5a942

Browse files
committed
Merge branch 'main' into refactor-error-handle-coderef
# Conflicts: # pkg/error/error.go
2 parents f1600e8 + 8489257 commit 5f5a942

38 files changed

Lines changed: 707 additions & 1108 deletions

pkg/api/api/grpc_status.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,16 @@ func convertErrorReason(errorType pkgErr.ErrorType) string {
114114
return "UNAUTHENTICATED"
115115
case pkgErr.ErrorTypePermissionDenied:
116116
return "PERMISSION_DENIED"
117-
case pkgErr.ErrorTypeFailedPrecondition:
118-
return "FAILED_PRECONDITION"
119117
case pkgErr.ErrorTypeUnexpectedAffectedRows:
120118
return "UNEXPECTED_AFFECTED_ROWS"
121119
case pkgErr.ErrorTypeInternal:
122120
return "INTERNAL"
121+
case pkgErr.ErrorTypeFailedPrecondition:
122+
return "FAILED_PRECONDITION"
123+
case pkgErr.ErrorTypeUnavailable:
124+
return "UNAVAILABLE"
125+
case pkgErr.ErrorTypeAborted:
126+
return "ABORTED"
123127
default:
124128
return "UNKNOWN"
125129
}
@@ -139,14 +143,18 @@ func convertStatusCode(errorType pkgErr.ErrorType) codes.Code {
139143
return codes.AlreadyExists
140144
case pkgErr.ErrorTypeUnauthenticated:
141145
return codes.Unauthenticated
142-
case pkgErr.ErrorTypeFailedPrecondition:
143-
return codes.FailedPrecondition
144146
case pkgErr.ErrorTypePermissionDenied:
145147
return codes.PermissionDenied
146148
case pkgErr.ErrorTypeUnexpectedAffectedRows:
147149
return codes.Internal
148150
case pkgErr.ErrorTypeInternal:
149151
return codes.Internal
152+
case pkgErr.ErrorTypeFailedPrecondition:
153+
return codes.FailedPrecondition
154+
case pkgErr.ErrorTypeUnavailable:
155+
return codes.Unavailable
156+
case pkgErr.ErrorTypeAborted:
157+
return codes.Aborted
150158
default:
151159
return codes.Unknown
152160
}

pkg/auditlog/api/api.go

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
accountclient "github.com/bucketeer-io/bucketeer/pkg/account/client"
3030
v2as "github.com/bucketeer-io/bucketeer/pkg/account/storage/v2"
31+
"github.com/bucketeer-io/bucketeer/pkg/api/api"
3132
v2als "github.com/bucketeer-io/bucketeer/pkg/auditlog/storage/v2"
3233
domainevent "github.com/bucketeer-io/bucketeer/pkg/domainevent/domain"
3334
"github.com/bucketeer-io/bucketeer/pkg/locale"
@@ -148,13 +149,7 @@ func (s *auditlogService) GetAuditLog(
148149
return nil, statusInternal.Err()
149150
}
150151
} else {
151-
dt, err = statusInternal.WithDetails(&errdetails.LocalizedMessage{
152-
Locale: localizer.GetLocale(),
153-
Message: localizer.MustLocalize(locale.InternalServerError),
154-
})
155-
if err != nil {
156-
return nil, statusInternal.Err()
157-
}
152+
return nil, api.NewGRPCStatus(err).Err()
158153
}
159154
return nil, dt.Err()
160155
}
@@ -280,14 +275,7 @@ func (s *auditlogService) ListAuditLogs(
280275
"Failed to list auditlogs",
281276
log.FieldsFromIncomingContext(ctx).AddFields(zap.Error(err))...,
282277
)
283-
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
284-
Locale: localizer.GetLocale(),
285-
Message: localizer.MustLocalize(locale.InternalServerError),
286-
})
287-
if err != nil {
288-
return nil, statusInternal.Err()
289-
}
290-
return nil, dt.Err()
278+
return nil, api.NewGRPCStatus(err).Err()
291279
}
292280
editorEmails := make([]string, 0, len(auditlogs))
293281
for _, auditlog := range auditlogs {
@@ -429,14 +417,7 @@ func (s *auditlogService) ListAdminAuditLogs(
429417
"Failed to list admin auditlogs",
430418
log.FieldsFromIncomingContext(ctx).AddFields(zap.Error(err))...,
431419
)
432-
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
433-
Locale: localizer.GetLocale(),
434-
Message: localizer.MustLocalize(locale.InternalServerError),
435-
})
436-
if err != nil {
437-
return nil, statusInternal.Err()
438-
}
439-
return nil, dt.Err()
420+
return nil, api.NewGRPCStatus(err).Err()
440421
}
441422
for _, auditlog := range auditlogs {
442423
auditlog.LocalizedMessage = domainevent.LocalizedMessage(auditlog.Type, localizer)
@@ -575,14 +556,7 @@ func (s *auditlogService) ListFeatureHistory(
575556
zap.String("featureId", req.FeatureId),
576557
)...,
577558
)
578-
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
579-
Locale: localizer.GetLocale(),
580-
Message: localizer.MustLocalize(locale.InternalServerError),
581-
})
582-
if err != nil {
583-
return nil, statusInternal.Err()
584-
}
585-
return nil, dt.Err()
559+
return nil, api.NewGRPCStatus(err).Err()
586560
}
587561
for _, auditlog := range auditlogs {
588562
auditlog.LocalizedMessage = domainevent.LocalizedMessage(auditlog.Type, localizer)
@@ -636,14 +610,7 @@ func (s *auditlogService) getAccountMapByEmails(
636610
zap.String("environmentId", environmentID),
637611
)...,
638612
)
639-
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
640-
Locale: localizer.GetLocale(),
641-
Message: localizer.MustLocalize(locale.InternalServerError),
642-
})
643-
if err != nil {
644-
return accountMap, statusInternal.Err()
645-
}
646-
return accountMap, dt.Err()
613+
return accountMap, api.NewGRPCStatus(err).Err()
647614
}
648615
for i := range accounts {
649616
accountMap[accounts[i].Email] = accounts[i]
@@ -740,14 +707,7 @@ func (s *auditlogService) checkEnvironmentRole(
740707
zap.String("environmentId", environmentId),
741708
)...,
742709
)
743-
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
744-
Locale: localizer.GetLocale(),
745-
Message: localizer.MustLocalize(locale.InternalServerError),
746-
})
747-
if err != nil {
748-
return nil, statusInternal.Err()
749-
}
750-
return nil, dt.Err()
710+
return nil, api.NewGRPCStatus(err).Err()
751711
}
752712
}
753713
return editor, nil
@@ -791,14 +751,7 @@ func (s *auditlogService) checkSystemAdminRole(
791751
"Failed to check role",
792752
log.FieldsFromIncomingContext(ctx).AddFields(zap.Error(err))...,
793753
)
794-
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
795-
Locale: localizer.GetLocale(),
796-
Message: localizer.MustLocalize(locale.InternalServerError),
797-
})
798-
if err != nil {
799-
return nil, statusInternal.Err()
800-
}
801-
return nil, dt.Err()
754+
return nil, api.NewGRPCStatus(err).Err()
802755
}
803756
}
804757
return editor, nil

pkg/auditlog/api/api_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package api
1616

1717
import (
1818
"context"
19-
"errors"
2019
"testing"
2120

2221
"github.com/stretchr/testify/assert"
@@ -29,8 +28,10 @@ import (
2928

3029
accountclientmock "github.com/bucketeer-io/bucketeer/pkg/account/client/mock"
3130
v2asmock "github.com/bucketeer-io/bucketeer/pkg/account/storage/v2/mock"
31+
"github.com/bucketeer-io/bucketeer/pkg/api/api"
3232
v2alsmock "github.com/bucketeer-io/bucketeer/pkg/auditlog/storage/v2/mock"
3333
domainevent "github.com/bucketeer-io/bucketeer/pkg/domainevent/domain"
34+
pkgErr "github.com/bucketeer-io/bucketeer/pkg/error"
3435
"github.com/bucketeer-io/bucketeer/pkg/locale"
3536
"github.com/bucketeer-io/bucketeer/pkg/log"
3637
"github.com/bucketeer-io/bucketeer/pkg/rpc"
@@ -110,15 +111,15 @@ func TestGetAuditLog(t *testing.T) {
110111
setup: func(s *auditlogService) {
111112
s.auditLogStorage.(*v2alsmock.MockAuditLogStorage).EXPECT().GetAuditLog(
112113
gomock.Any(), gomock.Any(), gomock.Any(),
113-
).Return(nil, errors.New("test"))
114+
).Return(nil, pkgErr.NewErrorInternal(pkgErr.AuditlogPackageName, "internal"))
114115
},
115116
input: &proto.GetAuditLogRequest{
116117
Id: "id-1",
117118
EnvironmentId: "env-1",
118119
},
119120
expected: nil,
120121
getExpectedErr: func(localizer locale.Localizer) error {
121-
return createError(statusInternal, localizer.MustLocalize(locale.InternalServerError), localizer)
122+
return api.NewGRPCStatus(pkgErr.NewErrorInternal(pkgErr.AuditlogPackageName, "internal")).Err()
122123
},
123124
},
124125
{
@@ -225,12 +226,12 @@ func TestListAuditLogsMySQL(t *testing.T) {
225226
setup: func(s *auditlogService) {
226227
s.auditLogStorage.(*v2alsmock.MockAuditLogStorage).EXPECT().ListAuditLogs(
227228
gomock.Any(), gomock.Any(),
228-
).Return(nil, 0, int64(0), errors.New("test"))
229+
).Return(nil, 0, int64(0), pkgErr.NewErrorInternal(pkgErr.AuditlogPackageName, "internal"))
229230
},
230231
input: &proto.ListAuditLogsRequest{EnvironmentId: "ns0"},
231232
expected: nil,
232233
getExpectedErr: func(localizer locale.Localizer) error {
233-
return createError(statusInternal, localizer.MustLocalize(locale.InternalServerError), localizer)
234+
return api.NewGRPCStatus(pkgErr.NewErrorInternal(pkgErr.AuditlogPackageName, "internal")).Err()
234235
},
235236
},
236237
{
@@ -420,11 +421,11 @@ func TestListAdminAuditLogsMySQL(t *testing.T) {
420421
setup: func(s *auditlogService) {
421422
s.adminAuditLogStorage.(*v2alsmock.MockAdminAuditLogStorage).EXPECT().ListAdminAuditLogs(
422423
gomock.Any(), gomock.Any(),
423-
).Return(nil, 0, int64(0), errors.New("test"))
424+
).Return(nil, 0, int64(0), pkgErr.NewErrorInternal(pkgErr.AuditlogPackageName, "internal"))
424425
},
425426
input: &proto.ListAdminAuditLogsRequest{},
426427
expected: nil,
427-
expectedErr: createError(statusInternal, localizer.MustLocalize(locale.InternalServerError)),
428+
expectedErr: api.NewGRPCStatus(pkgErr.NewErrorInternal(pkgErr.AuditlogPackageName, "internal")).Err(),
428429
},
429430
{
430431
desc: "success",
@@ -526,12 +527,12 @@ func TestListFeatureHistoryMySQL(t *testing.T) {
526527
setup: func(s *auditlogService) {
527528
s.auditLogStorage.(*v2alsmock.MockAuditLogStorage).EXPECT().ListAuditLogs(
528529
gomock.Any(), gomock.Any(),
529-
).Return(nil, 0, int64(0), errors.New("test"))
530+
).Return(nil, 0, int64(0), pkgErr.NewErrorInternal(pkgErr.AuditlogPackageName, "internal"))
530531
},
531532
input: &proto.ListFeatureHistoryRequest{EnvironmentId: "ns0"},
532533
expected: nil,
533534
getExpectedErr: func(localizer locale.Localizer) error {
534-
return createError(localizer, statusInternal, localizer.MustLocalize(locale.InternalServerError))
535+
return api.NewGRPCStatus(pkgErr.NewErrorInternal(pkgErr.AuditlogPackageName, "internal")).Err()
535536
},
536537
},
537538
{

pkg/auditlog/api/error.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,22 @@
1515
package api
1616

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

2222
var (
23-
statusInternal = gstatus.New(codes.Internal, "auditlog: internal")
24-
statusUnauthenticated = gstatus.New(codes.Unauthenticated, "auditlog: unauthenticated")
25-
statusPermissionDenied = gstatus.New(codes.PermissionDenied, "auditlog: permission denied")
26-
statusMissingID = gstatus.New(codes.InvalidArgument, "auditlog: missing ID")
27-
statusNotFound = gstatus.New(codes.NotFound, "auditlog: not found")
28-
statusInvalidCursor = gstatus.New(codes.InvalidArgument, "auditlog: cursor is invalid")
29-
statusInvalidOrderBy = gstatus.New(codes.InvalidArgument, "auditlog: order_by is invalid")
23+
statusInternal = api.NewGRPCStatus(pkgErr.NewErrorInternal(pkgErr.AuditlogPackageName, "internal"))
24+
statusUnauthenticated = api.NewGRPCStatus(
25+
pkgErr.NewErrorUnauthenticated(pkgErr.AuditlogPackageName, "unauthenticated"))
26+
statusPermissionDenied = api.NewGRPCStatus(
27+
pkgErr.NewErrorPermissionDenied(pkgErr.AuditlogPackageName, "permission denied"))
28+
statusMissingID = api.NewGRPCStatus(
29+
pkgErr.NewErrorInvalidArgEmpty(pkgErr.AuditlogPackageName, "missing ID", "ID"))
30+
statusNotFound = api.NewGRPCStatus(
31+
pkgErr.NewErrorNotFound(pkgErr.AuditlogPackageName, "not found", "auditlog"))
32+
statusInvalidCursor = api.NewGRPCStatus(
33+
pkgErr.NewErrorInvalidArgNotMatchFormat(pkgErr.AuditlogPackageName, "cursor is invalid", "cursor"))
34+
statusInvalidOrderBy = api.NewGRPCStatus(
35+
pkgErr.NewErrorInvalidArgNotMatchFormat(pkgErr.AuditlogPackageName, "order_by is invalid", "order_by"))
3036
)

pkg/auditlog/storage/v2/admin_audit_log.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ package v2
1818
import (
1919
"context"
2020
_ "embed"
21-
"errors"
2221
"strings"
2322

2423
"github.com/bucketeer-io/bucketeer/pkg/auditlog/domain"
24+
pkgErr "github.com/bucketeer-io/bucketeer/pkg/error"
2525
"github.com/bucketeer-io/bucketeer/pkg/storage/v2/mysql"
2626
proto "github.com/bucketeer-io/bucketeer/proto/auditlog"
2727
eventproto "github.com/bucketeer-io/bucketeer/proto/event/domain"
2828
)
2929

3030
var (
31-
ErrAdminAuditLogAlreadyExists = errors.New("auditlog: admin auditlog already exists")
31+
ErrAdminAuditLogAlreadyExists = pkgErr.NewErrorAlreadyExists(
32+
pkgErr.AuditlogPackageName, "admin auditlog already exists")
3233

3334
//go:embed sql/adminauditlog/insert_admin_audit_logs_v2.sql
3435
insertAdminAuditLogsV2SQL string

pkg/auditlog/storage/v2/audit_log.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ import (
2222
"strings"
2323

2424
"github.com/bucketeer-io/bucketeer/pkg/auditlog/domain"
25+
pkgErr "github.com/bucketeer-io/bucketeer/pkg/error"
2526
"github.com/bucketeer-io/bucketeer/pkg/storage/v2/mysql"
2627
proto "github.com/bucketeer-io/bucketeer/proto/auditlog"
2728
eventproto "github.com/bucketeer-io/bucketeer/proto/event/domain"
2829
)
2930

3031
var (
31-
ErrAuditLogAlreadyExists = errors.New("auditlog: auditlog already exists")
32-
ErrAuditLogNotFound = errors.New("auditlog: auditlog not found")
32+
ErrAuditLogAlreadyExists = pkgErr.NewErrorAlreadyExists(pkgErr.AuditlogPackageName, "auditlog already exists")
33+
ErrAuditLogNotFound = pkgErr.NewErrorNotFound(pkgErr.AuditlogPackageName, "auditlog not found", "auditlog")
3334
//go:embed sql/auditlog/select_audit_log_v2.sql
3435
selectAuditLogV2SQL string
3536
//go:embed sql/auditlog/insert_audit_logs_v2.sql

0 commit comments

Comments
 (0)