resolverutils

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

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

Go to latest
Published: Jan 3, 2024 License: MPL-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	USERNAME_KEY  = "username"
	CHAT_ID_KEY   = "chatID"
	CHAT_NAME_KEY = "chatName"
	ACTION_KEY    = "action"
)

Variables

This section is empty.

Functions

func AssertHTTPError

func AssertHTTPError(t *testing.T, err *HTTPError, expectedStatus int, expectedMessage string)

func CommonSetup

func CommonSetup(body string) (*response.Writer, *http.Request, database.Connection)

Creates mock resolver arguments

func DisplayHTTPError

func DisplayHTTPError(w *response.Writer, httpError *HTTPError) bool

Provides an error div for HTMX

func GetRequestBodyAndContext

func GetRequestBodyAndContext(
	r *http.Request,
	ptr any,
	paramKeys ...string,
) (*database.User, *RouteParams, *HTTPError)

Calls `resolverutils.GetRequestBody()` then, if successful, `resolverutils.GetRequestContext()`

func GetRequestContext

func GetRequestContext(r *http.Request, paramKeys ...string) (*database.User, *RouteParams, *HTTPError)

Gets all requested context attached to a request. Writes an HTTP error response + logs on failure.

func ProcessHTTPError

func ProcessHTTPError(w *response.Writer, httpError *HTTPError) bool

Writes message and status of HTTPError to the response Returns false if httpError is nil, true otherwise

func SetContext

func SetContext(
	t *testing.T,
	req *http.Request,
	user *database.User,
	params map[string]string,
) *http.Request

Adds a user and/or parameters to a new request context

Types

type HTTPError

type HTTPError struct {
	Status  int
	Message string
}

func GetRequestBody

func GetRequestBody(r *http.Request, ptr any) *HTTPError

Decodes JSON from HTTP request body and binds it to a struct pointer. Writes an HTTP error response on failure.

func GetRequestQueryParam

func GetRequestQueryParam(r *http.Request, key string, isRequired bool) (string, *HTTPError)

Extracts query parameter from request errors if missing when isRequired is true errors if the parameter value is an empty string

func GetRequestQueryParamInt

func GetRequestQueryParamInt(r *http.Request, key string, isRequired bool) (int64, *HTTPError)

Calls GetRequestQueryParam, then casts the result to an integer

func HandleDatabaseError

func HandleDatabaseError(err error) *HTTPError

Handles an unexpected error from the database

type RouteParams

type RouteParams struct {
	Username string
	ChatID   int64
	ChatName string
}

Jump to

Keyboard shortcuts

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