errs

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2025 License: MIT Imports: 8 Imported by: 5

Documentation

Index

Constants

View Source
const (
	ErrCodeBadRequest     = errCode(400)
	ErrCodeUnauthorized   = errCode(401)
	ErrCodeForbidden      = errCode(403)
	ErrCodeNotFound       = errCode(404)
	ErrCodeNotAllowed     = errCode(405)
	ErrCodeRequestTimeout = errCode(408)

	ErrCodeInternal       = errCode(500)
	ErrCodeNotImplemented = errCode(501)
	ErrCodeBadGateway     = errCode(502)
	ErrCodeUnknown        = errCode(520)
)

Variables

View Source
var (
	ErrUnknown = &Err{
		Code:    ErrCodeUnknown,
		Index:   0,
		Message: "UNKNOWN",
	}
)

Functions

func ClientInterceptor

func ClientInterceptor(
	ctx context.Context,
	method string,
	req interface{},
	reply interface{},
	cc *grpc.ClientConn,
	invoker grpc.UnaryInvoker,
	opts ...grpc.CallOption,
) error

func GetGRPCInterceptor

func GetGRPCInterceptor(serviceID int) grpc.UnaryServerInterceptor

func LoggingInterceptor

func LoggingInterceptor(
	ctx context.Context,
	req interface{},
	info *grpc.UnaryServerInfo,
	handler grpc.UnaryHandler,
) (resp interface{}, err error)

func New

func New(code errCode, index int, message string) error

func ToGRPC

func ToGRPC(err *Err) error

ToGRPC returns *Err as *status.Status error

Types

type Err

type Err struct {
	Code    errCode `json:"code"`             // HTTP-коды, 400, 500 и т.д.
	Index   int     `json:"index"`            // Шестизначный индекс ошибки, где первые две цифры - номер сервиса, остальные - номер ошибки в сервисе
	Message string  `json:"message"`          // Сообщение, которое принимают фронты или другие сервисы
	Params  any     `json:"params,omitempty"` // Дополнительные параметры
}

Err заменяет ошибку для фронта

func Parse

func Parse(err error) (customErr *Err, ok bool)

func (*Err) Error

func (e *Err) Error() string

type ErrTemplate

type ErrTemplate struct {
	Code    errCode `json:"code"`    // HTTP-коды, 400, 500 и т.д.
	Index   int     `json:"index"`   // Шестизначный индекс ошибки, где первые две цифры - номер сервиса, остальные - номер ошибки в сервисе
	Message string  `json:"message"` // Сообщение, которое принимают фронты или другие сервисы
}

ErrTemplate - шаблон для ошибки, для которой дополнительно можно указать параметры в месте возникновения

func NewTemplate

func NewTemplate(code errCode, index int, message string) *ErrTemplate

func (*ErrTemplate) New

func (t *ErrTemplate) New(params any) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL