Skip to content

Commit 550d25b

Browse files
authored
refactor: notificationAPI related error handling in Notification package (#2106)
1 parent ec2d34b commit 550d25b

File tree

9 files changed

+129
-231
lines changed

9 files changed

+129
-231
lines changed

pkg/error/error.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import (
2121
)
2222

2323
const (
24-
AccountPackageName = "account"
24+
AccountPackageName = "account"
25+
NotificationPackageName = "notification"
2526

2627
invalidTypeUnknown = "unknown"
2728
invalidTypeEmpty = "empty"

pkg/notification/api/admin_subscription.go

Lines changed: 10 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"google.golang.org/grpc/codes"
2424
"google.golang.org/grpc/status"
2525

26+
"github.com/bucketeer-io/bucketeer/pkg/api/api"
2627
"github.com/bucketeer-io/bucketeer/pkg/locale"
2728
"github.com/bucketeer-io/bucketeer/pkg/log"
2829
"github.com/bucketeer-io/bucketeer/pkg/notification/command"
@@ -55,14 +56,7 @@ func (s *NotificationService) CreateAdminSubscription(
5556
zap.Any("recipient", req.Command.Recipient),
5657
)...,
5758
)
58-
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
59-
Locale: localizer.GetLocale(),
60-
Message: localizer.MustLocalize(locale.InternalServerError),
61-
})
62-
if err != nil {
63-
return nil, statusInternal.Err()
64-
}
65-
return nil, dt.Err()
59+
return nil, api.NewGRPCStatus(err).Err()
6660
}
6761
var handler command.Handler = command.NewEmptyAdminSubscriptionCommandHandler()
6862
err = s.mysqlClient.RunInTransactionV2(ctx, func(contextWithTx context.Context, _ mysql.Transaction) error {
@@ -95,14 +89,7 @@ func (s *NotificationService) CreateAdminSubscription(
9589
zap.Error(err),
9690
)...,
9791
)
98-
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
99-
Locale: localizer.GetLocale(),
100-
Message: localizer.MustLocalize(locale.InternalServerError),
101-
})
102-
if err != nil {
103-
return nil, statusInternal.Err()
104-
}
105-
return nil, dt.Err()
92+
return nil, api.NewGRPCStatus(err).Err()
10693
}
10794
if errs := s.publishDomainEvents(ctx, handler.Events()); len(errs) > 0 {
10895
s.logger.Error(
@@ -111,14 +98,7 @@ func (s *NotificationService) CreateAdminSubscription(
11198
zap.Any("errors", errs),
11299
)...,
113100
)
114-
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
115-
Locale: localizer.GetLocale(),
116-
Message: localizer.MustLocalize(locale.InternalServerError),
117-
})
118-
if err != nil {
119-
return nil, statusInternal.Err()
120-
}
121-
return nil, dt.Err()
101+
return nil, api.NewGRPCStatus(err).Err()
122102
}
123103
return &notificationproto.CreateAdminSubscriptionResponse{}, nil
124104
}
@@ -407,14 +387,7 @@ func (s *NotificationService) updateAdminSubscription(
407387
zap.String("id", id),
408388
)...,
409389
)
410-
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
411-
Locale: localizer.GetLocale(),
412-
Message: localizer.MustLocalize(locale.InternalServerError),
413-
})
414-
if err != nil {
415-
return statusInternal.Err()
416-
}
417-
return dt.Err()
390+
return api.NewGRPCStatus(err).Err()
418391
}
419392
if errs := s.publishDomainEvents(ctx, handler.Events()); len(errs) > 0 {
420393
s.logger.Error(
@@ -424,14 +397,7 @@ func (s *NotificationService) updateAdminSubscription(
424397
zap.String("id", id),
425398
)...,
426399
)
427-
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
428-
Locale: localizer.GetLocale(),
429-
Message: localizer.MustLocalize(locale.InternalServerError),
430-
})
431-
if err != nil {
432-
return statusInternal.Err()
433-
}
434-
return dt.Err()
400+
return api.NewGRPCStatus(err).Err()
435401
}
436402
return nil
437403
}
@@ -484,14 +450,7 @@ func (s *NotificationService) DeleteAdminSubscription(
484450
zap.String("id", req.Id),
485451
)...,
486452
)
487-
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
488-
Locale: localizer.GetLocale(),
489-
Message: localizer.MustLocalize(locale.InternalServerError),
490-
})
491-
if err != nil {
492-
return nil, statusInternal.Err()
493-
}
494-
return nil, dt.Err()
453+
return nil, api.NewGRPCStatus(err).Err()
495454
}
496455
if errs := s.publishDomainEvents(ctx, handler.Events()); len(errs) > 0 {
497456
s.logger.Error(
@@ -501,14 +460,7 @@ func (s *NotificationService) DeleteAdminSubscription(
501460
zap.String("id", req.Id),
502461
)...,
503462
)
504-
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
505-
Locale: localizer.GetLocale(),
506-
Message: localizer.MustLocalize(locale.InternalServerError),
507-
})
508-
if err != nil {
509-
return nil, statusInternal.Err()
510-
}
511-
return nil, dt.Err()
463+
return nil, api.NewGRPCStatus(err).Err()
512464
}
513465
return &notificationproto.DeleteAdminSubscriptionResponse{}, nil
514466
}
@@ -587,14 +539,7 @@ func (s *NotificationService) GetAdminSubscription(
587539
zap.String("id", req.Id),
588540
)...,
589541
)
590-
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
591-
Locale: localizer.GetLocale(),
592-
Message: localizer.MustLocalize(locale.InternalServerError),
593-
})
594-
if err != nil {
595-
return nil, statusInternal.Err()
596-
}
597-
return nil, dt.Err()
542+
return nil, api.NewGRPCStatus(err).Err()
598543
}
599544
return &notificationproto.GetAdminSubscriptionResponse{Subscription: subscription.Subscription}, nil
600545
}
@@ -794,14 +739,7 @@ func (s *NotificationService) listAdminSubscriptionsMySQL(
794739
zap.Error(err),
795740
)...,
796741
)
797-
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
798-
Locale: localizer.GetLocale(),
799-
Message: localizer.MustLocalize(locale.InternalServerError),
800-
})
801-
if err != nil {
802-
return nil, "", 0, statusInternal.Err()
803-
}
804-
return nil, "", 0, dt.Err()
742+
return nil, "", 0, api.NewGRPCStatus(err).Err()
805743
}
806744
return subscriptions, strconv.Itoa(nextCursor), totalCount, nil
807745
}

