Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ServerError = New(500, "ServerError",
"There was an issue on the server side. Please report to us or try again later.")
Package xerr provides custom error handling with HTTP status codes.
Basic Usage ¶
err := xerr.New(404, "NotFound", "User not found") fmt.Println(err.StatusCode()) // 404
Error Checking ¶
if xerr.Is(err, "NotFound") {
// handle not found
}
if xerr.IsClientError(err) {
// handle 4xx errors
}
Error Wrapping ¶
err := xerr.Newf(500, "DatabaseError", "failed to connect: %w", dbErr)
if errors.Is(err, dbErr) {
// database connection failed
}
ServerError always the same
Functions ¶
func IsClientError ¶
func IsServerError ¶ added in v0.0.2
IsServerError check if error is a 5xx server error
Types ¶
type Error ¶
type Error struct {
Key string `json:"error"`
Message string `json:"message"`
// contains filtered or unexported fields
}
Error custom struct
func ParseResp ¶
ParseResp can parse http response, if there is a error, it would read and close the body for error messages.
Click to show internal directories.
Click to hide internal directories.