errcode

package
v2.0.5 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 21 Imported by: 2

README

errcode

errcode adds typed business error codes with protobuf and gRPC compatibility on top of the core errors package.

Quick Start

import (
    "github.com/pubgo/funk/v2/errors/errcode"
    "github.com/pubgo/funk/v2/proto/errorpb"
)

const notFound = "demo.user.not_found"

func init() {
    errcode.MustRegisterErrCode(&errorpb.ErrCode{
        Code:       404,
        Message:    "user not found",
        Name:       notFound,
        StatusCode: errorpb.Code_NotFound,
    })
}

func loadUser() error {
    code, ok := errcode.LookupErrCode(notFound)
    if !ok {
        return errcode.NewCodeErr(&errorpb.ErrCode{Name: "demo.internal", Message: "missing code"})
    }
    return errcode.NewCodeErr(code)
}

Registry API

Function Description
RegisterErrCode(code) Register and return an error on duplicate names
MustRegisterErrCode(code) Register and panic on failure
LookupErrCode(name) Clone a registered code by name
GetErrCodes() Return all registered codes
RegisterErrCodes(code) Deprecated alias of MustRegisterErrCode

Generated protobuf plugins typically call RegisterErrCodes during package init.

Error Construction

Function Description
NewCodeErr(code, details...) Create a typed code error
NewCodeErrWithMsg(code, msg, details...) Override the message
WrapCode(err, code) Attach a code to an existing error
ParseError(err) Extract *errorpb.ErrCode from any error

Protocol Mapping

Function Description
ConvertErr2Status(code) Build gRPC status from protobuf code
Err2GrpcCode(err) Map standard errors to gRPC codes
GrpcCodeToHTTP(code) Map gRPC codes to HTTP status
Http2GrpcCode(status) Map HTTP status to gRPC codes

See Also

Documentation

Overview

Package errcode provides error code handling with gRPC compatibility.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloneAndCheck

func CloneAndCheck(code *errorpb.ErrCode) *errorpb.ErrCode

CloneAndCheck clones and validates an error code.

func ConvertErr2Status

func ConvertErr2Status(errCode *errorpb.ErrCode) *status.Status

ConvertErr2Status converts an error code to a gRPC status. Internal conversion, so that when err=nil, OK information is included in monitoring data.

func Err2GrpcCode

func Err2GrpcCode(err error) codes.Code

Err2GrpcCode converts a standard Go error into its canonical code. Note that this is only used to translate the error returned by the server applications.

func GetErrCodes

func GetErrCodes() []*errorpb.ErrCode

GetErrCodes returns all registered error codes.

func GrpcCodeToHTTP

func GrpcCodeToHTTP(code codes.Code) int

GrpcCodeToHTTP converts a gRPC error code into the corresponding HTTP response status. See: https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto

func Http2GrpcCode

func Http2GrpcCode(code int32) codes.Code

Http2GrpcCode converts an HTTP status code to a gRPC code.

func IsGrpcAcceptable

func IsGrpcAcceptable(err error) bool

IsGrpcAcceptable checks if a gRPC error is acceptable for retry.

func LookupErrCode added in v2.0.5

func LookupErrCode(name string) (*errorpb.ErrCode, bool)

LookupErrCode returns a registered error code by name.

func MustProtoToAny

func MustProtoToAny(p proto.Message) *anypb.Any

func MustRegisterErrCode added in v2.0.5

func MustRegisterErrCode(code *errorpb.ErrCode)

MustRegisterErrCode registers an error code and panics on failure.

func MustStructToAny

func MustStructToAny(p map[string]any) *anypb.Any

func MustTagsToAny

func MustTagsToAny(tags errors.Tags) []*anypb.Any

func NewCodeErr

func NewCodeErr(code *errorpb.ErrCode, details ...proto.Message) error

func NewCodeErrWithMap

func NewCodeErrWithMap(code *errorpb.ErrCode, details ...map[string]any) error

func NewCodeErrWithMsg

func NewCodeErrWithMsg(code *errorpb.ErrCode, msg string, details ...proto.Message) error

func ParseErrToPb

func ParseErrToPb(err error) proto.Message

func ParseError

func ParseError(err error) *errorpb.ErrCode

ParseError tries to convert an error to *Error. It supports wrapped errors.

func RegisterErrCode added in v2.0.5

func RegisterErrCode(code *errorpb.ErrCode) error

RegisterErrCode registers an error code and returns an error when the name already exists.

func RegisterErrCodes

func RegisterErrCodes(code *errorpb.ErrCode) error

RegisterErrCodes registers an error code and panics when the name already exists. Deprecated: use RegisterErrCode or MustRegisterErrCode.

func WrapCode

func WrapCode(err error, code *errorpb.ErrCode) error

Types

type ErrCode

type ErrCode struct {
	// contains filtered or unexported fields
}

ErrCode represents an error with code information.

func (*ErrCode) As

func (t *ErrCode) As(target any) bool

func (*ErrCode) Error

func (t *ErrCode) Error() string

func (*ErrCode) Format

func (t *ErrCode) Format(f fmt.State, verb rune)

func (*ErrCode) ID

func (t *ErrCode) ID() string

func (*ErrCode) Is

func (t *ErrCode) Is(err error) bool

func (*ErrCode) MarshalJSON

func (t *ErrCode) MarshalJSON() ([]byte, error)

func (*ErrCode) Proto

func (t *ErrCode) Proto() proto.Message

func (*ErrCode) String

func (t *ErrCode) String() string

func (*ErrCode) Unwrap

func (t *ErrCode) Unwrap() error

type ErrorProto

type ErrorProto interface {
	error
	Proto() proto.Message
}

ErrorProto is an interface for errors that can be converted to protobuf messages.

type GRPCStatus

type GRPCStatus interface {
	GRPCStatus() *status.Status
}

GRPCStatus is an interface for errors that have gRPC status.

Jump to

Keyboard shortcuts

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