Documentation
¶
Index ¶
- Variables
- func BadRequest(ctx any, message string, format ...string) error
- func CustomError(ctx any, key string, message string, format ...string) (err error)
- func Expiration(ctx any, format ...string) error
- func Forbidden(ctx any, format ...string) error
- func InternalError(ctx any, format ...string) error
- func InvalidFields(ctx any, format ...string) error
- func ManyRequest(ctx any, format ...string) error
- func NotFound(ctx any, format ...string) error
- func OutdatedVersion(ctx any, format ...string) error
- func Send(ctx any, data any, format ...string) (err error)
- func ServiceWork(ctx any, format ...string) error
- func Timeout(ctx any, format ...string) error
- func Unauthorized(ctx any, format ...string) error
- func UnknownError(ctx any, format ...string) error
- type ByteSender
- type EchoContext
- type EchoResponse
- type FiberContext
- type GinContext
- type GinWriter
- type Header
Constants ¶
This section is empty.
Variables ¶
var (
ErrorUnsupportedContextType = errors.New("unsupported context type")
)
Functions ¶
func BadRequest ¶
BadRequest sends a standardized 400 Bad Request error response. It provides a consistent way to handle validation and malformed request errors.
Parameters:
- ctx: Request context compatible with multiple frameworks (Echo, Gin, Fiber, etc.)
- message: Human-readable error description detailing what was invalid
- format: Optional response format specification:
- "json" for JSON format (default)
- "msgpack" for binary MessagePack format
- Other custom formats if supported by the sender
Returns:
- error: Returns an error if serialization or response sending fails
Behavior:
- Sets HTTP status code implicitly through CustomError
- Uses standardized error key "BAD_REQUEST"
- Formats response according to requested format
- Maintains consistent error response structure: { "error": { "key": "BAD_REQUEST", "message": "[provided message]" } }
Example:
err := msg.BadRequest(c, "Invalid email format", "json") if err != nil { log.Printf("Failed to send error: %v", err) }
func CustomError ¶
CustomError sends a custom error response in specified format (JSON or MessagePack). It provides a unified interface for error handling across different web frameworks.
Parameters:
- ctx: Context object compatible with various web frameworks (Echo, Gin, Fiber etc.)
- key: Unique error identifier/error code (e.g., "AUTH_REQUIRED", "NOT_FOUND")
- message: Human-readable error description
- format: Optional format specification:
- "json" for JSON response (default)
- "msgpack" for MessagePack binary format
Returns:
- error: Returns serialization or sending error if any occurs
Behavior: - Automatically sets appropriate Content-Type header - Supports multiple web frameworks through unified interface - Provides consistent error response structure - Defaults to JSON format if none specified
func Expiration ¶
Expiration sends a pre-serialized error message to the client.
Parameters:
- ctx: Request context (compatible with Fiber, Echo, Gin or other frameworks)
- format: Optional format parameter ("json" or "msgpack"). Defaults to JSON.
Returns:
- error: Returns an error if there were problems sending the message
Behavior: - Automatically sets appropriate Content-Type header - Supports multiple frameworks through unified interface - Uses pre-serialized data for maximum performance - Defaults to JSON format if none specified
func Forbidden ¶
Forbidden sends a pre-serialized error message to the client.
Parameters:
- ctx: Request context (compatible with Fiber, Echo, Gin or other frameworks)
- format: Optional format parameter ("json" or "msgpack"). Defaults to JSON.
Returns:
- error: Returns an error if there were problems sending the message
Behavior: - Automatically sets appropriate Content-Type header - Supports multiple frameworks through unified interface - Uses pre-serialized data for maximum performance - Defaults to JSON format if none specified
func InternalError ¶ added in v0.0.2
InternalError sends a pre-serialized error message to the client.
Parameters:
- ctx: Request context (compatible with Fiber, Echo, Gin or other frameworks)
- format: Optional format parameter ("json" or "msgpack"). Defaults to JSON.
Returns:
- error: Returns an error if there were problems sending the message
Behavior: - Automatically sets appropriate Content-Type header - Supports multiple frameworks through unified interface - Uses pre-serialized data for maximum performance - Defaults to JSON format if none specified
func InvalidFields ¶
InvalidFields sends a pre-serialized error message to the client.
Parameters:
- ctx: Request context (compatible with Fiber, Echo, Gin or other frameworks)
- format: Optional format parameter ("json" or "msgpack"). Defaults to JSON.
Returns:
- error: Returns an error if there were problems sending the message
Behavior: - Automatically sets appropriate Content-Type header - Supports multiple frameworks through unified interface - Uses pre-serialized data for maximum performance - Defaults to JSON format if none specified
func ManyRequest ¶
ManyRequest sends a pre-serialized error message to the client.
Parameters:
- ctx: Request context (compatible with Fiber, Echo, Gin or other frameworks)
- format: Optional format parameter ("json" or "msgpack"). Defaults to JSON.
Returns:
- error: Returns an error if there were problems sending the message
Behavior: - Automatically sets appropriate Content-Type header - Supports multiple frameworks through unified interface - Uses pre-serialized data for maximum performance - Defaults to JSON format if none specified
func NotFound ¶
NotFound sends a pre-serialized error message to the client.
Parameters:
- ctx: Request context (compatible with Fiber, Echo, Gin or other frameworks)
- format: Optional format parameter ("json" or "msgpack"). Defaults to JSON.
Returns:
- error: Returns an error if there were problems sending the message
Behavior: - Automatically sets appropriate Content-Type header - Supports multiple frameworks through unified interface - Uses pre-serialized data for maximum performance - Defaults to JSON format if none specified
func OutdatedVersion ¶
OutdatedVersion sends a pre-serialized error message to the client.
Parameters:
- ctx: Request context (compatible with Fiber, Echo, Gin or other frameworks)
- format: Optional format parameter ("json" or "msgpack"). Defaults to JSON.
Returns:
- error: Returns an error if there were problems sending the message
Behavior: - Automatically sets appropriate Content-Type header - Supports multiple frameworks through unified interface - Uses pre-serialized data for maximum performance - Defaults to JSON format if none specified
func Send ¶
Send is a handler function that serializes the given data into JSON format and sends it as a response. It accepts any data type that can be marshaled to JSON.
func ServiceWork ¶
ServiceWork sends a pre-serialized error message to the client.
Parameters:
- ctx: Request context (compatible with Fiber, Echo, Gin or other frameworks)
- format: Optional format parameter ("json" or "msgpack"). Defaults to JSON.
Returns:
- error: Returns an error if there were problems sending the message
Behavior: - Automatically sets appropriate Content-Type header - Supports multiple frameworks through unified interface - Uses pre-serialized data for maximum performance - Defaults to JSON format if none specified
func Timeout ¶
Timeout sends a pre-serialized error message to the client.
Parameters:
- ctx: Request context (compatible with Fiber, Echo, Gin or other frameworks)
- format: Optional format parameter ("json" or "msgpack"). Defaults to JSON.
Returns:
- error: Returns an error if there were problems sending the message
Behavior: - Automatically sets appropriate Content-Type header - Supports multiple frameworks through unified interface - Uses pre-serialized data for maximum performance - Defaults to JSON format if none specified
func Unauthorized ¶
Unauthorized sends a pre-serialized error message to the client.
Parameters:
- ctx: Request context (compatible with Fiber, Echo, Gin or other frameworks)
- format: Optional format parameter ("json" or "msgpack"). Defaults to JSON.
Returns:
- error: Returns an error if there were problems sending the message
Behavior: - Automatically sets appropriate Content-Type header - Supports multiple frameworks through unified interface - Uses pre-serialized data for maximum performance - Defaults to JSON format if none specified
func UnknownError ¶ added in v0.0.2
UnknownError sends a pre-serialized error message to the client.
Parameters:
- ctx: Request context (compatible with Fiber, Echo, Gin or other frameworks)
- format: Optional format parameter ("json" or "msgpack"). Defaults to JSON.
Returns:
- error: Returns an error if there were problems sending the message
Behavior: - Automatically sets appropriate Content-Type header - Supports multiple frameworks through unified interface - Uses pre-serialized data for maximum performance - Defaults to JSON format if none specified
Types ¶
type ByteSender ¶ added in v0.1.0
ByteSender defines the interface for sending raw byte data. It's used as a fallback for custom context types not natively supported.
type EchoContext ¶ added in v0.1.0
type EchoContext interface {
Response() EchoResponse // Get response writer
}
EchoContext represents the Echo framework's request context interface.
type EchoResponse ¶ added in v0.1.0
type EchoResponse interface { Header() http.Header // Get header setter WriteHeader(code int) // Write status code Write(b []byte) (n int, err error) // Write response body }
EchoResponse represents Echo's response writer interface.
type FiberContext ¶ added in v0.1.0
type FiberContext interface { Set(string, string) // Set response header Status(int) any // Set HTTP status code Write([]byte) error // Write raw response }
FiberContext represents the Fiber framework's request context interface.
type GinContext ¶ added in v0.1.0
type GinContext interface { Writer() GinWriter // Get response writer Status(int) any // Set HTTP status code }
GinContext represents the Gin framework's request context interface.