resp

package
v0.0.0-...-4c4b7df Latest Latest
Warning

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

Go to latest
Published: May 7, 2019 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SUCCESS = 0
	FAILURE = 1
	ERROR   = 2
)

Variables

View Source
var (
	// When set to true, Failure() and Error() will write to log.
	// This is enabled by default.
	Log = true

	// HTTP codes send when appropriate function is used.
	// All status OK by default.
	SuccessHttpCode = http.StatusOK
	FailureHttpCode = http.StatusOK
	ErrorHttpCode   = http.StatusOK
)

Functions

func Error

func Error(w http.ResponseWriter, code int, msg string, data interface{}) error

Sends an error response with given code, message and data to the client. The first argument must be the ResponseWriter.

func Failure

func Failure(w http.ResponseWriter, code int, msg string, data interface{}) error

Sends a failure response with given code, message and data to the client. The first argument must be the ResponseWriter.

func Success

func Success(w http.ResponseWriter, code int, msg string, data interface{}) error

Sends a success response with given code, message and data to the client. The first argument must be the ResponseWriter.

Types

type Resp

type Resp struct {
	Type int         `json:"type"`
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
	// contains filtered or unexported fields
}

Response of type "success", "failure" or "error" with error code, a short message and additional data. The data will be marshalled to JSON and must have decorators for all fields which are supposed to be send to the client. The type can be used to differentiate between different kind of outcomes:

success = everything went fine, the operation was successful failure = the operation failed due to wrong input or usage error = a technical error occured

Jump to

Keyboard shortcuts

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