helper

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MimeTypeNOSNIFF prevents browsers from MIME type sniffing
	MimeTypeNOSNIFF = "nosniff"
)

Variables

This section is empty.

Functions

func JSONError

func JSONError(w http.ResponseWriter, err *Error, code int)

JSONError writes a JSON-formatted error response. It sets appropriate headers (Content-Type, X-Content-Type-Options) and writes the error with the given status code.

func WriteError

func WriteError(w http.ResponseWriter, r *http.Request, err string, code int)

WriteError intelligently writes an error response based on the request's Accept and Content-Type headers. If the client expects JSON, it responds with JSONError; otherwise, it falls back to plain text via http.Error.

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error represents a structured error response for JSON APIs. It implements both error and fmt.Stringer interfaces.

func NewError

func NewError(error string) *Error

NewError creates a new Error with the given message.

func (*Error) Error

func (e *Error) Error() string

Error returns the error message, implementing the error interface.

func (*Error) MarshalJSON

func (e *Error) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler for consistent JSON error format. Produces: {"error":"message"}

Jump to

Keyboard shortcuts

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