Documentation ¶
Index ¶
- Constants
- func Error(w http.ResponseWriter, msg string, status int) error
- func MustWrite(w http.ResponseWriter, err error) error
- func Write(w http.ResponseWriter, err error) error
- type HTTPError
- type ProblemDetails
- func (pd ProblemDetails) Error() string
- func (pd *ProblemDetails) Errorf(fmtstr string, args ...interface{}) *ProblemDetails
- func (pd ProblemDetails) GetStatus() int
- func (pd ProblemDetails) Unwrap() error
- func (pd *ProblemDetails) WithDetail(msg string) *ProblemDetails
- func (pd *ProblemDetails) WithErr(err error) *ProblemDetails
- func (pd *ProblemDetails) Write(w http.ResponseWriter) error
- type ValidationError
- type ValidationProblem
Constants ¶
const ContentProblemDetails = "application/problem+json"
ContentProblemDetails is the correct MIME type to use when returning a problem details object as JSON.
Variables ¶
This section is empty.
Functions ¶
func Error ¶
func Error(w http.ResponseWriter, msg string, status int) error
Error is used just like http.Error to create and immediately issue an error.
Types ¶
type HTTPError ¶
type HTTPError interface {
GetStatus() int
}
HTTPError is the minimal interface needed to be able to Write a problem, defined so that ProblemDetails can be encapsulated and expanded as needed.
type ProblemDetails ¶
type ProblemDetails struct { Status int `json:"status,omitempty" oas-desc:"HTTP status code for the response"` Title string `json:"title,omitempty" oas-desc:"Title of the problem"` Detail string `json:"detail,omitempty" oas-desc:"Detailed description of the problem"` Type string `json:"type,omitempty" oas-desc:"Type of problem"` Instance string `json:"instance,omitempty" oas-desc:"Instance affected by the problem"` // contains filtered or unexported fields }
ProblemDetails provides a standard encapsulation for problems encountered in web applications and REST APIs.
func Errorf ¶
func Errorf(status int, fmtstr string, args ...interface{}) *ProblemDetails
Errorf is used like fmt.Errorf to create and return errors. It takes an extra first argument of the HTTP status to use.
func New ¶
func New(status int) *ProblemDetails
New returns a ProblemDetails error object with the given HTTP status code.
func (ProblemDetails) Error ¶
func (pd ProblemDetails) Error() string
New implements the error interface, so ProblemDetails objects can be used as regular error return values.
func (*ProblemDetails) Errorf ¶
func (pd *ProblemDetails) Errorf(fmtstr string, args ...interface{}) *ProblemDetails
Errorf uses fmt.Errorf to add a detail message to the ProblemDetails object. It supports the %w verb.
func (ProblemDetails) GetStatus ¶
func (pd ProblemDetails) GetStatus() int
GetStatus implements the HTTPError interface
func (ProblemDetails) Unwrap ¶
func (pd ProblemDetails) Unwrap() error
Unwrap implements the Go 1.13+ unwrapping interface for errors.
func (*ProblemDetails) WithDetail ¶
func (pd *ProblemDetails) WithDetail(msg string) *ProblemDetails
WithDetail adds the supplied detail message to the problem details.
func (*ProblemDetails) WithErr ¶
func (pd *ProblemDetails) WithErr(err error) *ProblemDetails
WithErr adds an error value as a wrapped error. If the error detail message is currently blank, it is initialized from the error's New() message.
func (*ProblemDetails) Write ¶
func (pd *ProblemDetails) Write(w http.ResponseWriter) error
Write sets the HTTP response code from the ProblemDetails and then sends the entire object as JSON.
type ValidationError ¶
type ValidationError struct { FieldName string `json:"name" oas-desc:"Name of the field with failed validation"` Error string `json:"reason" oas-desc:"Description of the error"` }
ValidationError indicates a server-side validation error for data submitted as JSON or via a web form.
type ValidationProblem ¶
type ValidationProblem struct { ProblemDetails ValidationErrors []ValidationError `json:"invalid-params,omitempty" oas-desc:"Validation errors"` }
ValidationProblem is an example of extending the ProblemDetails structure as per the form validation example in section 3 of RFC 7807, to support reporting of server-side data validation errors.
func NewValidationProblem ¶
func NewValidationProblem(status int) *ValidationProblem
NewValidationProblem creates an object to represent a server-side validation error.
func (*ValidationProblem) Add ¶
func (vp *ValidationProblem) Add(field string, errmsg string)
Add adds a validation error message for the specified field to the ValidationProblem.