🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

github.com/codemodus/relay

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/codemodus/relay

v0.0.0-20200814173335-acf94137159e
Version published
Created

relay

go get github.com/codemodus/relay

Package relay provides a simple mechanism for relaying control flow based upon whether a checked error is nil or not.

Usage

func CodedFns(handler ...func(error)) (CodedCheckFunc, CodedTripFunc)
func DefaultHandler() func(error)
func Fns(handler ...func(error)) (CheckFunc, TripFunc)
func Handle()
type CheckFunc
type CodedCheckFunc
type CodedError
    func (ce *CodedError) Error() string
    func (ce *CodedError) ExitCode() int
type CodedTripFunc
    func CodedTripFn(ck CodedCheckFunc) CodedTripFunc
type ExitCoder
type Relay
    func New(handler ...func(error)) *Relay
    func (r *Relay) Check(err error)
    func (r *Relay) CodedCheck(code int, err error)
type TripFunc
    func TripFn(ck CheckFunc) TripFunc

Setup

import (
    "github.com/codemodus/relay"
)

func main() {
    r := relay.New()
    defer relay.Handle()

    err := fail()
    r.Check(err)

    // prints "{cmd_name}: {err_msg}" to stderr
    // calls os.Exit with code set as 1
}

Setup (Custom Handler)

    h := func(err error) {
        fmt.Println(err)
        fmt.Println("extra message")
    }

    r := relay.New(h)
    defer relay.Handle()

    err := fail()
    r.Check(err)

    fmt.Println("should not print")

    // Output:
    // always fails
    // extra message

Setup (Eased Usage)

    ck := relay.New().Check
    defer relay.Handle()

    err := fail()
    ck(err)

    // prints "{cmd_name}: {err_msg}" to stderr
    // calls os.Exit with code set as 1

Setup (Coded Check)

    ck := relay.New().CodedCheck
    defer relay.Handle()

    err := fail()
    ck(3, err)

    // prints "{cmd_name}: {err_msg}" to stderr
    // calls os.Exit with code set as first arg to r.CodedCheck ("ck")

Setup (Trip Function)

    ck := relay.New().Check
    trip := relay.TripFn(ck)
    defer relay.Handle()

    n := three()
    if n != 2 {
        trip("must receive %v: %v is invalid", 2, n)
    }

    fmt.Println("should not print")

    // prints "{cmd_name}: {trip_msg}" to stderr
    // calls os.Exit with code set as 1

Setup (Eased Usage - Check and Trip)

    ck, trip := relay.Fns()
    defer relay.Handle()

    err := mightFail()
    ck(err)

    n := three()
    if n != 2 {
        trip("must receive %v: %v is invalid", 2, n)
    }

    fmt.Println("should not print")

    // prints "{cmd_name}: {trip_msg}" to stderr
    // calls os.Exit with code set as 1

More Info

Background

https://github.com/golang/go/issues/32437#issuecomment-510214015

Documentation

View the GoDoc

Benchmarks

N/A

FAQs

Package last updated on 14 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts