Documentation
¶
Overview ¶
Package httpx holds the HTTP plumbing shared by every handler: status-carrying errors, JSON encoding, and the error response shape.
Index ¶
- func Bind(r *http.Request, dst any) error
- func DecodeJSON(r *http.Request, dst any) error
- func StatusOf(err error) int
- func WriteError(w http.ResponseWriter, r *http.Request, err error)
- func WriteJSON(w http.ResponseWriter, status int, v any)
- type Constraints
- func (c *Constraints) Add(field, message string)
- func (c *Constraints) Email(field, message string, value *string)
- func (c *Constraints) Fields() []FieldError
- func (c *Constraints) Nested(prefix string, v Validator)
- func (c *Constraints) NotBlank(field, message string, value string)
- func (c *Constraints) NotBlankPtr(field, message string, value *string)
- func (c *Constraints) NotNil(field, message string, value any)
- func (c *Constraints) Positive(field, message string, value *int64)
- type Error
- type FieldError
- type Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodeJSON ¶
DecodeJSON reads the request body into dst.
Unknown fields are ignored, matching Spring Boot's Jackson defaults (FAIL_ON_UNKNOWN_PROPERTIES is off), so a client sending an extra key gets the same acceptance it got from the Java service.
func StatusOf ¶
StatusOf returns the HTTP status err should be reported with, defaulting to 500 for anything that is not an *Error.
func WriteError ¶
func WriteError(w http.ResponseWriter, r *http.Request, err error)
WriteError renders err as a JSON failure. Anything that is not an *Error becomes a bare 500: the message of an unexpected error may name internal state, so it is logged rather than returned to the caller.
Types ¶
type Constraints ¶
type Constraints struct {
// contains filtered or unexported fields
}
Constraints accumulates field violations, standing in for the jakarta.validation annotations on the Java DTOs. The messages are carried over verbatim so clients see the same text.
func (*Constraints) Add ¶
func (c *Constraints) Add(field, message string)
Add records an arbitrary violation.
func (*Constraints) Email ¶
func (c *Constraints) Email(field, message string, value *string)
Email records a violation when value is present but not plausibly an address, mirroring @Email. Like Jakarta's default, a nil or empty value passes.
func (*Constraints) Fields ¶
func (c *Constraints) Fields() []FieldError
Fields returns everything recorded so far.
func (*Constraints) Nested ¶
func (c *Constraints) Nested(prefix string, v Validator)
Nested folds the violations of a nested payload in under a prefix, mirroring @Valid on a nested DTO.
func (*Constraints) NotBlank ¶
func (c *Constraints) NotBlank(field, message string, value string)
NotBlank records a violation when value is empty or only whitespace, mirroring @NotBlank.
func (*Constraints) NotBlankPtr ¶
func (c *Constraints) NotBlankPtr(field, message string, value *string)
NotBlankPtr is NotBlank for an optional field: a nil pointer is as much a violation as a blank string.
func (*Constraints) NotNil ¶
func (c *Constraints) NotNil(field, message string, value any)
NotNil records a violation when value is nil, mirroring @NotNull.
func (*Constraints) Positive ¶
func (c *Constraints) Positive(field, message string, value *int64)
Positive records a violation when value is nil or not greater than zero, mirroring @NotNull @Positive on wallTimeMinutes.
type Error ¶
type Error struct {
Status int
Message string
// Fields carries per-field validation failures. Empty for everything else.
Fields []FieldError
// Err is an optional wrapped cause, kept out of the response body.
Err error
}
Error is a failure that already knows its HTTP status. It is the counterpart to Spring's ResponseStatusException: services raise it, and the handler layer turns it into a response without needing to know what went wrong.
func BadRequest ¶
BadRequest reports a malformed or semantically invalid request.
func Conflict ¶
Conflict reports a request that collides with existing state — the two cases here are deleting an SSH credential still in use and deleting a template that still has deployments.
func Forbidden ¶
Forbidden reports an authenticated caller lacking the required authority. It corresponds to Spring's AccessDeniedException for a non-anonymous principal.
func Invalid ¶
func Invalid(fields []FieldError) *Error
Invalid reports failed request-body validation.
func NotFound ¶
NotFound reports a missing resource. Message is phrased as the Java services phrase it, e.g. "Cluster not found: abc123".
func Unauthorized ¶
Unauthorized reports a missing or unusable token. Spring returns this rather than 403 when the principal is anonymous, because the caller can fix it by presenting credentials.
type FieldError ¶
FieldError is one failed validation constraint.
type Validator ¶
type Validator interface {
Validate() []FieldError
}
Validator is implemented by request payloads that carry constraints.