errors

package
v3.10.30 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2023 License: Apache-2.0 Imports: 11 Imported by: 10

Documentation

Overview

Package errors provides a way to return detailed information for an RPC request error. The error is normally JSON encoded.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBadRequest returns then requests contains invalid data
	ErrBadRequest = &Error{Code: 400}
	// ErrUnauthorized returns then user have unauthorized call
	ErrUnauthorized = &Error{Code: 401}
	// ErrForbidden returns then user have not access the resource
	ErrForbidden = &Error{Code: 403}
	// ErrNotFound returns then user specify invalid endpoint
	ErrNotFound = &Error{Code: 404}
	// ErrMethodNotAllowed returns then user try to get invalid method
	ErrMethodNotAllowed = &Error{Code: 405}
	// ErrTimeout returns then timeout exceeded
	ErrTimeout = &Error{Code: 408}
	// ErrConflict returns then request create duplicate resource
	ErrConflict = &Error{Code: 409}
	// ErrInternalServerError returns then server cant process request because of internal error
	ErrInternalServerError = &Error{Code: 500}
	// ErNotImplemented returns then server does not have desired endpoint method
	ErNotImplemented = &Error{Code: 501}
	// ErrBadGateway returns then server cant process request
	ErrBadGateway = &Error{Code: 502}
	// ErrServiceUnavailable returns then service unavailable
	ErrServiceUnavailable = &Error{Code: 503}
	// ErrGatewayTimeout returns then server have long time to process request
	ErrGatewayTimeout = &Error{Code: 504}
)
View Source
var (
	RetrayableOracleErrors = []IsRetryableFunc{
		func(err error) bool {
			errmsg := err.Error()
			switch {
			case strings.Contains(errmsg, `ORA-`):
				return true
			case strings.Contains(errmsg, `can not assign`):
				return true
			case strings.Contains(errmsg, `can't assign`):
				return true
			}
			return false
		},
	}
	RetrayablePostgresErrors = []IsRetryableFunc{
		func(err error) bool {
			errmsg := err.Error()
			switch {
			case strings.Contains(errmsg, `number of field descriptions must equal number of`):
				return true
			case strings.Contains(errmsg, `not a pointer`):
				return true
			case strings.Contains(errmsg, `values, but dst struct has only`):
				return true
			case strings.Contains(errmsg, `struct doesn't have corresponding row field`):
				return true
			case strings.Contains(errmsg, `cannot find field`):
				return true
			case strings.Contains(errmsg, `cannot scan`) || strings.Contains(errmsg, `cannot convert`):
				return true
			case strings.Contains(errmsg, `failed to connect to`):
				return true
			}
			return false
		},
	}
	RetryableMicroErrors = []IsRetryableFunc{
		func(err error) bool {
			switch verr := err.(type) {
			case *Error:
				switch verr.Code {
				case 401, 403, 408, 500, 501, 502, 503, 504:
					return true
				default:
					return false
				}
			case *retryableError:
				return true
			}
			return false
		},
	}
	RetryableGoErrors = []IsRetryableFunc{
		func(err error) bool {
			switch verr := err.(type) {
			case interface{ SafeToRetry() bool }:
				return verr.SafeToRetry()
			case interface{ Timeout() bool }:
				return verr.Timeout()
			}
			switch {
			case errors.Is(err, io.EOF), errors.Is(err, io.ErrUnexpectedEOF):
				return true
			case errors.Is(err, context.DeadlineExceeded):
				return true
			case errors.Is(err, io.ErrClosedPipe), errors.Is(err, io.ErrShortBuffer), errors.Is(err, io.ErrShortWrite):
				return true
			}
			return false
		},
	}
	RetryableGrpcErrors = []IsRetryableFunc{
		func(err error) bool {
			st, ok := status.FromError(err)
			if !ok {
				return false
			}
			switch st.Code() {
			case codes.Unavailable, codes.ResourceExhausted:
				return true
			case codes.DeadlineExceeded:
				return true
			case codes.Internal:
				switch {
				case strings.Contains(st.Message(), `transport: received the unexpected content-type "text/html; charset=UTF-8"`):
					return true
				case strings.Contains(st.Message(), io.ErrUnexpectedEOF.Error()):
					return true
				case strings.Contains(st.Message(), `stream terminated by RST_STREAM with error code: INTERNAL_ERROR`):
					return true
				}
			}
			return false
		},
	}
)

