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 ¶
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 ¶
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 ¶
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 ¶
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) IsInternal ¶
IsInternal returns true if the error is internal.
type FieldError ¶
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