Documentation ¶
Index ¶
- Constants
- Variables
- func ContextWithValue(ctx context.Context, key any, val any) context.Context
- func DefaultErrorFormatter(s *Status) string
- func DoHandlerProxy(ctx context.Context) func(ctx any, r *http.Request, body any) (any, *Status)
- func EnvStr() string
- func FormatErrors(name string, errs []error) string
- func FormatTrace(name string, trace []string) string
- func GetOrCreateRequestId(t any) string
- func IsDebugEnvironment() bool
- func IsErrors(errs []error) bool
- func IsNil(a any) bool
- func IsProdEnvironment() bool
- func IsProxyable(ctx context.Context) ([]any, bool)
- func IsStageEnvironment() bool
- func IsTestEnvironment() bool
- func LookupEnv(name string) (string, error)
- func NewInvalidBodyTypeError(t any) error
- func NewProxyContext(ctx context.Context, proxy any) context.Context
- func NewRequestContext(req *http.Request) context.Context
- func NewRequestIdContext(ctx context.Context, requestId string) context.Context
- func OriginInstanceId() string
- func OriginRegion() string
- func OriginService() string
- func OriginString() string
- func OriginSubZone() string
- func OriginUrn(nid string, nss, resource string) string
- func OriginZone() string
- func PathFromUri(rawUri string) string
- func RequestId(t any) string
- func RequestIdFromContext(ctx any) string
- func SetFormatOutput(fn FormatOutput)
- func SetOrigin(region, zone, subZone, service, instanceId string)
- func SetProdEnvironment()
- func SetStageEnvironment()
- func SetTestEnvironment()
- type BypassError
- type DoHandler
- type ErrorHandler
- type FormatOutput
- type HttpHandler
- type LogError
- type Nillable
- type Origin
- type PostHandler
- type Status
- func (s *Status) AddLocation(location string) *Status
- func (s *Status) Code() int
- func (s *Status) Content() any
- func (s *Status) ContentString() string
- func (s *Status) CopyHeader(header http.Header) *Status
- func (s *Status) Description() string
- func (s *Status) Duration() time.Duration
- func (s *Status) Errors() []error
- func (s *Status) ErrorsHandled() bool
- func (s *Status) FirstError() error
- func (s *Status) Header() http.Header
- func (s *Status) Http() int
- func (s *Status) IsContent() bool
- func (s *Status) IsErrors() bool
- func (s *Status) Location() []string
- func (s *Status) NotFound() bool
- func (s *Status) OK() bool
- func (s *Status) RemoveContent()
- func (s *Status) RequestId() string
- func (s *Status) SetCode(code int) *Status
- func (s *Status) SetContent(content any, jsonContent bool) *Status
- func (s *Status) SetContentLocation(location string) *Status
- func (s *Status) SetContentType(str string) *Status
- func (s *Status) SetContentTypeAndLocation(location string) *Status
- func (s *Status) SetDuration(duration time.Duration) *Status
- func (s *Status) SetErrorsHandled()
- func (s *Status) SetHeader(header http.Header, keys ...string) *Status
- func (s *Status) SetRequestId(requestId any) *Status
- func (s *Status) String() string
Examples ¶
Constants ¶
const ( StatusName = "status" StatusCodeName = "code" TraceName = "trace" RequestIdName = "request-id" ErrorsName = "errors" )
const ( OriginRegionKey = "ORIGIN-REGION" OriginZoneKey = "ORIGIN-ZONE" OriginSubZoneKey = "ORIGIN-SUB-ZONE" OriginServiceKey = "ORIGIN-SERVICE" OriginInstanceIdKey = "ORIGIN-INSTANCE-ID" )
const ( StatusInvalidContent = int(90) // Content is not available, is nil, or is of the wrong type, usually found via unmarshalling StatusIOError = int(91) // I/O operation failed StatusJsonDecodeError = int(92) // Json decoding failed StatusJsonEncodeError = int(93) // Json decoding failed StatusNotProvided = int(94) // No status is available StatusRateLimited = int(95) // Rate limited StatusNotStarted = int(96) // Not started StatusHaveContent = int(97) // Content is available StatusInvalidArgument = 3 //codes.InvalidArgument // The client specified an invalid argument. Note that this differs from FAILED_PRECONDITION. INVALID_ARGUMENT indicates arguments that are problematic regardless of the state of the system (e.g., a malformed file name). StatusDeadlineExceeded = 4 //codes.DeadlineExceeded // The deadline expired before the operation could complete. For operations that change the state of the system, this error may be returned even if the operation has completed successfully. For example, a successful response from a server could have been delayed long )
const (
EnvPrefix = "$"
)
const (
NilDuration = time.Duration(-1)
)
const (
XRequestId = "x-request-id"
)
Variables ¶
var (
PkgUri = reflect.TypeOf(any(pkg{})).PkgPath()
)
Functions ¶
func ContextWithValue ¶
ContextWithValue - create a new context with a value, updating the context if it is a Proxy context
func DefaultErrorFormatter ¶
func DoHandlerProxy ¶
func FormatErrors ¶
func FormatTrace ¶
func GetOrCreateRequestId ¶
func IsDebugEnvironment ¶
func IsDebugEnvironment() bool
func IsNil ¶
IsNil - determine if the interface{} is nil, or if it holds a nil pointer
Example ¶
var i any var p *int fmt.Printf("test: IsNil(nil) -> %v\n", IsNil(nil)) fmt.Printf("test: IsNil(i) -> %v\n", IsNil(i)) fmt.Printf("test: IsNil(pi) -> %v\n", IsNil(p))
Output: test: IsNil(nil) -> true test: IsNil(i) -> true test: IsNil(pi) -> true
func IsProdEnvironment ¶
func IsProdEnvironment() bool
func IsProxyable ¶
IsProxyable - determine if the context is a ProxyContext, and return proxies
func IsStageEnvironment ¶
func IsStageEnvironment() bool
func IsTestEnvironment ¶
func IsTestEnvironment() bool
func LookupEnv ¶
Example ¶
name := "" s, err := LookupEnv(name) fmt.Printf("test: LookupEnv(%v) -> [err:%v][%v]\n", name, err, s) s, err = LookupEnv("$") fmt.Printf("test: LookupEnv(%v) -> [err:%v][%v]\n", name, err, s) os.Setenv("RUNTIME", "DEV") s, err = LookupEnv("$RUNTIME") fmt.Printf("test: LookupEnv(%v) -> [err:%v][%v]\n", name, err, s)
Output: test: LookupEnv() -> [err:invalid argument : LookupEnv() template variable is invalid: ][] test: LookupEnv() -> [err:<nil>][] test: LookupEnv() -> [err:<nil>][DEV]
func NewInvalidBodyTypeError ¶
func NewProxyContext ¶
NewProxyContext - create a new Context interface, containing a proxy
func NewRequestContext ¶
NewRequestContext - creates a new Context with a request id from the request headers
func NewRequestIdContext ¶
NewRequestIdContext - creates a new Context with a request id
func OriginInstanceId ¶
func OriginInstanceId() string
func OriginRegion ¶
func OriginRegion() string
func OriginService ¶
func OriginService() string
func OriginString ¶
func OriginString() string
func OriginSubZone ¶
func OriginSubZone() string
func OriginZone ¶
func OriginZone() string
func PathFromUri ¶
PathFromUri - return a path from a scheme less uri
func RequestIdFromContext ¶
RequestIdFromContext - return the requestId from a context
func SetFormatOutput ¶
func SetFormatOutput(fn FormatOutput)
func SetProdEnvironment ¶
func SetProdEnvironment()
func SetStageEnvironment ¶
func SetStageEnvironment()
func SetTestEnvironment ¶
func SetTestEnvironment()
Types ¶
type ErrorHandler ¶
ErrorHandler - template parameter error handler interface Handle(requestId string, location string, errs ...error) *Status
type FormatOutput ¶
type HttpHandler ¶
HttpHandler - function type for HTTP handling
type PostHandler ¶
PostHandler - function type for a Post handler
type Status ¶
type Status struct {
// contains filtered or unexported fields
}
Status - struct for status data
func NewStatusError ¶
NewStatusError - new Status from a code, location, and optional errors
func (*Status) AddLocation ¶
func (*Status) ContentString ¶
func (*Status) Description ¶
func (*Status) ErrorsHandled ¶
ErrorsHandled - determine errors status
func (*Status) FirstError ¶
func (*Status) RemoveContent ¶
func (s *Status) RemoveContent()
func (*Status) SetContentLocation ¶
func (*Status) SetContentType ¶
func (s *Status) SetJsonContent(content any) *Status { if content == nil { return s } s.content = content s.SetContentType(ContentTypeJson) return s }
func (*Status) SetContentTypeAndLocation ¶
func (*Status) SetErrorsHandled ¶
func (s *Status) SetErrorsHandled()