response

package
v0.0.0-...-2c3b082 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2023 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

Functions

func JSONErrorResponse

func JSONErrorResponse(ctx context.Context, w http.ResponseWriter, ec ErrorCode, msg string)

JSONErrorResponse implements an error json response

func JSONResponse

func JSONResponse(ctx context.Context, w http.ResponseWriter, res *Resp)

JSONResponse enforces a specific structure on all responses to make it easier to give a uniform response. It also enforces error codes and messages so that the consumer can expect a subset rather than different from each endpoint This would also be the place to replace the writer with a gzip writer or similar but I'd expect this service to be behind a load balancer or reverse proxy which will handle TLS certs and the Accept-Encoding header parsing

func SetJSONContent

func SetJSONContent(w http.ResponseWriter)

Types

type ErrorCode

type ErrorCode string
const (
	// AlreadyExists error code
	AlreadyExists ErrorCode = "already_exists"
	// Internal error code
	Internal ErrorCode = "internal"
	// InvalidArgument error code
	InvalidArgument ErrorCode = "invalid_argument"
	// MalformedRequest error code
	MalformedRequest ErrorCode = "malformed_request"
	// MalformedResponse error code
	MalformedResponse ErrorCode = "malformed_response"
	// NotFound error code
	NotFound ErrorCode = "not_found"
	// OutOfRange error code
	OutOfRange ErrorCode = "out_of_range"
	// PermissionDenied error code
	PermissionDenied ErrorCode = "permission_denied"
	// Stale error code
	Stale ErrorCode = "stale"
	// Unauthenticated error code
	Unauthenticated ErrorCode = "unauthenticated"
	// NoContent error code
	NoContent ErrorCode = "no_content"
	// UnprocessableEntity error code
	UnprocessableEntity ErrorCode = "unprocessable_entity"
)

type Resp

type Resp struct {
	Data  interface{} `json:"data,omitempty"`
	Meta  interface{} `json:"meta,omitempty"`
	Error *RespError  `json:"error,omitempty"`
}

Resp is the response envelope. All responses from the service will allways be of the format

{
   "data": { data from the service },
   "meta: { meta data such as pagination, next url etc },
   "error": { any errors happening }
}

type RespError

type RespError struct {
	Code ErrorCode `json:"code"`
	Msg  string    `json:"msg"`
}

RespError is error response type

Jump to

Keyboard shortcuts

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