Functions

func BadGateway

func BadGateway(id, format string, args ...interface{}) error

BadGateway generates a 502 error

func BadRequest

func BadRequest(id, format string, args ...interface{}) error

BadRequest generates a 400 error.

func CodeIn added in v3.9.8

func CodeIn(err interface{}, codes ...int32) bool

CodeIn return true if err has specified code

func Conflict

func Conflict(id, format string, args ...interface{}) error

Conflict generates a 409 error.

func Equal

func Equal(err1 error, err2 error) bool

Equal tries to compare errors

func Forbidden

func Forbidden(id, format string, args ...interface{}) error

Forbidden generates a 403 error.

func GatewayTimeout

func GatewayTimeout(id, format string, args ...interface{}) error

GatewayTimeout generates a 504 error

func InternalServerError

func InternalServerError(id, format string, args ...interface{}) error

InternalServerError generates a 500 error.

func IsRetryable added in v3.10.29

func IsRetryable(err error, fns ...IsRetryableFunc) bool

IsRetryable checks error for ability to retry later

func MethodNotAllowed

func MethodNotAllowed(id, format string, args ...interface{}) error

MethodNotAllowed generates a 405 error.

func New

func New(id, detail string, code int32) error

New generates a custom error

func NotFound

func NotFound(id, format string, args ...interface{}) error

NotFound generates a 404 error.

func NotImplemented

func NotImplemented(id, format string, args ...interface{}) error

NotImplemented generates a 501 error

func Retryable added in v3.10.29

func Retryable(err error) error

Retryable returns error that can be retried later

func ServiceUnavailable

func ServiceUnavailable(id, format string, args ...interface{}) error

ServiceUnavailable generates a 503 error

func Timeout

func Timeout(id, format string, args ...interface{}) error

Timeout generates a 408 error.

func Unauthorized

func Unauthorized(id, format string, args ...interface{}) error

Unauthorized generates a 401 error.

Types

type Error

type Error struct {
	// ID holds error id or service, usually someting like my_service or id
	ID string
	// Detail holds some useful details about error
	Detail string
	// Status usually holds text of http status
	Status string
	// Code holds error code
	Code int32
}

Error type

func FromError

func FromError(err error) *Error

FromError try to convert go error to *Error

func Parse

func Parse(err string) *Error

Parse tries to parse a JSON string into an error. If that fails, it will set the given string as the error detail.

func (*Error) Error

func (e *Error) Error() string

Error satisfies error interface

func (*Error) Marshal added in v3.8.16

func (e *Error) Marshal() ([]byte, error)

Marshal returns error data

func (*Error) MarshalJSON added in v3.8.16

func (e *Error) MarshalJSON() ([]byte, error)

MarshalJSON returns error data

func (*Error) ProtoMessage added in v3.8.16

func (e *Error) ProtoMessage()

ProtoMessage noop func

func (*Error) Reset added in v3.8.16

func (e *Error) Reset()

Reset resets error

func (*Error) String added in v3.8.16

func (e *Error) String() string

String returns error as string

func (*Error) Unmarshal added in v3.8.16

func (e *Error) Unmarshal(data []byte) error

Unmarshal set error data

func (*Error) UnmarshalJSON added in v3.8.16

func (e *Error) UnmarshalJSON(data []byte) error

UnmarshalJSON set error data

type IsRetryableFunc added in v3.10.29

type IsRetryableFunc func(error) bool

Jump to

Keyboard shortcuts

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