errs

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2024 License: ISC Imports: 1 Imported by: 0

README

Errors

Go Reference

Golang package to create constant sentinel errors.

Install

go get github.com/nicolasparada/go-errs

Usage

You can create your own constant sentinel errors as if they were a string.

package myapp

import (
    errs "github.com/nicolasparada/go-errs"
)

const (
    ErrInvalidEmail = errs.InvalidArgumentError("myapp: invalid email")
)

You can use errors.Is to check for the error group, which are exposed in the package.

Or you can use errors.As to check if your error is of type errs.Error so you can extract the error kind as well.

package main

import (
    "errors"
    "fmt"

    "myapp"
    errs "github.com/nicolasparada/go-errs"
)

func main() {
    ok := errors.Is(myapp.ErrInvalidEmail, errs.InvalidArgument)
    fmt.Println(ok)
    // Output: true

    var e errs.Error
    ok = errors.As(myapp.ErrInvalidEmail, &e)
    fmt.Println(ok)
    // Output: true

    ok = e.Kind() == errs.KindInvalidArgument
    fmt.Println(ok)
    // Output: true
}

HTTP Errors

You can use the httperrs subpackage to quickly convert an error defined using this package into an HTTP status code.

package main

import (
    "errors"
    "fmt"

    "myapp"
    "github.com/nicolasparada/go-errs/httperrs"
)

func main() {
    got := httperrs.Code(myapp.ErrInvalidEmail)
    fmt.Println(got)
    // Output: 422
}

Documentation

Overview

Package errs exposes a common set of error types. It is designed so these errors can be declared as constants.

Index

Examples

Constants

View Source
const (
	Unauthenticated  = UnauthenticatedError("unauthenticated")
	InvalidArgument  = InvalidArgumentError("invalid argument")
	NotFound         = NotFoundError("not found")
	Conflict         = ConflictError("conflict")
	PermissionDenied = PermissionDeniedError("permission denied")
	Gone             = GoneError("gone")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ConflictError

type ConflictError string

func (ConflictError) Error

func (e ConflictError) Error() string

func (ConflictError) Is

func (e ConflictError) Is(target error) bool

func (ConflictError) Kind added in v0.3.0

func (e ConflictError) Kind() Kind

type Error added in v0.3.0

type Error interface {
	error
	Kind() Kind
}

Error interface represents an error that was built using this very package. Meaning it exposes a Kind() method.

Example
const err = UnauthenticatedError("an unauthenticated error")
fmt.Println(err.Kind() == KindUnauthenticated)
Output:

true

type GoneError

type GoneError string

func (GoneError) Error

func (e GoneError) Error() string

func (GoneError) Is

func (e GoneError) Is(target error) bool

func (GoneError) Kind added in v0.3.0

func (e GoneError) Kind() Kind

type InvalidArgumentError

type InvalidArgumentError string

func (InvalidArgumentError) Error

func (e InvalidArgumentError) Error() string

func (InvalidArgumentError) Is

func (e InvalidArgumentError) Is(target error) bool

func (InvalidArgumentError) Kind added in v0.3.0

func (e InvalidArgumentError) Kind() Kind

type Kind added in v0.3.0

type Kind string

Kind enum.

const (
	KindUnauthenticated  Kind = "unauthenticated"
	KindInvalidArgument  Kind = "invalid_argument"
	KindNotFound         Kind = "not_found"
	KindConflict         Kind = "conflict"
	KindPermissionDenied Kind = "permission_denied"
	KindGone             Kind = "gone"
)

type NotFoundError

type NotFoundError string

func (NotFoundError) Error

func (e NotFoundError) Error() string

func (NotFoundError) Is

func (e NotFoundError) Is(target error) bool

func (NotFoundError) Kind added in v0.3.0

func (e NotFoundError) Kind() Kind

type PermissionDeniedError

type PermissionDeniedError string

func (PermissionDeniedError) Error

func (e PermissionDeniedError) Error() string

func (PermissionDeniedError) Is

func (e PermissionDeniedError) Is(target error) bool

func (PermissionDeniedError) Kind added in v0.3.0

func (e PermissionDeniedError) Kind() Kind

type UnauthenticatedError

type UnauthenticatedError string

func (UnauthenticatedError) Error

func (e UnauthenticatedError) Error() string

func (UnauthenticatedError) Is

func (e UnauthenticatedError) Is(target error) bool

func (UnauthenticatedError) Kind added in v0.3.0

func (e UnauthenticatedError) Kind() Kind

Directories

Path Synopsis
Package httperrs exposes HTTP utilities so you can quickly get an HTTP status code out of an error defined in the errs package.
Package httperrs exposes HTTP utilities so you can quickly get an HTTP status code out of an error defined in the errs package.

Jump to

Keyboard shortcuts

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