xerr

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: MIT Imports: 6 Imported by: 4

Documentation

Overview

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
}

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.")

ServerError always the same

Functions

func Is

func Is(err error, key string) bool

Is err the instance of Error,and has <key>?

func IsClientError

func IsClientError(err error) bool

IsClientError check if error is a 4xx client error

func IsCode

func IsCode(err error, code int) bool

IsCode check if the status code is <code>

func IsServerError added in v0.0.2

func IsServerError(err error) bool

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 As

func As(err error) (*Error, bool)

As finds the first *Error in err's tree.

func Join added in v0.0.2

func Join(code int, key string, msg string, errs ...error) *Error

Join wraps errors.Join to create a multi-error xerr

func New

func New(code int, key string, msg string) *Error

New Error

func Newf

func Newf(code int, key string, format string, a ...any) *Error

Newf create an Error use format

func ParseResp

func ParseResp(resp *http.Response) *Error

ParseResp can parse http response, if there is a error, it would read and close the body for error messages.

func (*Error) Error

func (e *Error) Error() string

Error makes it compatible with `error` interface.

func (*Error) StatusCode

func (e *Error) StatusCode() int

StatusCode is http status code

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap support the Unwrap interface

func (*Error) UnwrapAll added in v0.0.2

func (e *Error) UnwrapAll() []error

UnwrapAll returns all wrapped errors

Jump to

Keyboard shortcuts

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