lres

package
v1.3.7 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ExposeServerErrors = true

ExposeServerErrors is a boolean indicating whether the Error function should expose errors of status code 500 or above to clients. If false, the name of the status code is used as the error message instead.

Functions

func Custom added in v1.3.7

func Custom(httpStatus int, headers map[string]string, data interface{}) (
	events.APIGatewayProxyResponse,
	error,
)

Custom generated an events.APIGatewayProxyResponse object that can be directly returned via the lambda's handler function. It receives an HTTP status code for the response, a map of HTTP headers (can be empty or nil), and a value (probably a struct) representing the response body. This value will be marshaled to JSON (currently without base 64 encoding).

func Empty added in v1.3.7

Empty returns a simple empty events.APIGatewayProxyResponse with http.StatusOK

func Error added in v1.3.7

Error generates an events.APIGatewayProxyResponse from an error value. If the error is an HTTPError, the response's status code will be taken from the error. Otherwise, the error is assumed to be 500 Internal Server Error. Regardless, all errors will generate a JSON response in the format `{ "code": 500, "error": "something failed" }` This format cannot currently be changed. If you do not wish to expose server errors (i.e. errors whose status code is 500 or above), set the ExposeServerErrors global variable to false.

func File added in v1.3.7

func File(contentType string, headers map[string]string, fileBytes []byte) (events.APIGatewayProxyResponse, error)

File generates a new events.APIGatewayProxyResponse with the ContentTypeKey header set appropriately, the file bytes added to the response body, and the http status set to http.StatusOK

func FileB64 added in v1.3.7

func FileB64(contentType string, headers map[string]string, fileBytes []byte) (events.APIGatewayProxyResponse, error)

FileB64 generates a new events.APIGatewayProxyResponse with the ContentTypeKey header set appropriately, the file bytes encoded to base64, and the http status set to http.StatusOK

func StatusAndError added in v1.3.7

func StatusAndError(httpStatus int, err error) (events.APIGatewayProxyResponse, error)

StatusAndError generates a custom error return response with the given http status code and error. Setting ExposeServerErrors to false will prevent leaking data to clients.

func Success added in v1.3.7

func Success(data interface{}) (events.APIGatewayProxyResponse, error)

Success wraps Custom assuming a http.StatusOK status code and no custom headers to return. This was such a common use case I felt it necessary to create a wrapper to make everyone's life easier.

func Unmarshal added in v1.3.7

func Unmarshal(res events.APIGatewayProxyResponse, target interface{}) error

Unmarshal should generally be used only when testing as normally you return the response directly to the caller and won't need to Unmarshal it. However, if you are testing locally then it will help you extract the response body of a lambda request and marshal it to an object.

Types

type HTTPError

type HTTPError struct {
	Status  int    `json:"status"`
	Message string `json:"message"`
}

HTTPError is a generic struct type for JSON error responses. It allows the library to assign an HTTP status code for the errors returned by its various functions.

func (HTTPError) Error

func (err HTTPError) Error() string

Error returns a string representation of the HTTPError instance.

Jump to

Keyboard shortcuts

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