errs

package module
v0.0.0-...-f60a0b1 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2023 License: MIT Imports: 2 Imported by: 0

README

errs

Go Report Card Build Status codecov Godoc Release

errs is a package providing application errors for go services.

The package provides error codes that are human readable and map well to transport error codes or status'. It also provides a constructor for new errors and ability to parse the code and message from a go error interface if the packages provided Error type is the underlying implementation. This package also provides some smaller packages for interacting with different transport protocols and translating errors to the expected result in each protocol e.g.(http, gRPC, connect).

This package supports the two latest go versions

Installation

go get github.com/actatum/errs

Usage

Basic
package main

import (
    "fmt"
    
    "github.com/actatum/errs"
)

func main() {
    thingID := "1"
    err := errs.Errorf(errs.NotFound, "no thing with id: %s", thingID)
    fmt.Println(err.Error())
}
HTTP Handler
package handlers

import (
    "log"
    "net/http"

    "github.com/actatum/errs/httperr"
)

type request struct {
    X string `json:"x"`
}

func h(w http.ResponseWriter, r *http.Request) {
    var req request
    if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
        httperr.RenderError(err)
        return
    }

    log.Printf("request: %v\n", req)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Internal         = Code{"internal"}
	NotFound         = Code{"not_found"}
	Invalid          = Code{"invalid"}
	Unauthorized     = Code{"unauthorized"}
	PermissionDenied = Code{"permission_denied"}
	Conflict         = Code{"conflict"}
)

Functions

func ErrorMessage

func ErrorMessage(err error) string

ErrorMessage unwraps an Error and returns its message. Non Error types always return 'internal error'.

Types

type Code

type Code struct {
	// contains filtered or unexported fields
}

Code represents different types of errors that can arise in applications.

func CodeFromString

func CodeFromString(s string) Code

CodeFromString parses a Code from the given string. If no code exists for the string 'Internal' is returned.

func ErrorCode

func ErrorCode(err error) Code

ErrorCode unwraps an Error and returns its code. Non Error types always return 'Internal'.

func (Code) String

func (c Code) String() string

String returns the string representation of the error code.

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error represents an application-specific error. Application errors can be unwrapped by the caller to extract the code and message.

func Errorf

func Errorf(code Code, format string, args ...interface{}) Error

Errorf constructs a new error with the given code and formatted message.

func (Error) Code

func (e Error) Code() Code

Code returns the errors underlying code.

func (Error) Error

func (e Error) Error() string

Error implements the error interface.

func (Error) Message

func (e Error) Message() string

Message returns the errors underlying message.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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