wscutils

package
v0.0.0-...-7f35424 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	//HTTP responses
	ErrorStatus   = "error"
	SuccessStatus = "success"
)
View Source
const (
	ErrcodeUnknown                 = "unknown"
	ERRCODE_INVALID_REQUEST        = "invalid_request"
	ErrcodeInvalidJson             = "invalid_json"
	ErrcodeDatabaseError           = "database_error"
	ErrcodeRequestUserInvalid      = "request_user_invalid"
	ErrcodeMissing                 = "missing"
	ErrcodeTokenMissing            = "token_missing"
	ErrcodeTokenVerificationFailed = "token_verification_failed"
	ErrcodeTokenCacheFailed        = "token_cache_failed"
)

Variables

This section is empty.

Functions

func BindJSON

func BindJSON(c *gin.Context, data any) error

BindJson provides a standard way of binding incoming JSON data to a given request data structure. It incorporates error handling .

func GetRequestUser

func GetRequestUser(c *gin.Context) (string, error)

GetRequestUser extracts the requestUser from the gin context.

func LoadErrorTypes

func LoadErrorTypes(r io.Reader)

loadErrorTypes is used for loading predefined error types

func SendErrorResponse

func SendErrorResponse(c *gin.Context, response *Response)

SendErrorResponse sends a JSON error response.

func SendSuccessResponse

func SendSuccessResponse(c *gin.Context, response *Response)

SendSuccessResponse sends a JSON response.

Types

type ErrorMessage

type ErrorMessage struct {
	MsgID   int      `json:"msgid"`
	ErrCode string   `json:"errcode"`
	Field   *string  `json:"field,omitempty"` // make it a pointer so it can be omitted
	Vals    []string `json:"vals,omitempty"`  // omit if Vals is empty
}

ErrorMessage defines the format of error part of the standard response object See: https://redmine.bquanta.xyz/projects/mail-doc/wiki/Websvcgeneral#Web-service-response-format

func BuildErrorMessage

func BuildErrorMessage(errcode string, fieldName *string, vals ...string) ErrorMessage

BuildErrorMessage generates a ErrorMessage which includes the required validation error information such as code, msgcode It encapsulates the process of building an error message for consistency. Examples: Without vals errorMessage := BuildErrorMessage("field1", "error1")

With vals errorMessage := BuildErrorMessage("field2", "error2", "val1", "val2")

func WscValidate

func WscValidate[T any](data T, getVals func(err validator.FieldError) []string) []ErrorMessage

WscValidate is a generic function that accepts any data structure, validates it according to struct tag-provided validation rules and returns a slice of ErrorMessage in case of validation errors. This function will not add `vals` that's required as per the specifications because it does not know the request-specific values. `vals` will be added to ErrorMessage by the caller.

type Request

type Request struct {
	Data any `json:"data" binding:"required"`
}

Request represents the standard structure of a request to the web service.

type Response

type Response struct {
	Status   string         `json:"status"`
	Data     any            `json:"data"`
	Messages []ErrorMessage `json:"messages"`
}

Response represents the standard structure of a response of the web service.

func NewErrorResponse

func NewErrorResponse(errcode string) *Response

NewErrorResponse simplifies the process of creating a standard error response with a single error message

func NewResponse

func NewResponse(status string, data any, messages []ErrorMessage) *Response

NewResponse is a helper function to create a new web service response and any error messages that might need to be sent back to the client. It allows for a consistent structure in all API responses

func NewSuccessResponse

func NewSuccessResponse(data any) *Response

NewSuccessResponse simplifies the process of creating a standard success response

Jump to

Keyboard shortcuts

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