feat(smalltrip,problems): Problem interface

This commit is contained in:
Guz
2025-07-29 19:18:27 -03:00
parent 61d9bee554
commit 22f2ea5fa8
3 changed files with 137 additions and 102 deletions

View File

@@ -9,14 +9,14 @@ import (
func NewInternalError(err error, opts ...Option) InternalServerError {
return InternalServerError{
Problem: NewDetailed(http.StatusInternalServerError, err.Error(), opts...),
Errors: newErrorTree(err).Errors,
error: err,
RegisteredProblem: NewDetailed(http.StatusInternalServerError, err.Error(), opts...),
Errors: newErrorTree(err).Errors,
error: err,
}
}
type InternalServerError struct {
Problem
RegisteredProblem
Errors []ErrorTree `json:"errors" xml:"errors"`
error error `json:"-" xml:"-"`
@@ -25,7 +25,7 @@ type InternalServerError struct {
var _ error = InternalServerError{}
func (p InternalServerError) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p.handler(p).ServeHTTP(w, r)
p.Handler(p).ServeHTTP(w, r)
}
func (p InternalServerError) Error() string {
@@ -60,32 +60,32 @@ func (i ErrorTree) Error() string {
func NewNotImplemented[T time.Time | time.Duration](retryAfter T, opts ...Option) NotImplemented[T] {
p := NewStatus(http.StatusNotImplemented, opts...)
return NotImplemented[T]{Problem: p, RetryAfter: RetryAfter[T]{time: retryAfter}}
return NotImplemented[T]{RegisteredProblem: p, RetryAfter: RetryAfter[T]{time: retryAfter}}
}
type NotImplemented[T time.Time | time.Duration] struct {
Problem
RegisteredProblem
RetryAfter RetryAfter[T] `json:"retryAfter" xml:"retry-after"`
}
func (p NotImplemented[T]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Retry-After", p.RetryAfter.String())
p.handler(p).ServeHTTP(w, r)
p.Handler(p).ServeHTTP(w, r)
}
func NewBadGateway(opts ...Option) BadGateway {
return BadGateway{NewStatus(http.StatusBadGateway, opts...)}
}
type BadGateway struct{ Problem }
type BadGateway struct{ RegisteredProblem }
func NewServiceUnavailable[T time.Time | time.Duration](retryAfter T, opts ...Option) ServiceUnavailable[T] {
p := NewStatus(http.StatusNotImplemented, opts...)
return ServiceUnavailable[T]{Problem: p, RetryAfter: RetryAfter[T]{time: retryAfter}}
return ServiceUnavailable[T]{RegisteredProblem: p, RetryAfter: RetryAfter[T]{time: retryAfter}}
}
type ServiceUnavailable[T time.Time | time.Duration] struct {
Problem
RegisteredProblem
RetryAfter RetryAfter[T] `json:"retryAfter" xml:"retry-after"`
}
@@ -98,40 +98,40 @@ func NewGatewayTimeout(opts ...Option) GatewayTimeout {
return GatewayTimeout{NewStatus(http.StatusGatewayTimeout, opts...)}
}
type GatewayTimeout struct{ Problem }
type GatewayTimeout struct{ RegisteredProblem }
func NewHTTPVersionNotSupported(opts ...Option) HTTPVersionNotSupported {
return HTTPVersionNotSupported{NewStatus(http.StatusHTTPVersionNotSupported, opts...)}
}
type HTTPVersionNotSupported struct{ Problem }
type HTTPVersionNotSupported struct{ RegisteredProblem }
func NewVariantAlsoNegotiates(opts ...Option) VariantAlsoNegotiates {
return VariantAlsoNegotiates{NewStatus(http.StatusVariantAlsoNegotiates, opts...)}
}
type VariantAlsoNegotiates struct{ Problem }
type VariantAlsoNegotiates struct{ RegisteredProblem }
func NewInsufficientStorage(opts ...Option) InsufficientStorage {
return InsufficientStorage{NewStatus(http.StatusInsufficientStorage, opts...)}
}
type InsufficientStorage struct{ Problem }
type InsufficientStorage struct{ RegisteredProblem }
func NewLoopDetected(opts ...Option) LoopDetected {
return LoopDetected{NewStatus(http.StatusLoopDetected, opts...)}
}
type LoopDetected struct{ Problem }
type LoopDetected struct{ RegisteredProblem }
func NewNotExtended(opts ...Option) NotExtended {
return NotExtended{NewStatus(http.StatusNotExtended, opts...)}
}
type NotExtended struct{ Problem }
type NotExtended struct{ RegisteredProblem }
func NewNetworkAuthenticationRequired(opts ...Option) NetworkAuthenticationRequired {
return NetworkAuthenticationRequired{NewStatus(http.StatusNetworkAuthenticationRequired, opts...)}
}
type NetworkAuthenticationRequired struct{ Problem }
type NetworkAuthenticationRequired struct{ RegisteredProblem }