errs

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package errs enables error handling and definition at the http/app level.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsFieldErrors

func IsFieldErrors(err error) bool

IsFieldErrors checks if an error of type FieldErrors exists.

Example
package main

import (
	"fmt"

	"github.com/adamwoolhether/httper/web/errs"
)

func main() {
	err := errs.NewFieldsError("age", fmt.Errorf("must be positive"))

	fmt.Println(errs.IsFieldErrors(err))
	fmt.Println(errs.IsFieldErrors(fmt.Errorf("other error")))
}
Output:
true
false

func NewFieldsError

func NewFieldsError(field string, err error) error

NewFieldsError creates a fields error.

Example
package main

import (
	"fmt"

	"github.com/adamwoolhether/httper/web/errs"
)

func main() {
	err := errs.NewFieldsError("email", fmt.Errorf("invalid format"))

	fmt.Println(err)
}
Output:
[{"field":"email","error":"invalid format"}]

Types

type Error

type Error struct {
	Code     int    `json:"code"`
	Message  string `json:"message"`
	FuncName string `json:"-"`
	FileName string `json:"-"`
	InnerErr bool   `json:"-"`
}

Error represents an error in the system.

func New

func New(code int, err error) *Error

New constructs an error based on an app error.

Example
package main

import (
	"fmt"
	"net/http"

	"github.com/adamwoolhether/httper/web/errs"
)

func main() {
	err := errs.New(http.StatusNotFound, fmt.Errorf("user not found"))

	fmt.Println(err.Code)
	fmt.Println(err.Error())
}
Output:
404
user not found

func NewInternal

func NewInternal(err error) *Error

NewInternal creates an error that is not intended to be seen by users.

Example
package main

import (
	"fmt"

	"github.com/adamwoolhether/httper/web/errs"
)

func main() {
	err := errs.NewInternal(fmt.Errorf("db connection lost"))

	fmt.Println(err.Code)
	fmt.Println(err.IsInternal())
}
Output:
500
true

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface.

func (*Error) IsInternal

func (e *Error) IsInternal() bool

IsInternal returns true if the error is internal.

type FieldError

type FieldError struct {
	Field string `json:"field"`
	Err   string `json:"error"`
}

FieldError is used to indicate an error with a specific request field.

type FieldErrors

type FieldErrors []FieldError

FieldErrors represents a collection of field errors.

func GetFieldErrors

func GetFieldErrors(err error) FieldErrors

GetFieldErrors returns a copy of the FieldErrors pointer.

Example
package main

import (
	"fmt"

	"github.com/adamwoolhether/httper/web/errs"
)

func main() {
	err := errs.NewFieldsError("name", fmt.Errorf("required"))
	wrapped := fmt.Errorf("validation failed: %w", err)

	fe := errs.GetFieldErrors(wrapped)
	fmt.Println(fe.Fields()["name"])
}
Output:
required

func (FieldErrors) Error

func (fe FieldErrors) Error() string

Error implements the error interface.

func (FieldErrors) Fields

func (fe FieldErrors) Fields() map[string]string

Fields returns the fields that failed validation

Example
package main

import (
	"fmt"

	"github.com/adamwoolhether/httper/web/errs"
)

func main() {
	fe := errs.FieldErrors{
		{Field: "name", Err: "required"},
		{Field: "email", Err: "invalid"},
	}

	fields := fe.Fields()
	fmt.Println(fields["name"])
	fmt.Println(fields["email"])
}
Output:
required
invalid

Jump to

Keyboard shortcuts

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