Skip to content

codemodus/relay

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

9 Commits

Repository files navigation

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

funcCodedFns(handler...func(error)) (CodedCheckFunc, CodedTripFunc) funcDefaultHandler() func(error) funcFns(handler...func(error)) (CheckFunc, TripFunc) funcHandle() typeCheckFunctypeCodedCheckFunctypeCodedErrorfunc (ce*CodedError) Error() stringfunc (ce*CodedError) ExitCode() inttypeCodedTripFuncfuncCodedTripFn(ckCodedCheckFunc) CodedTripFunctypeExitCodertypeRelayfuncNew(handler...func(error)) *Relayfunc (r*Relay) Check(errerror) func (r*Relay) CodedCheck(codeint, errerror) typeTripFuncfuncTripFn(ckCheckFunc) TripFunc

Setup

import ( "github.com/codemodus/relay" ) funcmain(){r:=relay.New() deferrelay.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(errerror){fmt.Println(err) fmt.Println("extra message") } r:=relay.New(h) deferrelay.Handle() err:=fail() r.Check(err) fmt.Println("should not print") // Output:// always fails// extra message

Setup (Eased Usage)

ck:=relay.New().Checkdeferrelay.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().CodedCheckdeferrelay.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().Checktrip:=relay.TripFn(ck) deferrelay.Handle() n:=three() ifn!=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() deferrelay.Handle() err:=mightFail() ck(err) n:=three() ifn!=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

golang/go#32437 (comment)

Documentation

View the GoDoc

Benchmarks

N/A

About

Try it out.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages