mirror of
https://github.com/harness/drone.git
synced 2025-05-04 23:22:42 +08:00

This change adds the following: - Inject APP server zerolog RequestID as metadata into all gitrpc calls from client side. - Inject Zerolog logger into context with common fields set (like service, method, requestID, ...). This allows for better request tracking within gitrpc, but also request tracking across services we extract the requestID send by the grpc client - Modify http logs to use http. prefix for common http related logging annotations.
83 lines
2.4 KiB
Go
83 lines
2.4 KiB
Go
// Copyright 2022 Harness Inc. All rights reserved.
|
|
// Use of this source code is governed by the Polyform Free Trial License
|
|
// that can be found in the LICENSE.md file for this repository.
|
|
|
|
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"reflect"
|
|
|
|
"github.com/harness/gitness/gitrpc/internal/types"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type ErrInterceptor struct {
|
|
}
|
|
|
|
func NewErrInterceptor() ErrInterceptor {
|
|
return ErrInterceptor{}
|
|
}
|
|
|
|
func (i ErrInterceptor) UnaryInterceptor() grpc.UnaryServerInterceptor {
|
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
|
|
handler grpc.UnaryHandler) (interface{}, error) {
|
|
value, err := handler(ctx, req)
|
|
if (value == nil || reflect.ValueOf(value).IsNil()) && err == nil {
|
|
return nil, status.Error(codes.Internal, "service returned no error and no object")
|
|
}
|
|
err = processError(err)
|
|
return value, err
|
|
}
|
|
}
|
|
|
|
func (i ErrInterceptor) StreamInterceptor() grpc.StreamServerInterceptor {
|
|
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo,
|
|
handler grpc.StreamHandler) error {
|
|
err := handler(srv, stream)
|
|
err = processError(err)
|
|
return err
|
|
}
|
|
}
|
|
|
|
func processError(err error) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
|
|
// check if error already is grpc error
|
|
// TODO: this should be removed once all error handling has been refactored.
|
|
if status, ok := status.FromError(err); ok {
|
|
return status.Err()
|
|
}
|
|
|
|
message := err.Error()
|
|
|
|
switch {
|
|
case errors.Is(err, context.DeadlineExceeded):
|
|
return status.Error(codes.DeadlineExceeded, message)
|
|
case errors.Is(err, types.ErrNotFound):
|
|
return status.Error(codes.NotFound, message)
|
|
case errors.Is(err, types.ErrAlreadyExists):
|
|
return status.Error(codes.AlreadyExists, message)
|
|
case errors.Is(err, types.ErrInvalidArgument):
|
|
return status.Error(codes.InvalidArgument, message)
|
|
case errors.Is(err, types.ErrInvalidPath):
|
|
return status.Error(codes.InvalidArgument, message)
|
|
case errors.Is(err, types.ErrUndefinedAction):
|
|
return status.Error(codes.InvalidArgument, message)
|
|
case errors.Is(err, types.ErrHeaderCannotBeEmpty):
|
|
return status.Error(codes.InvalidArgument, message)
|
|
case errors.Is(err, types.ErrActionListEmpty):
|
|
return status.Error(codes.FailedPrecondition, message)
|
|
case errors.Is(err, types.ErrContentSentBeforeAction):
|
|
return status.Error(codes.FailedPrecondition, message)
|
|
default:
|
|
return status.Errorf(codes.Unknown, message)
|
|
}
|
|
}
|