pkg/notification/api/api.go

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"google.golang.org/grpc/status"
2525

2626
accountclient "github.com/bucketeer-io/bucketeer/pkg/account/client"
27+
"github.com/bucketeer-io/bucketeer/pkg/api/api"
2728
"github.com/bucketeer-io/bucketeer/pkg/locale"
2829
"github.com/bucketeer-io/bucketeer/pkg/log"
2930
v2 "github.com/bucketeer-io/bucketeer/pkg/notification/storage/v2"
@@ -123,14 +124,7 @@ func (s *NotificationService) checkSystemAdminRole(
123124
"Failed to check role",
124125
log.FieldsFromIncomingContext(ctx).AddFields(zap.Error(err))...,
125126
)
126-
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
127-
Locale: localizer.GetLocale(),
128-
Message: localizer.MustLocalize(locale.InternalServerError),
129-
})
130-
if err != nil {
131-
return nil, statusInternal.Err()
132-
}
133-
return nil, dt.Err()
127+
return nil, api.NewGRPCStatus(err).Err()
134128
}
135129
}
136130
return editor, nil
@@ -198,14 +192,7 @@ func (s *NotificationService) checkEnvironmentRole(
198192
zap.String("environmentId", environmentId),
199193
)...,
200194
)
201-
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
202-
Locale: localizer.GetLocale(),
203-
Message: localizer.MustLocalize(locale.InternalServerError),
204-
})
205-
if err != nil {
206-
return nil, statusInternal.Err()
207-
}
208-
return nil, dt.Err()
195+
return nil, api.NewGRPCStatus(err).Err()
209196
}
210197
}
211198
return editor, nil
@@ -269,14 +256,7 @@ func (s *NotificationService) checkOrganizationRole(
269256
zap.String("organizationID", organizationID),
270257
)...,
271258
)
272-
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
273-
Locale: localizer.GetLocale(),
274-
Message: localizer.MustLocalize(locale.InternalServerError),
275-
})
276-
if err != nil {
277-
return nil, statusInternal.Err()
278-
}
279-
return nil, dt.Err()
259+
return nil, api.NewGRPCStatus(err).Err()
280260
}
281261
}
282262
return editor, nil

pkg/notification/api/error.go

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,56 @@
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+
err "github.com/bucketeer-io/bucketeer/pkg/error"
2020
)
2121

