runtime

package
v0.0.0-...-b511e1a Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: BSD-3-Clause Imports: 12 Imported by: 7

Documentation

Index

Examples

Constants

View Source
const (
	TimestampName = "timestamp"
	StatusName    = "status"
	CodeName      = "code"
	TraceName     = "trace"
	RequestIdName = "request-id"
	ErrorsName    = "errors"
)
View Source
const (
	XRequestId = "x-request-id"
	XRelatesTo = "x-relates-to"
)
View Source
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 encoding failed
	StatusContentEncodingError = int(94)  // Content encoding error
	StatusNotProvided          = int(95)  // No status is available
	StatusRateLimited          = int(96)  // Rate limited
	StatusNotStarted           = int(97)  // Not started
	StatusHaveContent          = int(98)  // Content is available
	StatusGzipEncodingError    = int(99)  // Gzip encoding error
	StatusGzipDecodingError    = int(100) // Gzip decoding error

	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

)
View Source
const (
	EnvKey = "RUNTIME_ENV"
)
View Source
const (
	PkgPath = "github/advanced-go/core/runtime"
)

Variables

This section is empty.

Functions

func AddRequestId

func AddRequestId(t any) http.Header

AddRequestId - add a request to an http.Request or an http.Header

Example
func ExampleRequestId_New() {
	id := RequestId(nil)
	fmt.Printf("test: RequestId(nil) -> [empty:%v]\n", len(id) == 0)

	id = RequestId(context.Background())
	fmt.Printf("test: RequestId(ctx) -> [empty:%v]\n", len(id) == 0)

	req, _ := http.NewRequest("", "https.www.google.com", nil)
	id = RequestId(req)
	fmt.Printf("test: RequestId(request) -> [empty:%v]\n", len(id) == 0)

	h := make(http.Header)
	id = RequestId(h)
	fmt.Printf("test: RequestId(header) -> [empty:%v]\n", len(id) == 0)

	//Output:
	//test: RequestId(nil) -> [empty:false]
	//test: RequestId(ctx) -> [empty:false]
	//test: RequestId(request) -> [empty:false]
	//test: RequestId(header) -> [empty:false]

}

h := AddRequestId(nil)
fmt.Printf("test: AddRequestId(nil) -> [empty:%v]\n", len(h.Get(XRequestId)) == 0)

head := make(http.Header)
h = AddRequestId(head)
fmt.Printf("test: AddRequestId(head) -> [empty:%v]\n", len(h.Get(XRequestId)) == 0)

head = make(http.Header)
head.Add(XRequestId, "123-45-head")
h = AddRequestId(head)
fmt.Printf("test: AddRequestId(head) -> %v\n", h.Get(XRequestId))

req, _ := http.NewRequest("", "https.www.google.com", nil)
h = AddRequestId(req)
fmt.Printf("test: RequestId(request) -> [empty:%v]\n", len(h.Get(XRequestId)) == 0)

req, _ = http.NewRequest("", "https.www.google.com", nil)
req.Header.Set(XRequestId, "123-456-request")
h = AddRequestId(req)
fmt.Printf("test: RequestId(request) -> %v\n", h.Get(XRequestId))
Output:

test: AddRequestId(nil) -> [empty:false]
test: AddRequestId(head) -> [empty:false]
test: AddRequestId(head) -> 123-45-head
test: RequestId(request) -> [empty:false]
test: RequestId(request) -> 123-456-request

func EnvStr

func EnvStr() string

EnvStr - string representation for the environment

func FmtRFC3339Millis

func FmtRFC3339Millis(t time.Time) string

FmtRFC3339Millis - format time https://datatracker.ietf.org/doc/html/rfc3339

func HttpCode

func HttpCode(code int) int

HttpCode - conversion of a code to HTTP status code

func HttpStatus

func HttpStatus(code int) string

HttpStatus - string representation of status code

func IsDebugEnvironment

func IsDebugEnvironment() bool

IsDebugEnvironment - determine if debug environment

func IsProdEnvironment

func IsProdEnvironment() bool

IsProdEnvironment - determine if production environment

func IsStageEnvironment

func IsStageEnvironment() bool

IsStageEnvironment - determine if staging environment

func IsTestEnvironment

func IsTestEnvironment() bool

IsTestEnvironment - determine if test environment

func NewInvalidBodyTypeError

func NewInvalidBodyTypeError(t any) error

NewInvalidBodyTypeError - invalid type error

func NewRequestIdContext

func NewRequestIdContext(ctx context.Context, requestId string) context.Context

