jsonresp

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInternalServerError = HttpError{
		ErrCode:    "internal_server_error",
		ErrMsg:     "internal server error; please contact the administrator",
		HttpStatus: http.StatusInternalServerError,
	}
	ErrHttpBadRequest = HttpError{
		ErrCode:    "http_bad_request",
		ErrMsg:     "bad request",
		HttpStatus: http.StatusBadRequest,
	}
	ErrHttpForbidden = HttpError{
		ErrCode:    "http_forbidden",
		ErrMsg:     "forbidden",
		HttpStatus: http.StatusForbidden,
	}
	ErrHttpUnauthorized = HttpError{
		ErrCode:    "http_unauthorized",
		ErrMsg:     "unauthorized",
		HttpStatus: http.StatusUnauthorized,
	}
)

Functions

func BadRequest

func BadRequest(w http.ResponseWriter, errMsg string)

shorthand for jsonresp.FromError(w, ErrHttpBadRequest.WithMsg(errMsg))

func Forbidden

func Forbidden(w http.ResponseWriter, errMsg string)

shorthand for jsonresp.FromError(w, ErrHttpForbidden.WithMsg(errMsg))

func HandleErrorWithContext

func HandleErrorWithContext(ctx context.Context, w http.ResponseWriter, err error)

HandleErrorWithContext is a convenience function that extracts the logger from the context

func HandleSrvcError

func HandleSrvcError(logger *slog.Logger, w http.ResponseWriter, err error)

HandleSrvcError handles service errors and writes appropriate HTTP responses

func InternalError

func InternalError(w http.ResponseWriter)

shorthand for jsonresp.FromError(w, ErrInternalServerError)

func Success

func Success(w http.ResponseWriter, data any) error

func Unauthorized

func Unauthorized(w http.ResponseWriter, errMsg string)

shorthand for jsonresp.FromError(w, ErrHttpUnauthorized.WithMsg(errMsg))

func WriteCustom

func WriteCustom(w http.ResponseWriter, errMsg string, statusCode int, errCode string) error

func WriteError

func WriteError(w http.ResponseWriter, err error)

assume error implements httpStatusCoder interface

Types

type HttpError

type HttpError struct {
	ErrCode    string // short well-formatted identifier
	ErrMsg     string
	HttpStatus int
}

func (HttpError) Error

func (e HttpError) Error() string

func (HttpError) ErrorCode

func (e HttpError) ErrorCode() string

func (HttpError) HttpStatusCode

func (e HttpError) HttpStatusCode() int

func (HttpError) Is

func (e HttpError) Is(err error) bool

func (HttpError) WithMsg

func (e HttpError) WithMsg(errMsg string) HttpError

type HttpStatusCoder

type HttpStatusCoder interface {
	Error() string
	HttpStatusCode() int
	ErrorCode() string
}

type JsonResponse

type JsonResponse struct {
	Status  string `json:"status"` // "success" or "error"
	Data    any    `json:"data"`
	ErrCode string `json:"code,omitempty"`
	ErrMsg  string `json:"message,omitempty"`
}

Jump to

Keyboard shortcuts

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