Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GinHandleErrorAndAbort ¶
GinHandleErrorAndAbort is a convenience function that handles error checking, logging, and response in a single call. It's useful for early returns in handlers.
If err is not nil:
- Creates a RorError from the error
- Logs the error with context and any provided fields
- Aborts the Gin request with appropriate JSON response
- Returns true
If err is nil:
- Does nothing
- Returns false
Parameters:
- c: Gin context for the current request
- status: HTTP status code to return if error is not nil
- err: Error to check and handle (can be nil)
- fields: Optional structured logging fields
Returns:
- true if error was handled, false if err was nil
Example:
func Handler(c *gin.Context) {
data, err := fetchData()
if rorerror.GinHandleErrorAndAbort(c, 500, err, rlog.String("source", "database")) {
return // Error was handled
}
// Continue processing...
}
Types ¶
type Field ¶
Field is a type alias for rlog.Field, used for adding structured logging fields to error logs. This allows consistent field formatting across the application.
type RorGinError ¶
type RorGinError interface {
rorerror.RorError
// GinLogErrorAbort logs the error with context and aborts the Gin request
// with a JSON response containing the error details.
GinLogErrorAbort(c *gin.Context, fields ...Field)
// GinLogErrorJSON logs the error with context and returns a JSON response
// containing the error details without aborting the request.
GinLogErrorJSON(c *gin.Context, fields ...Field)
}
func NewRorGinError ¶
func NewRorGinError(status int, err string, errors ...error) RorGinError
func NewRorGinErrorFromError ¶
func NewRorGinErrorFromError(status int, err error) RorGinError
type RorGinErrorData ¶
func (RorGinErrorData) GinLogErrorAbort ¶
func (e RorGinErrorData) GinLogErrorAbort(c *gin.Context, fields ...Field)
GinLogErrorAbort logs the error with context and aborts the Gin request with a JSON response containing the error details. This is the most common way to handle errors in Gin handlers.
The JSON response contains:
- status: HTTP status code
- message: Error message
After calling this method:
- The request is aborted (no further handlers execute)
- The error is logged with full context
- A JSON error response is sent to the client
Parameters:
- c: Gin context for the current request
- fields: Optional structured logging fields (e.g., rlog.String("key", "value"))
Example:
func Handler(c *gin.Context) {
if !isAuthorized {
err := rorerror.NewRorError(403, "unauthorized")
err.GinLogErrorAbort(c, rlog.String("user_id", userId))
return
}
}
func (RorGinErrorData) GinLogErrorJSON ¶
func (e RorGinErrorData) GinLogErrorJSON(c *gin.Context, fields ...Field)
GinLogErrorJSON logs the error with context and returns the error as a JSON response. Unlike GinLogErrorAbort, this does not abort the request, allowing the handler to continue processing if needed.
The JSON response contains:
- status: HTTP status code
- message: Error message
Parameters:
- c: Gin context for the current request
- fields: Optional structured logging fields (e.g., rlog.String("key", "value"))
Example:
func Handler(c *gin.Context) {
err := rorerror.NewRorError(400, "validation failed")
err.GinLogErrorJSON(c, rlog.String("field", "email"))
// Handler can continue if needed
}