wrapped

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package wrapped provides the wrapped.Response.

Index

Constants

View Source
const (
	// StatusFail will be set if StatusCode is 5XX.
	StatusFail = "fail"
	// StatusError will be set if StatusCode is 4XX.
	StatusError = "error"
	// StatusSuccess will be set if StatusCode is 1XX, 2XX or 3XX.
	StatusSuccess = "success"
)

Variables

This section is empty.

Functions

func CtxSetShowError

func CtxSetShowError(ctx context.Context, show bool) context.Context

CtxSetShowError will set ctxKeyShowError on ctx.

If set to true, Response.Data will contain details about the error.

func ShowErrorFromCtx

func ShowErrorFromCtx(ctx context.Context) bool

ShowErrorFromCtx will get ctxKeyShowError on ctx.

Types

type DataResponder

type DataResponder interface {
	Data() interface{}
}

DataResponder will set the Data field on Response.

type MsgResponder

type MsgResponder interface {
	Message() string
}

MsgResponder will set the Message field on Response.

type Response

type Response struct {
	// Code contains the HTTP response status code as an integer.
	Code int `json:"code"`
	// Status contains the text: “success”, “fail”, or “error”.
	Status string `json:"status"`
	// Message is only used for “fail” and “error” statuses to contain the error message.
	//
	// For internationalization (i18n) purposes, this could contain a message number or code,
	// either alone or contained within delimiters.
	Message string `json:"message,omitempty"`
	// Data can contain user provides data,
	Data interface{} `json:"data,omitempty"`
	// Err contains the error
	Err error `json:"-"`
}

Response represents a response from a rest endpoint.

The idea comes from https://www.restapitutorial.com/resources.html

Details

Code value will determined base on the following order:

  • If field is already set, the already set value will be used
  • If Err will implement StatusCodeResponder the result will be used
  • If Err != nil the value will be http.StatusInternalServerError
  • Else the value will be http.StatusSuccess

Status will be one of

  • “fail” for HTTP status code response values from 500-599,
  • “error” for HTTP status code response values from 400-499,
  • “success” for everything else (e.g. 1XX, 2XX and 3XX responses).

Message value will be determined based on the following order:

  • If status is "success" this field will always be "" and not present in json
  • If the field is already set, the already set value will be used
  • If status is not "success" and Err will implement MsgResponder, this field will be MsgResponder.ResponseMessage()
  • If status is "error" this will be Err.Error()
  • If status is "fail" and ctxKeyShowError is true this will be Err.Error()
  • Else it will contain http.StatusText(Code)

Data value will be determined base on the following order:

  • if status is "success" it could contain the response body
  • If the field is already set, the already set value will be used
  • If the status is not "success" it could contain the cause/exception (depends on ShowErrorFromCtx).

func (*Response) Parse

func (res *Response) Parse(ctx context.Context)

Parse will prepare Response for rendering.

func (*Response) Render

func (res *Response) Render(w http.ResponseWriter, r *http.Request) error

Render implements chi's render.Renderer interface.

func (*Response) Reset

func (res *Response) Reset()

Reset will clear and reset the struct for reuse (e.g. object pool).

type StatusCodeResponder

type StatusCodeResponder interface {
	StatusCode() int
}

StatusCodeResponder will set the Status field on Response.

Jump to

Keyboard shortcuts

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