feat(api): define public problem errors, pagination, and retry contract
This commit is contained in:
1 parent
b2f6e87bfe
commit
565ca94bbb
86 files changed
+8200
-154
No files matched your search
@@ -3,9 +3,15 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/go-faster/errors"
|
||||
)
|
||||
|
||||
func (s *ProblemStatusCode) Error() string {
|
||||
return fmt.Sprintf("code %d: %+v", s.StatusCode, s.Response)
|
||||
}
|
||||
|
||||
// Ref: #/components/schemas/HealthResponse
|
||||
type HealthResponse struct {
|
||||
Status HealthResponseStatus `json:"status"`
|
||||
@@ -54,3 +60,454 @@ func (s *HealthResponseStatus) UnmarshalText(data []byte) error {
|
||||
return errors.Errorf("invalid value: %q", data)
|
||||
}
|
||||
}
|
||||
|
||||
// NewOptString returns new OptString with value set to v.
|
||||
func NewOptString(v string) OptString {
|
||||
return OptString{
|
||||
Value: v,
|
||||
Set: true,
|
||||
}
|
||||
}
|
||||
|
||||
// OptString is optional string.
|
||||
type OptString struct {
|
||||
Value string
|
||||
Set bool
|
||||
}
|
||||
|
||||
// IsSet returns true if OptString was set.
|
||||
func (o OptString) IsSet() bool { return o.Set }
|
||||
|
||||
// Reset unsets value.
|
||||
func (o *OptString) Reset() {
|
||||
var v string
|
||||
o.Value = v
|
||||
o.Set = false
|
||||
}
|
||||
|
||||
// SetTo sets value to v.
|
||||
func (o *OptString) SetTo(v string) {
|
||||
o.Set = true
|
||||
o.Value = v
|
||||
}
|
||||
|
||||
// Get returns value and boolean that denotes whether value was set.
|
||||
func (o OptString) Get() (v string, ok bool) {
|
||||
if !o.Set {
|
||||
return v, false
|
||||
}
|
||||
return o.Value, true
|
||||
}
|
||||
|
||||
// Or returns value if set, or given parameter if does not.
|
||||
func (o OptString) Or(d string) string {
|
||||
if v, ok := o.Get(); ok {
|
||||
return v
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// Problem details (RFC 9457) returned with the application/problem+json media type for every error
|
||||
// response. `code` is the stable, machine-readable reason; clients branch on it rather than on
|
||||
// `title`, `detail`, or the HTTP status alone. The body never contains internal error text, stack
|
||||
// traces, SQL, file paths, credentials, contact data, or echoed request values.
|
||||
// Ref: #/components/schemas/Problem
|
||||
type Problem struct {
|
||||
// URI that identifies the problem type. It has the form `urn:lincodex:problem:<code>` and is an
|
||||
// identifier, not a link to dereference.
|
||||
Type string `json:"type"`
|
||||
// Short, fixed English summary of the problem type. It does not change between occurrences.
|
||||
Title string `json:"title"`
|
||||
// HTTP status code generated by the origin server.
|
||||
Status int32 `json:"status"`
|
||||
Code ProblemCode `json:"code"`
|
||||
// Safe English explanation of this occurrence. It never contains internal error text or request
|
||||
// values.
|
||||
Detail OptString `json:"detail"`
|
||||
// URI reference that identifies this occurrence, when one exists.
|
||||
Instance OptString `json:"instance"`
|
||||
// Request identifier, equal to the X-Request-ID response header. Quote it when reporting the problem;
|
||||
// it is not a secret.
|
||||
TraceID OptString `json:"trace_id"`
|
||||
// Field-level reasons for validation_failed. Other codes omit it.
|
||||
Errors []ProblemFieldError `json:"errors"`
|
||||
}
|
||||
|
||||
// GetType returns the value of Type.
|
||||
func (s *Problem) GetType() string {
|
||||
return s.Type
|
||||
}
|
||||
|
||||
// GetTitle returns the value of Title.
|
||||
func (s *Problem) GetTitle() string {
|
||||
return s.Title
|
||||
}
|
||||
|
||||
// GetStatus returns the value of Status.
|
||||
func (s *Problem) GetStatus() int32 {
|
||||
return s.Status
|
||||
}
|
||||
|
||||
// GetCode returns the value of Code.
|
||||
func (s *Problem) GetCode() ProblemCode {
|
||||
return s.Code
|
||||
}
|
||||
|
||||
// GetDetail returns the value of Detail.
|
||||
func (s *Problem) GetDetail() OptString {
|
||||
return s.Detail
|
||||
}
|
||||
|
||||
// GetInstance returns the value of Instance.
|
||||
func (s *Problem) GetInstance() OptString {
|
||||
return s.Instance
|
||||
}
|
||||
|
||||
// GetTraceID returns the value of TraceID.
|
||||
func (s *Problem) GetTraceID() OptString {
|
||||
return s.TraceID
|
||||
}
|
||||
|
||||
// GetErrors returns the value of Errors.
|
||||
func (s *Problem) GetErrors() []ProblemFieldError {
|
||||
return s.Errors
|
||||
}
|
||||
|
||||
// SetType sets the value of Type.
|
||||
func (s *Problem) SetType(val string) {
|
||||
s.Type = val
|
||||
}
|
||||
|
||||
// SetTitle sets the value of Title.
|
||||
func (s *Problem) SetTitle(val string) {
|
||||
s.Title = val
|
||||
}
|
||||
|
||||
// SetStatus sets the value of Status.
|
||||
func (s *Problem) SetStatus(val int32) {
|
||||
s.Status = val
|
||||
}
|
||||
|
||||
// SetCode sets the value of Code.
|
||||
func (s *Problem) SetCode(val ProblemCode) {
|
||||
s.Code = val
|
||||
}
|
||||
|
||||
// SetDetail sets the value of Detail.
|
||||
func (s *Problem) SetDetail(val OptString) {
|
||||
s.Detail = val
|
||||
}
|
||||
|
||||
// SetInstance sets the value of Instance.
|
||||
func (s *Problem) SetInstance(val OptString) {
|
||||
s.Instance = val
|
||||
}
|
||||
|
||||
// SetTraceID sets the value of TraceID.
|
||||
func (s *Problem) SetTraceID(val OptString) {
|
||||
s.TraceID = val
|
||||
}
|
||||
|
||||
// SetErrors sets the value of Errors.
|
||||
func (s *Problem) SetErrors(val []ProblemFieldError) {
|
||||
s.Errors = val
|
||||
}
|
||||
|
||||
// Stable public error code. Adding a code is a contract change; renaming or removing one is a breaking
|
||||
// change. Clients must handle an unknown code by its HTTP status.
|
||||
//
|
||||
// - validation_failed (400): parameters or body do not match the contract, or a body violates a
|
||||
// domain rule such as salary_from <= salary_to; `errors` points at the fields.
|
||||
// - unauthenticated (401): no valid session or credentials.
|
||||
// - forbidden (403): the caller is authenticated but not allowed.
|
||||
// - not_found (404): the route or object does not exist or is not visible.
|
||||
// - method_not_allowed (405): the route does not support the method.
|
||||
// - idempotency_in_progress (409): a request with the same Idempotency-Key is still being processed.
|
||||
// - state_conflict (409): the operation conflicts with the current state of the resource, for example
|
||||
// an invitation that was already accepted.
|
||||
// - version_conflict (412): If-Match does not match the current version.
|
||||
// - payload_too_large (413): the request body exceeds the limit.
|
||||
// - unsupported_media_type (415): the request Content-Type is not accepted.
|
||||
// - idempotency_key_reused (422): the Idempotency-Key was already used with a different request.
|
||||
// - precondition_required (428): the operation requires If-Match.
|
||||
// - rate_limited (429): too many requests; see Retry-After.
|
||||
// - internal (500): unexpected server error.
|
||||
// - not_implemented (501): the operation is in the contract but not implemented yet.
|
||||
//
|
||||
// Ref: #/components/schemas/ProblemCode
|
||||
type ProblemCode string
|
||||
|
||||
const (
|
||||
ProblemCodeValidationFailed ProblemCode = "validation_failed"
|
||||
ProblemCodeUnauthenticated ProblemCode = "unauthenticated"
|
||||
ProblemCodeForbidden ProblemCode = "forbidden"
|
||||
ProblemCodeNotFound ProblemCode = "not_found"
|
||||
ProblemCodeMethodNotAllowed ProblemCode = "method_not_allowed"
|
||||
ProblemCodeIdempotencyInProgress ProblemCode = "idempotency_in_progress"
|
||||
ProblemCodeStateConflict ProblemCode = "state_conflict"
|
||||
ProblemCodeVersionConflict ProblemCode = "version_conflict"
|
||||
ProblemCodePayloadTooLarge ProblemCode = "payload_too_large"
|
||||
ProblemCodeUnsupportedMediaType ProblemCode = "unsupported_media_type"
|
||||
ProblemCodeIdempotencyKeyReused ProblemCode = "idempotency_key_reused"
|
||||
ProblemCodePreconditionRequired ProblemCode = "precondition_required"
|
||||
ProblemCodeRateLimited ProblemCode = "rate_limited"
|
||||
ProblemCodeInternal ProblemCode = "internal"
|
||||
ProblemCodeNotImplemented ProblemCode = "not_implemented"
|
||||
)
|
||||
|
||||
// AllValues returns all ProblemCode values.
|
||||
func (ProblemCode) AllValues() []ProblemCode {
|
||||
return []ProblemCode{
|
||||
ProblemCodeValidationFailed,
|
||||
ProblemCodeUnauthenticated,
|
||||
ProblemCodeForbidden,
|
||||
ProblemCodeNotFound,
|
||||
ProblemCodeMethodNotAllowed,
|
||||
ProblemCodeIdempotencyInProgress,
|
||||
ProblemCodeStateConflict,
|
||||
ProblemCodeVersionConflict,
|
||||
ProblemCodePayloadTooLarge,
|
||||
ProblemCodeUnsupportedMediaType,
|
||||
ProblemCodeIdempotencyKeyReused,
|
||||
ProblemCodePreconditionRequired,
|
||||
ProblemCodeRateLimited,
|
||||
ProblemCodeInternal,
|
||||
ProblemCodeNotImplemented,
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalText implements encoding.TextMarshaler.
|
||||
func (s ProblemCode) MarshalText() ([]byte, error) {
|
||||
switch s {
|
||||
case ProblemCodeValidationFailed:
|
||||
return []byte(s), nil
|
||||
case ProblemCodeUnauthenticated:
|
||||
return []byte(s), nil
|
||||
case ProblemCodeForbidden:
|
||||
return []byte(s), nil
|
||||
case ProblemCodeNotFound:
|
||||
return []byte(s), nil
|
||||
case ProblemCodeMethodNotAllowed:
|
||||
return []byte(s), nil
|
||||
case ProblemCodeIdempotencyInProgress:
|
||||
return []byte(s), nil
|
||||
case ProblemCodeStateConflict:
|
||||
return []byte(s), nil
|
||||
case ProblemCodeVersionConflict:
|
||||
return []byte(s), nil
|
||||
case ProblemCodePayloadTooLarge:
|
||||
return []byte(s), nil
|
||||
case ProblemCodeUnsupportedMediaType:
|
||||
return []byte(s), nil
|
||||
case ProblemCodeIdempotencyKeyReused:
|
||||
return []byte(s), nil
|
||||
case ProblemCodePreconditionRequired:
|
||||
return []byte(s), nil
|
||||
case ProblemCodeRateLimited:
|
||||
return []byte(s), nil
|
||||
case ProblemCodeInternal:
|
||||
return []byte(s), nil
|
||||
case ProblemCodeNotImplemented:
|
||||
return []byte(s), nil
|
||||
default:
|
||||
return nil, errors.Errorf("invalid value: %q", s)
|
||||
}
|
||||
}
|
||||
|
||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||
func (s *ProblemCode) UnmarshalText(data []byte) error {
|
||||
switch ProblemCode(data) {
|
||||
case ProblemCodeValidationFailed:
|
||||
*s = ProblemCodeValidationFailed
|
||||
return nil
|
||||
case ProblemCodeUnauthenticated:
|
||||
*s = ProblemCodeUnauthenticated
|
||||
return nil
|
||||
case ProblemCodeForbidden:
|
||||
*s = ProblemCodeForbidden
|
||||
return nil
|
||||
case ProblemCodeNotFound:
|
||||
*s = ProblemCodeNotFound
|
||||
return nil
|
||||
case ProblemCodeMethodNotAllowed:
|
||||
*s = ProblemCodeMethodNotAllowed
|
||||
return nil
|
||||
case ProblemCodeIdempotencyInProgress:
|
||||
*s = ProblemCodeIdempotencyInProgress
|
||||
return nil
|
||||
case ProblemCodeStateConflict:
|
||||
*s = ProblemCodeStateConflict
|
||||
return nil
|
||||
case ProblemCodeVersionConflict:
|
||||
*s = ProblemCodeVersionConflict
|
||||
return nil
|
||||
case ProblemCodePayloadTooLarge:
|
||||
*s = ProblemCodePayloadTooLarge
|
||||
return nil
|
||||
case ProblemCodeUnsupportedMediaType:
|
||||
*s = ProblemCodeUnsupportedMediaType
|
||||
return nil
|
||||
case ProblemCodeIdempotencyKeyReused:
|
||||
*s = ProblemCodeIdempotencyKeyReused
|
||||
return nil
|
||||
case ProblemCodePreconditionRequired:
|
||||
*s = ProblemCodePreconditionRequired
|
||||
return nil
|
||||
case ProblemCodeRateLimited:
|
||||
*s = ProblemCodeRateLimited
|
||||
return nil
|
||||
case ProblemCodeInternal:
|
||||
*s = ProblemCodeInternal
|
||||
return nil
|
||||
case ProblemCodeNotImplemented:
|
||||
*s = ProblemCodeNotImplemented
|
||||
return nil
|
||||
default:
|
||||
return errors.Errorf("invalid value: %q", data)
|
||||
}
|
||||
}
|
||||
|
||||
// One reason why a request field or parameter is invalid.
|
||||
// Ref: #/components/schemas/ProblemFieldError
|
||||
type ProblemFieldError struct {
|
||||
In ProblemFieldLocation `json:"in"`
|
||||
// JSON Pointer (RFC 6901). For `body` it points into the request body document, for example
|
||||
// `/salary_from`; an empty string means the whole body. For a parameter it is `/` followed by the
|
||||
// parameter name, for example `/limit` or `/Idempotency-Key`.
|
||||
Pointer string `json:"pointer"`
|
||||
// Stable field reason. Current values are `required` (missing value) and `invalid` (malformed value or
|
||||
// a value outside the schema). Clients must tolerate new values.
|
||||
Code string `json:"code"`
|
||||
// Fixed English message that never echoes the submitted value.
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// GetIn returns the value of In.
|
||||
func (s *ProblemFieldError) GetIn() ProblemFieldLocation {
|
||||
return s.In
|
||||
}
|
||||
|
||||
// GetPointer returns the value of Pointer.
|
||||
func (s *ProblemFieldError) GetPointer() string {
|
||||
return s.Pointer
|
||||
}
|
||||
|
||||
// GetCode returns the value of Code.
|
||||
func (s *ProblemFieldError) GetCode() string {
|
||||
return s.Code
|
||||
}
|
||||
|
||||
// GetMessage returns the value of Message.
|
||||
func (s *ProblemFieldError) GetMessage() string {
|
||||
return s.Message
|
||||
}
|
||||
|
||||
// SetIn sets the value of In.
|
||||
func (s *ProblemFieldError) SetIn(val ProblemFieldLocation) {
|
||||
s.In = val
|
||||
}
|
||||
|
||||
// SetPointer sets the value of Pointer.
|
||||
func (s *ProblemFieldError) SetPointer(val string) {
|
||||
s.Pointer = val
|
||||
}
|
||||
|
||||
// SetCode sets the value of Code.
|
||||
func (s *ProblemFieldError) SetCode(val string) {
|
||||
s.Code = val
|
||||
}
|
||||
|
||||
// SetMessage sets the value of Message.
|
||||
func (s *ProblemFieldError) SetMessage(val string) {
|
||||
s.Message = val
|
||||
}
|
||||
|
||||
// Part of the request that contains the invalid field.
|
||||
// Ref: #/components/schemas/ProblemFieldLocation
|
||||
type ProblemFieldLocation string
|
||||
|
||||
const (
|
||||
ProblemFieldLocationBody ProblemFieldLocation = "body"
|
||||
ProblemFieldLocationQuery ProblemFieldLocation = "query"
|
||||
ProblemFieldLocationPath ProblemFieldLocation = "path"
|
||||
ProblemFieldLocationHeader ProblemFieldLocation = "header"
|
||||
ProblemFieldLocationCookie ProblemFieldLocation = "cookie"
|
||||
)
|
||||
|
||||
// AllValues returns all ProblemFieldLocation values.
|
||||
func (ProblemFieldLocation) AllValues() []ProblemFieldLocation {
|
||||
return []ProblemFieldLocation{
|
||||
ProblemFieldLocationBody,
|
||||
ProblemFieldLocationQuery,
|
||||
ProblemFieldLocationPath,
|
||||
ProblemFieldLocationHeader,
|
||||
ProblemFieldLocationCookie,
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalText implements encoding.TextMarshaler.
|
||||
func (s ProblemFieldLocation) MarshalText() ([]byte, error) {
|
||||
switch s {
|
||||
case ProblemFieldLocationBody:
|
||||
return []byte(s), nil
|
||||
case ProblemFieldLocationQuery:
|
||||
return []byte(s), nil
|
||||
case ProblemFieldLocationPath:
|
||||
return []byte(s), nil
|
||||
case ProblemFieldLocationHeader:
|
||||
return []byte(s), nil
|
||||
case ProblemFieldLocationCookie:
|
||||
return []byte(s), nil
|
||||
default:
|
||||
return nil, errors.Errorf("invalid value: %q", s)
|
||||
}
|
||||
}
|
||||
|
||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||
func (s *ProblemFieldLocation) UnmarshalText(data []byte) error {
|
||||
switch ProblemFieldLocation(data) {
|
||||
case ProblemFieldLocationBody:
|
||||
*s = ProblemFieldLocationBody
|
||||
return nil
|
||||
case ProblemFieldLocationQuery:
|
||||
*s = ProblemFieldLocationQuery
|
||||
return nil
|
||||
case ProblemFieldLocationPath:
|
||||
*s = ProblemFieldLocationPath
|
||||
return nil
|
||||
case ProblemFieldLocationHeader:
|
||||
*s = ProblemFieldLocationHeader
|
||||
return nil
|
||||
case ProblemFieldLocationCookie:
|
||||
*s = ProblemFieldLocationCookie
|
||||
return nil
|
||||
default:
|
||||
return errors.Errorf("invalid value: %q", data)
|
||||
}
|
||||
}
|
||||
|
||||
// ProblemStatusCode wraps Problem with StatusCode.
|
||||
type ProblemStatusCode struct {
|
||||
StatusCode int
|
||||
Response Problem
|
||||
}
|
||||
|
||||
// GetStatusCode returns the value of StatusCode.
|
||||
func (s *ProblemStatusCode) GetStatusCode() int {
|
||||
return s.StatusCode
|
||||
}
|
||||
|
||||
// GetResponse returns the value of Response.
|
||||
func (s *ProblemStatusCode) GetResponse() Problem {
|
||||
return s.Response
|
||||
}
|
||||
|
||||
// SetStatusCode sets the value of StatusCode.
|
||||
func (s *ProblemStatusCode) SetStatusCode(val int) {
|
||||
s.StatusCode = val
|
||||
}
|
||||
|
||||
// SetResponse sets the value of Response.
|
||||
func (s *ProblemStatusCode) SetResponse(val Problem) {
|
||||
s.Response = val
|
||||
}
|
||||
Reference in new issue
Block a user