2222
var (
23-
statusInternal = gstatus.New(codes.Internal, "notification: internal")
24-
statusIDRequired = gstatus.New(codes.InvalidArgument, "notification: id must be specified")
25-
statusNameRequired = gstatus.New(codes.InvalidArgument, "notification: name must be specified")
26-
statusSourceTypesRequired = gstatus.New(
27-
codes.InvalidArgument,
28-
"notification: notification types must be specified",
29-
)
30-
statusUnknownRecipient = gstatus.New(codes.InvalidArgument, "notification: unknown recipient")
31-
statusRecipientRequired = gstatus.New(
32-
codes.InvalidArgument,
33-
"notification: recipient must be specified",
34-
)
35-
statusSlackRecipientRequired = gstatus.New(
36-
codes.InvalidArgument,
37-
"notification: slack recipient must be specified",
38-
)
39-
statusSlackRecipientWebhookURLRequired = gstatus.New(
40-
codes.InvalidArgument,
41-
"notification: webhook URL must be specified",
42-
)
43-
statusInvalidCursor = gstatus.New(codes.InvalidArgument, "notification: cursor is invalid")
44-
statusNoCommand = gstatus.New(codes.InvalidArgument, "notification: no command")
45-
statusInvalidOrderBy = gstatus.New(codes.InvalidArgument, "environment: order_by is invalid")
46-
statusNotFound = gstatus.New(codes.NotFound, "notification: not found")
47-
statusAlreadyExists = gstatus.New(codes.AlreadyExists, "notification: already exists")
48-
statusUnauthenticated = gstatus.New(codes.Unauthenticated, "notification: unauthenticated")
49-
statusPermissionDenied = gstatus.New(codes.PermissionDenied, "notification: permission denied")
23+
statusInternal = api.NewGRPCStatus(err.NewErrorInternal(err.NotificationPackageName, "internal"))
24+
statusIDRequired = api.NewGRPCStatus(
25+
err.NewErrorInvalidArgEmpty(err.NotificationPackageName, "id must be specified", "id"),
26+
)
27+
statusNameRequired = api.NewGRPCStatus(
28+
err.NewErrorInvalidArgEmpty(err.NotificationPackageName, "name must be specified", "name"),
29+
)
30+
statusSourceTypesRequired = api.NewGRPCStatus(err.NewErrorInvalidArgEmpty(
31+
err.NotificationPackageName,
32+
"notification types must be specified",
33+
"notification_types",
34+
))
35+
statusUnknownRecipient = api.NewGRPCStatus(
36+
err.NewErrorInvalidArgUnknown(err.NotificationPackageName, "unknown recipient", "recipient"),
37+
)
38+
statusRecipientRequired = api.NewGRPCStatus(err.NewErrorInvalidArgEmpty(
39+
err.NotificationPackageName,
40+
"recipient must be specified",
41+
"recipient",
42+
))
43+
statusSlackRecipientRequired = api.NewGRPCStatus(err.NewErrorInvalidArgEmpty(
44+
err.NotificationPackageName,
45+
"slack recipient must be specified",
46+
"slack_recipient",
47+
))
48+
statusSlackRecipientWebhookURLRequired = api.NewGRPCStatus(err.NewErrorInvalidArgEmpty(
49+
err.NotificationPackageName,
50+
"webhook URL must be specified",
51+
"webhook_url",
52+
))
53+
statusInvalidCursor = api.NewGRPCStatus(
54+
err.NewErrorInvalidArgNotMatchFormat(err.NotificationPackageName, "cursor is invalid", "cursor"),
55+
)
56+
statusNoCommand = api.NewGRPCStatus(
57+
err.NewErrorInvalidArgEmpty(err.NotificationPackageName, "no command", "command"),
58+
)
59+
statusInvalidOrderBy = api.NewGRPCStatus(
60+
err.NewErrorInvalidArgNotMatchFormat(err.NotificationPackageName, "order_by is invalid", "order_by"),
61+
)
62+
statusNotFound = api.NewGRPCStatus(err.NewErrorNotFound(err.NotificationPackageName, "not found", "id"))
63+
statusAlreadyExists = api.NewGRPCStatus(
64+
err.NewErrorAlreadyExists(err.NotificationPackageName, "already exists"),
65+
)
66+
statusUnauthenticated = api.NewGRPCStatus(err.NewErrorUnauthenticated(err.NotificationPackageName, "unauthenticated"))
67+
statusPermissionDenied = api.NewGRPCStatus(
68+
err.NewErrorPermissionDenied(err.NotificationPackageName, "permission denied"),
69+
)
5070
)

0 commit comments

Comments
 (0)