Skip to content
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
17 changes: 2 additions & 15 deletions pkg/coderef/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"google.golang.org/grpc/status"

accountclient "github.com/bucketeer-io/bucketeer/v2/pkg/account/client"
"github.com/bucketeer-io/bucketeer/v2/pkg/api/api"
"github.com/bucketeer-io/bucketeer/v2/pkg/locale"
"github.com/bucketeer-io/bucketeer/v2/pkg/log"
"github.com/bucketeer-io/bucketeer/v2/pkg/pubsub/publisher"
Expand Down Expand Up @@ -135,21 +136,7 @@ func (s *CodeReferenceService) checkEnvironmentRole(
}
return nil, dt.Err()
default:
s.logger.Error(
"Failed to check role",
log.FieldsFromIncomingContext(ctx).AddFields(
zap.Error(err),
zap.String("environmentId", environmentID),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
}
return editor, nil
Expand Down
136 changes: 16 additions & 120 deletions pkg/coderef/api/code_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"go.uber.org/zap"
"google.golang.org/genproto/googleapis/rpc/errdetails"

"github.com/bucketeer-io/bucketeer/v2/pkg/api/api"
"github.com/bucketeer-io/bucketeer/v2/pkg/coderef/domain"
"github.com/bucketeer-io/bucketeer/v2/pkg/coderef/storage"
domainevent "github.com/bucketeer-io/bucketeer/v2/pkg/domainevent/domain"
Expand Down Expand Up @@ -124,14 +125,7 @@ func (s *CodeReferenceService) GetCodeReference(
zap.String("environmentId", req.EnvironmentId),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
codeRef.SourceUrl = generateSourceURL(&codeRef.CodeReference)
codeRef.BranchUrl = generateBranchURL(&codeRef.CodeReference)
Expand Down Expand Up @@ -213,14 +207,7 @@ func (s *CodeReferenceService) ListCodeReferences(
zap.String("environmentId", req.EnvironmentId),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
protoRefs := make([]*proto.CodeReference, 0, len(codeRefs))
for _, ref := range codeRefs {
Expand Down Expand Up @@ -274,14 +261,7 @@ func (s *CodeReferenceService) CreateCodeReference(
zap.Error(err),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
codeRefStorage := storage.NewCodeReferenceStorage(s.mysqlClient)
if err := codeRefStorage.CreateCodeReference(ctx, codeRef); err != nil {
Expand All @@ -292,14 +272,7 @@ func (s *CodeReferenceService) CreateCodeReference(
zap.String("id", codeRef.Id),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
createEvent, err := domainevent.NewEvent(
editor,
Expand Down Expand Up @@ -334,14 +307,7 @@ func (s *CodeReferenceService) CreateCodeReference(
zap.Error(err),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
if err := s.publisher.Publish(ctx, createEvent); err != nil {
s.logger.Error(
Expand All @@ -350,14 +316,7 @@ func (s *CodeReferenceService) CreateCodeReference(
zap.Error(err),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
return &proto.CreateCodeReferenceResponse{CodeReference: &codeRef.CodeReference}, nil
}
Expand Down Expand Up @@ -404,14 +363,7 @@ func (s *CodeReferenceService) UpdateCodeReference(
zap.String("environmentId", req.EnvironmentId),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return statusInternal.Err()
}
return dt.Err()
return api.NewGRPCStatus(err).Err()
}
updatedCodeRef, err = codeRef.Update(
req.FilePath,
Expand All @@ -431,14 +383,7 @@ func (s *CodeReferenceService) UpdateCodeReference(
zap.String("id", req.Id),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return statusInternal.Err()
}
return dt.Err()
return api.NewGRPCStatus(err).Err()
}
if err := codeRefStorage.UpdateCodeReference(ctx, updatedCodeRef); err != nil {
s.logger.Error(
Expand All @@ -448,14 +393,7 @@ func (s *CodeReferenceService) UpdateCodeReference(
zap.String("id", req.Id),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return statusInternal.Err()
}
return dt.Err()
return api.NewGRPCStatus(err).Err()
}
return nil
})
Expand Down Expand Up @@ -490,14 +428,7 @@ func (s *CodeReferenceService) UpdateCodeReference(
zap.Error(err),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
if err := s.publisher.Publish(ctx, updateEvent); err != nil {
s.logger.Error(
Expand All @@ -506,14 +437,7 @@ func (s *CodeReferenceService) UpdateCodeReference(
zap.Error(err),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
return &proto.UpdateCodeReferenceResponse{CodeReference: &updatedCodeRef.CodeReference}, nil
}
Expand Down Expand Up @@ -556,14 +480,7 @@ func (s *CodeReferenceService) DeleteCodeReference(
zap.String("environmentId", req.EnvironmentId),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
if err := codeRefStorage.DeleteCodeReference(ctx, codeRef.Id); err != nil {
s.logger.Error(
Expand All @@ -573,14 +490,7 @@ func (s *CodeReferenceService) DeleteCodeReference(
zap.String("id", req.Id),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
deleteEvent, err := domainevent.NewEvent(
editor,
Expand All @@ -602,14 +512,7 @@ func (s *CodeReferenceService) DeleteCodeReference(
zap.Error(err),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
if err := s.publisher.Publish(ctx, deleteEvent); err != nil {
s.logger.Error(
Expand All @@ -618,14 +521,7 @@ func (s *CodeReferenceService) DeleteCodeReference(
zap.Error(err),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
return &proto.DeleteCodeReferenceResponse{}, nil
}
Expand Down
54 changes: 39 additions & 15 deletions pkg/coderef/api/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,46 @@
package api

import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/bucketeer-io/bucketeer/v2/pkg/api/api"
bkterr "github.com/bucketeer-io/bucketeer/v2/pkg/error"
)

var (
statusInternal = status.New(codes.Internal, "coderef: internal")
statusInvalidCursor = status.New(codes.InvalidArgument, "coderef: invalid cursor")
statusMissingID = status.New(codes.InvalidArgument, "coderef: missing id")
statusMissingFeatureID = status.New(codes.InvalidArgument, "coderef: missing feature_id")
statusMissingFilePath = status.New(codes.InvalidArgument, "coderef: missing file_path")
statusMissingLineNumber = status.New(codes.InvalidArgument, "coderef: missing line_number")
statusMissingCodeSnippet = status.New(codes.InvalidArgument, "coderef: missing code_snippet")
statusMissingContentHash = status.New(codes.InvalidArgument, "coderef: missing content_hash")
statusMissingRepositoryInfo = status.New(codes.InvalidArgument, "coderef: missing repository info")
statusInvalidRepositoryType = status.New(codes.InvalidArgument, "coderef: invalid repository type")
statusNotFound = status.New(codes.NotFound, "coderef: not found")
statusUnauthenticated = status.New(codes.Unauthenticated, "coderef: unauthenticated")
statusPermissionDenied = status.New(codes.PermissionDenied, "coderef: permission denied")
statusInternal = api.NewGRPCStatus(bkterr.NewErrorInternal(bkterr.CoderefPackageName, "internal"))
statusInvalidCursor = api.NewGRPCStatus(
bkterr.NewErrorInvalidArgEmpty(bkterr.CoderefPackageName, "invalid cursor", "cursor"),
)
statusMissingID = api.NewGRPCStatus(
bkterr.NewErrorInvalidArgEmpty(bkterr.CoderefPackageName, "missing id", "id"),
)
statusMissingFeatureID = api.NewGRPCStatus(
bkterr.NewErrorInvalidArgEmpty(bkterr.CoderefPackageName, "missing feature_id", "feature_id"),
)
statusMissingFilePath = api.NewGRPCStatus(
bkterr.NewErrorInvalidArgEmpty(bkterr.CoderefPackageName, "missing file_path", "file_path"),
)
statusMissingLineNumber = api.NewGRPCStatus(
bkterr.NewErrorInvalidArgNotMatchFormat(bkterr.CoderefPackageName, "missing line_number", "line_number"),
)
statusMissingCodeSnippet = api.NewGRPCStatus(
bkterr.NewErrorInvalidArgEmpty(bkterr.CoderefPackageName, "missing code_snippet", "code_snippet"),
)
statusMissingContentHash = api.NewGRPCStatus(
bkterr.NewErrorInvalidArgEmpty(bkterr.CoderefPackageName, "missing content_hash", "content_hash"),
)
statusMissingRepositoryInfo = api.NewGRPCStatus(
bkterr.NewErrorInvalidArgEmpty(bkterr.CoderefPackageName, "missing repository info", "repository_info"),
)
statusInvalidRepositoryType = api.NewGRPCStatus(
bkterr.NewErrorInvalidArgUnknown(bkterr.CoderefPackageName, "invalid repository type", "repository_type"),
)
statusNotFound = api.NewGRPCStatus(
bkterr.NewErrorNotFound(bkterr.CoderefPackageName, "not found", "coderef"),
)
statusUnauthenticated = api.NewGRPCStatus(
bkterr.NewErrorUnauthenticated(bkterr.CoderefPackageName, "unauthenticated"),
)
statusPermissionDenied = api.NewGRPCStatus(
bkterr.NewErrorPermissionDenied(bkterr.CoderefPackageName, "permission denied"),
)
)
31 changes: 0 additions & 31 deletions pkg/coderef/domain/errors.go

This file was deleted.

11 changes: 9 additions & 2 deletions pkg/coderef/storage/code_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"

"github.com/bucketeer-io/bucketeer/v2/pkg/coderef/domain"
bkterr "github.com/bucketeer-io/bucketeer/v2/pkg/error"
"github.com/bucketeer-io/bucketeer/v2/pkg/storage/v2/mysql"
)

Expand All @@ -44,8 +45,14 @@ type contextKey string
const transactionKey contextKey = "transaction"

var (
ErrCodeReferenceNotFound = errors.New("coderef: code reference not found")
ErrCodeReferenceUnexpectedAffectedRows = errors.New("coderef: code reference unexpected affected rows")
ErrCodeReferenceNotFound = bkterr.NewErrorNotFound(
bkterr.CoderefPackageName,
"code reference not found", "code_reference",
)
ErrCodeReferenceUnexpectedAffectedRows = bkterr.NewErrorUnexpectedAffectedRows(
bkterr.CoderefPackageName,
"code reference unexpected affected rows",
)
)

type codeReferenceStorage struct {
Expand Down
Loading