errors

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

README

Errors

Standardizing HTTP error responses

Error

Field Type Description
Code int The HTTP response code that would be associated with this error
Message string The error message to convey
Details string (Optional) additional details relevant to the error

ErrorResponse

Field Type Description
Code int The highest HTTP response code found among the errors
Errors []Error The errors included in the response
RequestID string (Optional) the ID associated with the request

Errors can be be treated as a collection. For a collection of errors the highest Code for the available errors is used, defaulting to 500.

If no errors matching the Error type are included a default error response will be used.

When writing errors to a HTTP response the result will be JSON that looks like

{
	"errors": [
		{
		"message": "not authorized"
		}, 
		{
			"message": "resource not found",
			"details": "test-resource"
		}
	]
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteError

func WriteError(ctx context.Context, w http.ResponseWriter, errs ...error)

Write sets the HTTP response status code and body on the response A request ID will be included if available If the error response code is 500 or higher, the error will be recorded in the span

Types

type Error

type Error struct {
	Code    int    `json:"-"`
	Message string `json:"message"`
	Details string `json:"details,omitempty"`
}

func NewError

func NewError(code int, message, details string) *Error

NewError creates a new error with HTTP status code, message, and (optional) details

func (*Error) Error

func (e *Error) Error() string

Error converts the Error to a string

type ErrorResponse

type ErrorResponse struct {
	RequestID string  `json:"requestID,omitempty"`
	Errors    []Error `json:"errors"`
	Code      int     `json:"-"`
}

ErrorResponse is a collection of errors intended to standardize error responses

func NewErrorResponse

func NewErrorResponse(requestID string, errs ...error) *ErrorResponse

NewErrorResponse creates a new error response with the given request ID and errors it returns the highest HTTP status code found in the errors, defaulting to 500 Internal Server Error Errors from this package are included in the response If there are no errors that are from this package a default error is used

func (*ErrorResponse) Error

func (e *ErrorResponse) Error() string

Error joins the messages from the inner errors in the ErrorResponse

Jump to

Keyboard shortcuts

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