msg

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorUnsupportedContextType = errors.New("unsupported context type")
)

Functions

func BadRequest

func BadRequest(ctx any, message string, format ...string) error

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

func CustomError(ctx any, key string, message string, format ...string) (err error)

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

func Expiration(ctx any, format ...string) error

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

func Forbidden(ctx any, format ...string) error

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

func InternalError(ctx any, format ...string) error

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

func InvalidFields(ctx any, format ...string) error

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

func ManyRequest(ctx any, format ...string) error

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

func NotFound(ctx any, format ...string) error

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

func OutdatedVersion(ctx any, format ...string) error

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

func Send(ctx any, data any, format ...string) (err error)

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

func ServiceWork(ctx any, format ...string) error

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

func Timeout(ctx any, format ...string) error

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

func Unauthorized(ctx any, format ...string) error

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

func UnknownError(ctx any, format ...string) error

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

type ByteSender interface {
	Send([]byte) error
}

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.

type GinWriter added in v0.1.0

type GinWriter interface {
	Header() Header            // Get header setter
	Write([]byte) (int, error) // Write response body
}

GinWriter represents Gin's response writer interface.

type Header interface {
	Set(string, string) // Set header value
}

Header provides common interface for setting HTTP headers.

Jump to

Keyboard shortcuts

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