resp

package
v0.0.0-...-716047f Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2021 License: BSD-2-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package resp provides convenience methods for handling JSON responses

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert

func Assert(t *testing.T, resRec *httptest.ResponseRecorder, statusCode int, status, message, code string)

Assert makes an assertion about a Response, with optional status, message, and code checks.

func AssertError

func AssertError(t *testing.T, res *httptest.ResponseRecorder, code, message string)

AssertError confirms that the response was an error

func AssertFail

func AssertFail(t *testing.T, res *httptest.ResponseRecorder, code, message string)

AssertFail confirms that the response is a failure

func AssertOK

func AssertOK(t *testing.T, res *httptest.ResponseRecorder, message string)

AssertOK confirms that the response was successful

func BadRequest

func BadRequest(w http.ResponseWriter, code string)

BadRequest

func Error

func Error(w http.ResponseWriter, err error, code string)

Error responds with 501 and the provided code and error message status code >= 500

func ExtractData

func ExtractData(t *testing.T, res *httptest.ResponseRecorder, data interface{})

ExtractData extracts the `data` payload inside of the JSON response

func Fail

func Fail(w http.ResponseWriter, status int, data interface{}, message string, code string)

Fail responds with the given status code and the provided code and message. Code should be machine readable. Data can optionally be provided status code >= 400 < 500

func InvalidRequest

func InvalidRequest(w http.ResponseWriter)

InvalidRequest is used to notify that the request is invalid and cannot be handled

func OK

func OK(w http.ResponseWriter, data interface{}, message string)

OK is wrapper func for Success with status code 200

func Success

func Success(w http.ResponseWriter, statusCode int, data interface{}, message string)

Success responds with status 200 and the provided data and message status code >= 200 < 400

func Unauthorized

func Unauthorized(w http.ResponseWriter)

Unauthorized is used when the user does not have a valid token

func UnknownResource

func UnknownResource(w http.ResponseWriter)

UnknownResource is used when the URL is valid, but the requested resource could not be found

func UnprocessableError

func UnprocessableError(w http.ResponseWriter, message, code string)

UnprocessableError is used to return error with 422 HTTP status code.

func ValidationError

func ValidationError(w http.ResponseWriter, code string)

ValidationError is used to notify that the request's parameters/body/form-data contained invalid data

func WriteJSON

func WriteJSON(w http.ResponseWriter, data interface{}, statusCode int)

WriteJSON writes out the given data to the `http.ResponseWriter`, along with required headers.

Types

type Code

type Code = string

Code type

const (
	CodeUnknownError    Code = "UNKNOWN_ERROR"
	CodeInvalidRequest  Code = "INVALID_REQUEST"
	CodeInternalError   Code = "INTERNAL_ERROR"
	CodeValidationError Code = "VALIDATION_ERROR"
	CodeUnknownResource Code = "UNKNOWN_RESOURCE"
	CodeAuthRequired    Code = "AUTH_REQUIRED"
	CodeUnknownEndpoint Code = "UNKNOWN_ENDPOINT"
)

General code

const (
	CodeVerificationFailed Code = "VERIFICATION_FAILED"
	CodeInvalidTokenState  Code = "INVALID_TOKEN_STATE"
)

Business error code

type Resp

type Resp struct {
	Status  Status      `json:"status"`         // Response status class
	Code    Code        `json:"code,omitempty"` // A machine-readable error code in the case of failure/error responses.
	Data    interface{} `json:"data"`           // A data payload provided for success/failure responses.
	Message string      `json:"message"`        // A user-friendly message detailing the result of the operation.
}

Resp is the standard response format

func Get

func Get(body io.Reader, dataPtr interface{}) (Resp, error)

Get extracts a `resp.Resp` shaped response payload from a `http.Response`. The data will be available in the `dataPtr`, and the other JSON fields are returned.

func NewError

func NewError(code string, message string) Resp

NewError creates a new error response with the given code and optional message.

func NewFail

func NewFail(data interface{}, code string, message string) Resp

NewFail creates a failure JSON response with the given data, code, and optional message.

func NewSuccess

func NewSuccess(data interface{}, message string) Resp

NewSuccess creates a successful JSON response with the given data and optional message.

type Status

type Status = string

Status type

const (
	StatusSuccess Status = "success"
	StatusFail    Status = "fail"
	StatusError   Status = "error"
)

Status

Jump to

Keyboard shortcuts

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