response

package
v0.0.0-...-9a2df54 Latest Latest
Warning

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

Go to latest
Published: May 17, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var UseDefaultCode = true

Functions

func IsJsonErrorGetDetails

func IsJsonErrorGetDetails(err error) (ok bool, e error)

IsJsonErrorGetDetails takes an error and check if it is a JSON Error. If it is, it returns true and the type of json Error Else it returns the error back as it came

func JSON

func JSON(w http.ResponseWriter, vPtr *Response, headers map[string]string)

JSON send output when using standard library it marshals 'response'struct to JSON, escapes HTML & sets the Content-Type as 'application/json' all via the standard library.

Types

type Response

type Response struct {
	StatusCode int     `json:"-"`
	Success    bool    `json:"success"`
	Message    string  `json:"message"`
	ErrorCode  *string `json:"errorCode,omitempty"`
	Data       any     `json:"data,omitempty"`
}

Response this struct represents the response structure.

func BadRequest

func BadRequest(msg string, errorCode string) *Response

BadRequest creates a error response with (HTTP 400) code

func Conflict

func Conflict(msg string, errorCode string) *Response

Conflict creates a error response with (HTTP 409) code

func Created

func Created(msg string, data any) *Response

Created creates a success response with (HTTP 201) code

func Forbidden

func Forbidden(msg string, errorCode string) *Response

Forbidden creates a error response with (HTTP 403) code

func InternalServerError

func InternalServerError(msg string, errorCode string) *Response

InternalServerError creates a error response with (HTTP 500)code

func New

func New(statusCode int, success bool, msg string, errorCode *string, data any) *Response

New creates an response struct

func NewError

func NewError(statusCode int, msg string, errorCode string) *Response

NewError creates an error response

func NewSuccess

func NewSuccess(statusCode int, msg string, data any) *Response

NewSuccess creates a success response

func NotFound

func NotFound(msg string, errorCode string) *Response

NotFound creates a error response with (HTTP 404) code

func Ok

func Ok(msg string, data any) *Response

OK creates a success response with (HTTP 200) code

func Unauthorized

func Unauthorized(msg string, errorCode string) *Response

Unauthorized creates a error response with (HTTP 401) code

func (Response) Error

func (a Response) Error() string

Error, lets satisfy golang's error interface. so we can return 'response' as an error type.

func (*Response) OverideDefaultMsg

func (a *Response) OverideDefaultMsg(msg string) *Response

OverideDefaultMsg helps overide or set a message useful in shorthand variables dfinition

Jump to

Keyboard shortcuts

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