Skip to content

Commit

Permalink
Refactor error handling on grpc_handlers section (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
AssassinRobot authored Aug 10, 2024
1 parent eedb3dc commit f794aed
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
15 changes: 8 additions & 7 deletions delivery/grpc/handlers/auth_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
grpc_model "github.com/kavkaco/Kavka-Core/delivery/grpc/model"
"github.com/kavkaco/Kavka-Core/internal/service/auth"
authv1 "github.com/kavkaco/Kavka-Core/protobuf/gen/go/protobuf/auth/v1"
"google.golang.org/genproto/googleapis/rpc/code"
"google.golang.org/protobuf/types/known/durationpb"
)

Expand All @@ -22,7 +23,7 @@ func NewAuthGrpcHandler(authService auth.AuthService) AuthGrpcServer {
func (a AuthGrpcServer) Login(ctx context.Context, req *connect.Request[authv1.LoginRequest]) (*connect.Response[authv1.LoginResponse], error) {
user, accessToken, refreshToken, varror := a.authService.Login(ctx, req.Msg.Email, req.Msg.Password)
if varror != nil {
return nil, grpc_helpers.GrpcVarror(varror, connect.CodeInvalidArgument)
return nil, grpc_helpers.GrpcVarror(varror, connect.Code(code.Code_INTERNAL))
}

res := connect.NewResponse(&authv1.LoginResponse{
Expand All @@ -37,7 +38,7 @@ func (a AuthGrpcServer) Login(ctx context.Context, req *connect.Request[authv1.L
func (a AuthGrpcServer) Register(ctx context.Context, req *connect.Request[authv1.RegisterRequest]) (*connect.Response[authv1.RegisterResponse], error) {
_, varror := a.authService.Register(ctx, req.Msg.Name, req.Msg.LastName, req.Msg.Username, req.Msg.Email, req.Msg.Password, req.Msg.VerifyEmailRedirectUrl)
if varror != nil {
return nil, grpc_helpers.GrpcVarror(varror, connect.CodeInvalidArgument)
return nil, grpc_helpers.GrpcVarror(varror, connect.Code(code.Code_INVALID_ARGUMENT))
}

res := connect.NewResponse(&authv1.RegisterResponse{})
Expand All @@ -48,7 +49,7 @@ func (a AuthGrpcServer) Register(ctx context.Context, req *connect.Request[authv
func (a AuthGrpcServer) Authenticate(ctx context.Context, req *connect.Request[authv1.AuthenticateRequest]) (*connect.Response[authv1.AuthenticateResponse], error) {
user, varror := a.authService.Authenticate(ctx, req.Msg.AccessToken)
if varror != nil {
return nil, grpc_helpers.GrpcVarror(varror, connect.CodePermissionDenied)
return nil, grpc_helpers.GrpcVarror(varror, connect.Code(code.Code_PERMISSION_DENIED))
}

res := connect.NewResponse(&authv1.AuthenticateResponse{
Expand All @@ -71,7 +72,7 @@ func (a AuthGrpcServer) ChangePassword(ctx context.Context, req *connect.Request
func (a AuthGrpcServer) RefreshToken(ctx context.Context, req *connect.Request[authv1.RefreshTokenRequest]) (*connect.Response[authv1.RefreshTokenResponse], error) {
newAccessToken, varror := a.authService.RefreshToken(ctx, req.Msg.UserId, req.Msg.RefreshToken)
if varror != nil {
return nil, grpc_helpers.GrpcVarror(varror, connect.CodeUnavailable)
return nil, grpc_helpers.GrpcVarror(varror, connect.Code(code.Code_UNAVAILABLE))
}

res := connect.NewResponse(&authv1.RefreshTokenResponse{
Expand All @@ -84,7 +85,7 @@ func (a AuthGrpcServer) RefreshToken(ctx context.Context, req *connect.Request[a
func (a AuthGrpcServer) SendResetPassword(ctx context.Context, req *connect.Request[authv1.SendResetPasswordRequest]) (*connect.Response[authv1.SendResetPasswordResponse], error) {
resetPasswordToken, timeout, varror := a.authService.SendResetPassword(ctx, req.Msg.Email, req.Msg.ResetPasswordRedirectUrl)
if varror != nil {
return nil, grpc_helpers.GrpcVarror(varror, connect.CodeUnavailable)
return nil, grpc_helpers.GrpcVarror(varror, connect.Code(code.Code_UNAVAILABLE))
}

timeoutProto := durationpb.New(timeout)
Expand All @@ -99,7 +100,7 @@ func (a AuthGrpcServer) SendResetPassword(ctx context.Context, req *connect.Requ
func (a AuthGrpcServer) SubmitResetPassword(ctx context.Context, req *connect.Request[authv1.SubmitResetPasswordRequest]) (*connect.Response[authv1.SubmitResetPasswordResponse], error) {
varror := a.authService.SubmitResetPassword(ctx, req.Msg.ResetPasswordToken, req.Msg.NewPassword)
if varror != nil {
return nil, grpc_helpers.GrpcVarror(varror, connect.CodeUnavailable)
return nil, grpc_helpers.GrpcVarror(varror, connect.Code(code.Code_UNAVAILABLE))
}

res := connect.NewResponse(&authv1.SubmitResetPasswordResponse{})
Expand All @@ -109,7 +110,7 @@ func (a AuthGrpcServer) SubmitResetPassword(ctx context.Context, req *connect.Re
func (a AuthGrpcServer) VerifyEmail(ctx context.Context, req *connect.Request[authv1.VerifyEmailRequest]) (*connect.Response[authv1.VerifyEmailResponse], error) {
varror := a.authService.VerifyEmail(ctx, req.Msg.VerifyEmailToken)
if varror != nil {
return nil, grpc_helpers.GrpcVarror(varror, connect.CodeUnavailable)
return nil, grpc_helpers.GrpcVarror(varror, connect.Code(code.Code_UNAVAILABLE))
}

res := connect.NewResponse(&authv1.VerifyEmailResponse{})
Expand Down
6 changes: 2 additions & 4 deletions delivery/grpc/handlers/chat_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
chatv1 "github.com/kavkaco/Kavka-Core/protobuf/gen/go/protobuf/chat/v1"
"github.com/kavkaco/Kavka-Core/protobuf/gen/go/protobuf/chat/v1/chatv1connect"
chatv1model "github.com/kavkaco/Kavka-Core/protobuf/gen/go/protobuf/model/chat/v1"
"google.golang.org/genproto/googleapis/rpc/code"
)

type chatHandler struct {
Expand All @@ -32,10 +33,7 @@ func (h chatHandler) CreateChannel(ctx context.Context, req *connect.Request[cha

chat, varror := h.chatService.CreateChannel(ctx, userID, req.Msg.Title, req.Msg.Username, req.Msg.Description)
if varror != nil {
connectErr := connect.NewError(connect.CodeUnavailable, varror.Error)
varrorDetail, _ := grpc_helpers.VarrorAsGrpcErrDetails(varror)
connectErr.AddDetail(varrorDetail)
return nil, connectErr
return nil, grpc_helpers.GrpcVarror(varror, connect.Code(code.Code_UNAVAILABLE))
}

chatGrpcModel, err := grpc_model.TransformChatToGrpcModel(*chat)
Expand Down

0 comments on commit f794aed

Please sign in to comment.