zeerr

package
v0.0.9-alpha Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithLocale

func ContextWithLocale(ctx context.Context, lang language.Tag) context.Context

func MakeUID

func MakeUID(domain, namespace, code string) string

MakeUID creates a unique error identifier. Format: <domain>/<namespace>/<code> Example: "acme.com/auth/invalid_credentials"

Types

type Arguments

type Arguments map[string]interface{}

Arguments represents Error arguments.

type Error

type Error interface {
	// UID is a unique identifier of the error.
	UID() string
	// Domain represents error's domain.
	// Usually it's a domain owned by the organization.
	Domain() string
	// Namespace can represent a service, package, or module.
	// For example all the errors under the "auth" namespaces
	// are related to the authentication and authorization.
	Namespace() string
	// Code returns error's code.
	Code() string
	// GRPCCode is a gRPC status code that will be returned to the
	// client in case the error was returned via gRPC.
	GRPCCode() codes.Code
	// HTTPCode is an HTTP status code that will be returned to the
	// client in case the error was returned via HTTP.
	HTTPCode() int
	// InternalMsg is used to describe the error and not exposed to
	// external consumers of the service.
	InternalMsg() string
	// PublicMsg provides a user-friendly message, which is localized based
	// on the user's locale and will be returned to the client.
	PublicMsg() string
	// Args returns error's arguments.
	Args() Arguments
	// Causes returns a list of errors that caused the current error.
	// It's usually used to nest other errors.
	// Image the usecase, when the user submits a complex form and the latter
	// contains multiple sections with different fields.
	// Sometimes, it is required to validate each field and return errors
	// in a structured manner, so they can be easily rendered on the client side.
	Causes() []Error
	// WithCauses is used to attach causes to the error.
	WithCauses(causes ...Error) Error
	// Error implements the error interface.
	Error() string
}

Error represents standardized error.

The interface should not be implemented directly. Use generated errors and constructors to create new errors instances.

func NewError

func NewError(
	ctx context.Context,
	localizer Localizer,
	code string,
	domain string,
	namespace string,
	httpCode int,
	grpcCode codes.Code,
	internalMsgTmpl *template.Template,
	args Arguments,
) Error

NewError creates a new error. NOTE: The constructor is meant to be used only by the generated code.

type LocaleCtxKeyType

type LocaleCtxKeyType struct{}

type Localizer

type Localizer interface {
	// LocalizePublicMessage localizes error's public message.
	// Usually it's used only during the error construction.
	LocalizePublicMessage(errUID string, lang language.Tag, args Arguments) string
}

Localizer is responsible for localizing public and internal error messages.

Jump to

Keyboard shortcuts

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