wscutils

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2024 License: Apache-2.0 Imports: 5 Imported by: 27

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

View Source
var DefaultErrCode string

DefaultErrCode holds the default error code for validation errors. Its value can be set using the SetDefaultErrCode function.

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 SendErrorResponse added in v0.4.0

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

SendErrorResponse sends a JSON error response.

func SendSuccessResponse added in v0.4.0

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

SendSuccessResponse sends a JSON response.

func SetDefaultErrCode added in v0.10.0

func SetDefaultErrCode(errCode string)

SetDefaultErrCode allows external code to set a custom default error code for validation errors. Similar to SetDefaultMsgID, this function sets a fallback error code to be used when a specific validation error does not have an error code registered.

func SetDefaultMsgID added in v0.10.0

func SetDefaultMsgID(msgID int)

SetDefaultMsgID allows external code to set a custom default message ID for validation errors. This ID is used as a fallback when a specific validation error does not have a message ID registered via SetValidationTagToMsgIDMap.

func SetErrCodeInvalidJSON added in v0.10.0

func SetErrCodeInvalidJSON(errCode string)

SetErrCodeInvalidJSON allows external code to set a custom error code for errors related to invalid JSON.

func SetMsgIDInvalidJSON added in v0.10.0

func SetMsgIDInvalidJSON(msgID int)

SetMsgIDInvalidJSON allows external code to set a custom message ID for errors related to invalid JSON.

func SetValidationTagToErrCodeMap added in v0.10.0

func SetValidationTagToErrCodeMap(customMap map[string]string)

SetValidationTagToErrCodeMap updates the internal mapping of validation tags to error codes. Similar to SetValidationTagToMsgIDMap, this function customizes the error codes returned in the response for specific validation errors. The customMap parameter should contain a mapping of validation tags to their corresponding error codes.

func SetValidationTagToMsgIDMap added in v0.10.0

func SetValidationTagToMsgIDMap(customMap map[string]int)

SetValidationTagToMsgIDMap updates the internal mapping of validation tags to message IDs. This function allows for the customization of message IDs associated with specific validation errors. The customMap parameter should contain a mapping of validation tags (e.g., "required", "email") to their corresponding message IDs.

Types

type ErrorMessage

type ErrorMessage struct {
	MsgID   int      `json:"msgid"`
	ErrCode string   `json:"errcode"`
	Field   string   `json:"field,omitempty"`
	Vals    []string `json:"vals,omitempty"`
}

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(msgid int, 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(1001, "retry", "field1", "error1")

With vals errorMessage := BuildErrorMessage(1000, "invalid", "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(msgid int, errcode string) *Response

NewErrorResponse simplifies the process of creating a standard error response with a single error message. Now updated to include a msgid.

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