Documentation
¶
Overview ¶
Package nerror provides utility functions and structures for handling structured JSON error responses in API applications.
It defines error response formats commonly used in APIs, including simple error messages and nested error structures. The package also offers helper functions for serializing error messages into JSON format.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ErrInvalidParameter ¶
ErrInvalidParameter returns an error indicating a missing or invalid parameter.
This function helps standardize parameter validation errors by formatting a descriptive message with the provided parameter name.
Example usage:
err := ErrInvalidParameter("user_id")
fmt.Println(err) // Output: "missing parameter user_id"
@param message The name of the missing or invalid parameter. @return Formatted error message.
func SimpleErrorResponseWithCodeV2 ¶
SimpleErrorResponseWithCodeV2 creates a JSON-formatted error response using the SimpleErrorMessageV2 structure.
It returns a JSON response in the format:
{
"error": {
"status": 502,
"message": "Bad gateway."
}
}
@param status HTTP status code. @param err The error message. @return JSON-encoded error response as []byte and any marshaling error.
func SimpleErrorResponseWithStatus ¶
SimpleErrorResponseWithStatus creates a JSON-formatted error response using the SimpleErrorMessage structure.
It returns a JSON response in the format:
{
"message": "Your JSON is wrong or something",
"status": 500
}
@param status HTTP status code. @param err The error message. @return JSON-encoded error response as []byte and any marshaling error.
func WrapError ¶
WrapError wraps an existing error with additional context, preserving the original error.
This function is useful for adding meaningful context to errors before propagating them, aiding in debugging and error analysis.
Example usage:
err := errors.New("connection timeout")
wrappedErr := WrapError(err, "failed to fetch data")
fmt.Println(wrappedErr) // Output: "failed to fetch data: connection timeout"
@param err The original error to wrap. @param message Additional context message to include. @return Wrapped error message.
Types ¶
type SimpleErrorMessage ¶
SimpleErrorMessage represents a basic JSON error response format.
Example JSON output:
{
"message": "Your JSON is wrong or something",
"status": 500
}
type SimpleErrorMessageV2 ¶
type SimpleErrorMessageV2 struct {
ErrorST SimpleErrorMessage `json:"error"`
}
SimpleErrorMessageV2 represents a more structured JSON error response format, encapsulating the error message within an "error" object.
Example JSON output:
{
"error": {
"status": 502,
"message": "Bad gateway."
}
}