errors

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2024 License: Apache-2.0 Imports: 10 Imported by: 6

README

Errors

The standardizing of errors to be used in Dapr based on the gRPC Richer Error Model and accepted dapr/proposal.

Usage

Define the error

import kitErrors "github.com/dapr/kit/errors"

// Define error in dapr pkg/api/errors/<building_block>.go
func PubSubNotFound(name string, pubsubType string, metadata map[string]string) error {
	message := fmt.Sprintf("pubsub %s is not found", name)

	return kitErrors.NewBuilder(
		grpcCodes.NotFound,
		http.StatusBadRequest,
		message,
		kitErrors.CodePrefixPubSub+kitErrors.CodeNotFound,
	).
		WithErrorInfo(kitErrors.CodePrefixPubSub+kitErrors.CodeNotFound, metadata).
		WithResourceInfo(pubsubType, name, "", message).
		Build()
}

Use the error

import apiErrors "github.com/dapr/dapr/pkg/api/errors"

// Use error in dapr and pass in relevant information
err = apiErrors.PubSubNotFound(pubsubName, pubsubType, metadata)

Documentation

Index

Constants

View Source
const (
	// Generic
	CodeNotFound      = "NOT_FOUND"
	CodeNotConfigured = "NOT_CONFIGURED"
	CodeNotSupported  = "NOT_SUPPORTED"
	CodeIllegalKey    = "ILLEGAL_KEY"

	// Components
	CodePrefixStateStore         = "DAPR_STATE_"
	CodePrefixPubSub             = "DAPR_PUBSUB_"
	CodePrefixBindings           = "DAPR_BINDING_"
	CodePrefixSecretStore        = "DAPR_SECRET_"
	CodePrefixConfigurationStore = "DAPR_CONFIGURATION_"
	CodePrefixLock               = "DAPR_LOCK_"
	CodePrefixNameResolution     = "DAPR_NAME_RESOLUTION_"
	CodePrefixMiddleware         = "DAPR_MIDDLEWARE_"

	// State
	CodePostfixGetStateFailed      = "GET_STATE_FAILED"
	CodePostfixTooManyTransactions = "TOO_MANY_TRANSACTIONS"
	CodePostfixQueryFailed         = "QUERY_FAILED"
)
View Source
const (
	Domain = "dapr.io"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

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

Error implements the Error interface and the interface that complies with "google.golang.org/grpc/status".FromError(). It can be used to send errors to HTTP and gRPC servers, indicating the correct status code for each.

func FromError

func FromError(err error) (*Error, bool)

FromError takes in an error and returns back the kitError if it's that type under the hood

func (*Error) AddDetails

func (e *Error) AddDetails(details ...proto.Message) *Error

Allow details to be mutable and added to the error in runtime

func (Error) Error

func (e Error) Error() string

Error implements the error interface.

func (Error) GRPCStatus

func (e Error) GRPCStatus() *status.Status

GRPCStatus returns the gRPC status.Status object.

func (*Error) GrpcStatusCode

func (e *Error) GrpcStatusCode() grpcCodes.Code

GrpcStatusCode gets the error grpc code

func (*Error) HTTPStatusCode

func (e *Error) HTTPStatusCode() int

HTTPStatusCode gets the error http code

func (*Error) Is

func (e *Error) Is(targetI error) bool

Is implements the interface that checks if the error matches the given one.

func (Error) JSONErrorValue

func (e Error) JSONErrorValue() []byte

JSONErrorValue implements the errorResponseValue interface.

func (Error) String

func (e Error) String() string

String returns the string representation.

type ErrorBuilder

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

ErrorBuilder is used to build the error

func NewBuilder

func NewBuilder(grpcCode grpcCodes.Code, httpCode int, message string, tag string) *ErrorBuilder

NewBuilder create a new ErrorBuilder using the supplied required error fields

func (*ErrorBuilder) Build

func (b *ErrorBuilder) Build() error

Build builds our error

func (*ErrorBuilder) WithDetails

func (b *ErrorBuilder) WithDetails(details ...proto.Message) *ErrorBuilder

WithDetails is used to pass any error details to the Error struct.

func (*ErrorBuilder) WithErrorInfo

func (b *ErrorBuilder) WithErrorInfo(reason string, metadata map[string]string) *ErrorBuilder

WithErrorInfo adds error information to the Error struct.

func (*ErrorBuilder) WithFieldViolation

func (b *ErrorBuilder) WithFieldViolation(fieldName string, msg string) *ErrorBuilder

WithFieldViolation is used to pass FieldViolation error details to the Error struct.

func (*ErrorBuilder) WithHelp

func (b *ErrorBuilder) WithHelp(links []*errdetails.Help_Link) *ErrorBuilder

WithHelp is used to pass Help error details to the Error struct.

func (b *ErrorBuilder) WithHelpLink(url string, description string) *ErrorBuilder

WithHelpLink is used to pass HelpLink error details to the Error struct.

func (*ErrorBuilder) WithResourceInfo

func (b *ErrorBuilder) WithResourceInfo(resourceType string, resourceName string, owner string, description string) *ErrorBuilder

WithResourceInfo is used to pass ResourceInfo error details to the Error struct.

Jump to

Keyboard shortcuts

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