NewRequestIdContext - creates a new Context with a request id

func NewRequestIdContextFromHeader

func NewRequestIdContextFromHeader(h http.Header) context.Context

NewRequestIdContextFromHeader - creates a new Context with a request id from the request headers

func OutputFormatter

func OutputFormatter(ts time.Time, code int, status, requestId string, errs []error, trace []string) string

OutputFormatter - formatter for special output formatting

func ParseRFC3339Millis

func ParseRFC3339Millis(ts string) (time.Time, error)

ParseRFC3339Millis - parse a string into a time.Time, using the following string : 2023-04-14T14:14:45.522Z

func ParseTimestamp2

func ParseTimestamp2(s string) (time.Time, error)

ParseTimestamp2 - parse a string into a time.Time, using the following string : 2023-04-14 14:14:45.522460

func RequestId

func RequestId(t any) string

RequestId - return a request id from any type and will create a new one if not found

Example
id := RequestId("123-456-string")
fmt.Printf("test: RequestId(string) -> %v\n", id)

ctx := NewRequestIdContext(context.Background(), "123-456-context")
id = RequestId(ctx)
fmt.Printf("test: RequestId(ctx) -> %v\n", id)

req, _ := http.NewRequest("", "https.www.google.com", nil)
req.Header.Set(XRequestId, "123-456-request")
id = RequestId(req)
fmt.Printf("test: RequestId(request) -> %v\n", id)

h := make(http.Header)
h.Set(XRequestId, "123-456-header")
id = RequestId(h)
fmt.Printf("test: RequestId(header) -> %v\n", id)
Output:

test: RequestId(string) -> 123-456-string
test: RequestId(ctx) -> 123-456-context
test: RequestId(request) -> 123-456-request
test: RequestId(header) -> 123-456-header

func RequestIdFromContext

func RequestIdFromContext(ctx any) string

RequestIdFromContext - return the requestId from a context

func SetErrorFormatter

func SetErrorFormatter(fn Formatter)

SetErrorFormatter - optional override of error formatting

func SetErrorLogger

func SetErrorLogger(fn Logger)

SetErrorLogger - optional override of logging

func SetOutputFormatter

func SetOutputFormatter()

SetOutputFormatter - optional override of output formatting

func SetProdEnvironment

func SetProdEnvironment()

SetProdEnvironment - set production environment

func SetStageEnvironment

func SetStageEnvironment()

SetStageEnvironment - set staging environment

func SetTestEnvironment

func SetTestEnvironment()

SetTestEnvironment - set test environment

Types

type Attr

type Attr struct {
	Key   string
	Value any
}

func Attributes

func Attributes(e error) []Attr

func (Attr) String

func (a Attr) String() string

type Attributable

type Attributable interface {
	Attributes() []Attr
}

type Bypass

type Bypass struct{}

Bypass - bypass error handler

func (Bypass) Handle

func (h Bypass) Handle(s *Status, _ string) *Status

Handle - bypass error handler

type ErrorHandler

type ErrorHandler interface {
	Handle(s *Status, requestId string) *Status
}

ErrorHandler - error handler interface

type Formatter

type Formatter func(ts time.Time, code int, status, requestId string, errs []error, trace []string) string

Formatter - output formatting type

type Log

type Log struct{}

Log - log error handler

func (Log) Handle

func (h Log) Handle(s *Status, requestId string) *Status

Handle - log error handler

type Logger

type Logger func(code int, status, requestId string, errs []error, trace []string)

Logger - log function

type Nillable

type Nillable *struct{}

Nillable - nillable type for generic constraints

type Output

type Output struct{}

Output - standard output error handler

func (Output) Handle

func (h Output) Handle(s *Status, requestId string) *Status

Handle - output error handler

type Status

type Status struct {
	Code int `json:"code"`
	// contains filtered or unexported fields
}

func NewStatus

func NewStatus(code int) *Status

func NewStatusError

func NewStatusError(code int, err error) *Status

func StatusOK

func StatusOK() *Status

func (*Status) AddError

func (s *Status) AddError(err error) *Status

func (*Status) AddLocation

func (s *Status) AddLocation() *Status

func (*Status) Error

func (s *Status) Error() error

func (*Status) HttpCode

func (s *Status) HttpCode() int

func (*Status) OK

func (s *Status) OK() bool

func (*Status) StatusCode

func (s *Status) StatusCode() int

func (*Status) String

func (s *Status) String() string

func (*Status) Trace

func (s *Status) Trace() []string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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