Documentation ¶
Index ¶
- Constants
- func BindJSON(c *gin.Context, data any) error
- func GetRequestUser(c *gin.Context) (string, error)
- func LoadErrorTypes(r io.Reader)
- func SendErrorResponse(c *gin.Context, response *Response)
- func SendSuccessResponse(c *gin.Context, response *Response)
- type ErrorMessage
- type Request
- type Response
Constants ¶
const ( //HTTP responses ErrorStatus = "error" SuccessStatus = "success" )
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 ¶
BindJson provides a standard way of binding incoming JSON data to a given request data structure. It incorporates error handling .
func GetRequestUser ¶
GetRequestUser extracts the requestUser from the gin context.
func LoadErrorTypes ¶
loadErrorTypes is used for loading predefined error types
func SendErrorResponse ¶
SendErrorResponse sends a JSON error response.
func SendSuccessResponse ¶
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 ¶
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 ¶
NewSuccessResponse simplifies the process of creating a standard success response