apiutils

package
v0.0.0-...-3511abf Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	// MaxLimit is the maximum limit value for pagination.
	MaxLimit = 500
)

Variables

View Source
var (
	// ErrBadRequest is the returned standard error for bad requests.
	ErrBadRequest = status.Error(codes.InvalidArgument, "bad request")
	// ErrInvalidLimit is the returned standard error for invalid limit for pagination.
	ErrInvalidLimit = status.Errorf(codes.InvalidArgument,
		"Bad request: limit is required and must be <= %d", MaxLimit)
	// ErrNotFound is the returned standard error for value(s) not found.
	ErrNotFound = status.Error(codes.NotFound, "not found")
	// ErrDuplicateRecord is the returned standard error for finding duplicates.
	ErrDuplicateRecord = status.Error(codes.AlreadyExists, "duplicate record")
	// ErrInternal is the returned standard error for an internal error.
	ErrInternal = status.Error(codes.Internal, "internal server error")

	// ErrorPassthroughSet is the default set of errors that will be passed through by
	// MapAndFilterErrors without blocking or mapping.
	ErrorPassthroughSet = map[error]bool{
		ErrBadRequest:                true,
		ErrInvalidLimit:              true,
		ErrNotFound:                  true,
		ErrDuplicateRecord:           true,
		ErrInternal:                  true,
		grpcutil.ErrPermissionDenied: true,
	}

	// ErrorMapping is the default mapping of errors used by MapAndFilterErrors to, for example,
	// map errors from other application layers to what an API package will want to return.
	ErrorMapping = map[error]error{
		db.ErrNotFound:        ErrNotFound,
		db.ErrDuplicateRecord: ErrDuplicateRecord,
		db.ErrInvalidInput:    ErrBadRequest,
	}
)

Functions

func MapAndFilterErrors

func MapAndFilterErrors(err error, passthrough map[error]bool, mapping map[error]error) error

MapAndFilterErrors takes in an error at the db level and translates it into a standard error.

Types

type FieldMask

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

FieldMask is a utility type for efficiently interacting with protobuf FieldMasks in a compliant way.

func NewFieldMask

func NewFieldMask(m *field_mask.FieldMask) FieldMask

NewFieldMask initializes a FieldMask object from a protobuf FieldMask pointer. A passed-in nil or a FieldMask with zero paths will result in an empty FieldMask, which is considered to contain every field.

func (*FieldMask) FieldInSet

func (f *FieldMask) FieldInSet(field string) bool

FieldInSet answers whether the passed-in field is in the FieldMask. FieldInSet respects the FieldMask convention of treating empty FieldMasks as containing every field.

Jump to

Keyboard shortcuts

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