util

package
v0.0.0-...-c713ad7 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// APINameContextKey is the context key for the API name
	APINameContextKey = "apiName"

	// APIErrorInternalServer indicates an unexpected error is reported from the API
	APIErrorInternalServer = "An unexpected error occurred while processing the request"

	// APIErrorNotFound indicates that the requested path was not found
	APIErrorNotFound = "The requested path was not found"

	// MaxRequestBodyBytes is the largest request body the API accepts. The audit
	// capture uses the same limit so every accepted body is audited in full.
	MaxRequestBodyBytes = 10_000_000
)
View Source
const (
	// SecretLogPrefixLen is how much of an opaque secret a diagnostic log may
	// carry. The shortest one this covers is a Site registration OTP, 20 random
	// bytes in base64, so four characters tell two of them apart and leave the
	// remaining 136 bits out of the logs.
	SecretLogPrefixLen = 4

	// CertLogPrefixLen is how much of a PEM a diagnostic log may carry. Its
	// `-----BEGIN CERTIFICATE-----` line takes the first 28 characters, so a
	// prefix has to clear that before it reaches anything that tells two
	// certificates apart. This leaves 36 characters of the encoded body, and a
	// certificate is public in any case.
	CertLogPrefixLen = 64
)
View Source
const (
	// DefaultInventoryReceiptInterval is the assumed interval between 2 subsequent inventory
	// receipts for a Site that has not reported its own collection interval. Prefer
	// Site.IsTimeWithinStaleInventoryThreshold, which follows the reported interval where there
	// is one.
	DefaultInventoryReceiptInterval = 3 * time.Minute
	// MaxInventoryReceiptInterval is the slowest inventory collection the system supports. REST
	// layer waits out the reported interval before acting on an object, so a slower Site would
	// hold off deletions and status updates long enough to destabilize it. The Site Agent rejects
	// a schedule past this at config load rather than run in that state.
	MaxInventoryReceiptInterval = 5 * time.Minute

	// ActivityStartToCloseTimeout bounds the on-site request before the workflow and
	// the REST caller time out.
	ActivityStartToCloseTimeout = 40 * time.Second
	// WorkflowExecutionTimeout leaves the REST caller time to observe and translate a
	// terminal workflow result. Temporal enforces it and closes the execution, unlike
	// WorkflowContextTimeout, which only stops the caller waiting.
	WorkflowExecutionTimeout = 45 * time.Second
	// WorkflowContextTimeout is how long a REST handler waits for the workflow it
	// started before abandoning it.
	WorkflowContextTimeout = time.Second * 50
	// WorkflowContextNewAfterTimeout is the timeout for a new workflow context
	WorkflowContextNewAfterTimeout = time.Second * 5
)

Variables

View Source
var (
	// ErrBadRequest (400) is returned for bad request (validation)
	ErrBadRequest = echo.ErrBadRequest

	// ErrUnauthorized (401) is returned when user is not authorized
	ErrUnauthorized = echo.ErrUnauthorized

	// ErrInternal (500) is returned when an internal server error occurs
	ErrInternal = echo.ErrInternalServerError
)

Functions

func CreateHash

func CreateHash(key string) []byte

CreateHash takes a string and returns SHA 256 digest in a byte array

func DecryptData

func DecryptData(data []byte, passphrase string) []byte

DecryptData provides mechanism to decrypt arguments being passed into workflows

func DefaultHTTPErrorHandler

func DefaultHTTPErrorHandler(err error, c echo.Context)

DefaultHTTPErrorHandler is the default HTTP error handler. It sends a structured error response

NOTE: In case errors happens in middleware call-chain that is returning from handler (handler ran into un-recovered error): When handler has already sent response (ala c.JSON()) and there is error in middleware that is returning from handler, then the error that global error handler received will be ignored because we have already "committed" the response and status code header has been sent to the client.

func EncryptData

func EncryptData(data []byte, passphrase string) []byte

EncryptData provides mechanism to encrypt arguments being passed into workflows so it is not visible within Temporal system

func GetPtr

func GetPtr[T any](v T) *T

