Documentation
¶
Overview ¶
Package resp provides standardized HTTP response helpers for building consistent JSON, XML, and text responses in web applications.
This package simplifies response handling by providing:
- Success and failure response builders
- Common HTTP error responses (404, 400, 500, etc.)
- Multiple content type support (JSON, XML, Text)
- Business error code integration
- Consistent response structure
Response Structure ¶
All responses follow a standard structure:
{
"status": 200, // HTTP status code
"code": 0, // Business error code (0 = success)
"message": "ok", // Human-readable message
"data": {...}, // Response payload (on success)
"errors": {...} // Error details (on failure)
}
Success Responses ¶
// Simple success with data resp.Success(w, userData) // Success with custom status code resp.WithStatusCode(w, http.StatusCreated, newResource) // Success with message only resp.Success(w, "Operation completed")
Error Responses ¶
// Pre-defined error responses
resp.NotFound(w, "User not found")
resp.BadRequest(w, "Invalid input", validationErrors)
resp.Unauthorized(w, "Authentication required")
resp.ServerError(w, "Internal error occurred")
// Custom error response
resp.Fail(w, &resp.Exception{
Status: http.StatusConflict,
Code: 1001,
Message: "Resource already exists",
Errors: conflictDetails,
})
Content Types ¶
The package supports JSON (default), XML, and plain text responses. Content type is automatically set based on the response format.
Error Codes ¶
Business error codes are defined in the ecode package and provide standardized error classification across the application.
Index ¶
- func Fail(w http.ResponseWriter, r *Exception, abort ...bool)
- func Success(w http.ResponseWriter, data ...any)
- func WithStatusCode(w http.ResponseWriter, statusCode int, data ...any)
- type Exception
- func AlreadyExists(message string, data ...any) *Exception
- func BadRequest(message string, data ...any) *Exception
- func Conflict(message string, data ...any) *Exception
- func DBQuery(message string, data ...any) *Exception
- func Forbidden(message string, data ...any) *Exception
- func Gone(message string, data ...any) *Exception
- func InternalServer(message string, data ...any) *Exception
- func NotAllowed(message string, data ...any) *Exception
- func NotExists(message string, data ...any) *Exception
- func NotFound(message string, data ...any) *Exception
- func ServiceUnavailable(message string, data ...any) *Exception
- func Transactions(message string, data ...any) *Exception
- func UnAuthorized(message string, data ...any) *Exception
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Fail ¶
func Fail(w http.ResponseWriter, r *Exception, abort ...bool)
Fail handles failure responses.
func WithStatusCode ¶
func WithStatusCode(w http.ResponseWriter, statusCode int, data ...any)
WithStatusCode handles success responses with custom status code.
Types ¶
type Exception ¶
type Exception struct {
Status int `json:"status,omitempty"` // HTTP status
Code int `json:"code,omitempty"` // Business code
Message string `json:"message,omitempty"` // Message
Errors any `json:"errors,omitempty"` // Validation errors
Data any `json:"data,omitempty"` // Response data
}
Exception represents the response structure.
func AlreadyExists ¶
AlreadyExists indicates that the resource already exists.
func BadRequest ¶
BadRequest indicates a bad request.
func InternalServer ¶
InternalServer indicates a server error.
func NotAllowed ¶
NotAllowed indicates a not allowed error.
func ServiceUnavailable ¶
ServiceUnavailable indicates a service is unavailable.
func Transactions ¶
Transactions indicates a transaction processing failure.
func UnAuthorized ¶
UnAuthorized indicates that the request is unauthorized.