GetPtr returns a pointer to a copy of v. Generic helper that replaces the per-type `GetStrPtr` / `GetIntPtr` / `GetBoolPtr` / `GetUUIDPtr` / `GetTimePtr` helpers that previously lived in `db/pkg/db`.

func GetPtrIfNotZero

func GetPtrIfNotZero[T comparable](v T) *T

GetPtrIfNotZero returns nil for a type's zero value or a pointer to a copy of v otherwise.

func GetValueOrZero

func GetValueOrZero[T any](v *T) T

GetValueOrZero returns a type's zero value for nil or the dereferenced value otherwise.

func IntPtrToUint32Ptr

func IntPtrToUint32Ptr(i *int) *uint32

IntPtrToUint32Ptr converts a `*int` to a `*uint32`. nil in, nil out. Callers must ensure the int sits in `[0, MaxUint32]`; the cast otherwise silently wraps. Under the proto-conversion convention, that bound is the responsibility of the request-side `Validate` (which rejects negatives and overflow with a 400) on the API-inbound path, or guaranteed by construction on the proto-inbound path (where values originate from a proto `uint32` field).

func NewAPIErrorResponse

func NewAPIErrorResponse(c echo.Context, code int, message string, data error) error

NewAPIErrorResponse sends an unclassified API error response.

func ProtoTimePtrToStrPtr

func ProtoTimePtrToStrPtr(t *timestamppb.Timestamp) *string

ProtoTimePtrToStrPtr converts a protobuf timestamp pointer to a string pointer

func RedactSecret

func RedactSecret(secret string, prefixLen int) string

RedactSecret renders a secret for a diagnostic log: up to prefixLen leading characters, a marker for the withheld remainder, and the length. That is enough to tell which value a failure was about, and never enough to reuse it. Pass SecretLogPrefixLen for an opaque secret or CertLogPrefixLen for a PEM. A secret no longer than prefixLen keeps none of its characters, so asking for more than the value holds yields nothing rather than all of it.

func StrPtrToProtoTimePtr

func StrPtrToProtoTimePtr(s *string) *timestamppb.Timestamp

StrPtrToProtoTimePtr converts a string pointer to a protobuf timestamp pointer

func Uint32PtrToIntPtr

func Uint32PtrToIntPtr(u *uint32) *int

Uint32PtrToIntPtr converts a `*uint32` to a `*int`. nil in, nil out. The cast is always safe on 64-bit platforms (the only ones we target); on a hypothetical 32-bit build it would wrap on values above `MaxInt32`. Under the proto-conversion convention this is a trusted cast used inside `FromProto` mappers — the upstream value originates from a proto `uint32` field, and any bounds checks that would gate it belong in `Validate` upstream.

Types

type APIError

type APIError struct {
	Code    int    `json:"-"`
	Source  string `json:"source"`
	Message string `json:"message"`
	Data    error  `json:"data"`
	// Omit unclassified recovery metadata to preserve existing error bodies.
	Retryable *bool `json:"retryable,omitempty"`
	// contains filtered or unexported fields
}

APIError represents a structured API error

func NewAPIError

func NewAPIError(code int, message string, data error) *APIError

NewAPIError returns an API error given appropriate params

func (*APIError) Diagnosis

func (a *APIError) Diagnosis() error

Diagnosis returns the error worth logging: the internal cause, or a itself when the cause was folded into Message and Unwrap is therefore nil.

func (*APIError) Error

func (a *APIError) Error() string

Error implements the error interface so *APIError can flow through error channels (e.g., closures returned by WithTx). Use NewAPIErrorResponse to turn one into an Echo response at the API boundary.

func (*APIError) Send

func (a *APIError) Send(c echo.Context) error

Send preserves the error metadata and sets the response's API source.

func (*APIError) Unwrap

func (a *APIError) Unwrap() error

Unwrap lets errors.Is/As inspect the private cause without exposing it in response data. Errors without a private cause still unwrap Data.

func (*APIError) WithCause

func (a *APIError) WithCause(cause error) *APIError

WithCause preserves the internal error chain without changing response data.

func (*APIError) WithRetryable

func (a *APIError) WithRetryable(retryable bool) *APIError

WithRetryable classifies a definite rejection. True permits a bounded retry; it does not promise availability or eventual success.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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