gobail

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

README

GoBail

Exit immediately your app when you see an error.

GitHub release (latest SemVer) GitHub Workflow Status report card godoc

GitHub watchers GitHub stars Twitter URL

Sometimes when your app encounters an error, it can't be recovered from and the only option is to exit immediately. It doesn't always make sense to pass the error all the way back to the your error handling, just so you can print a string to the console - you just want to exit now. This library makes it easy to do exactly that using a fluent API. I've also made sure that the library is fully tested and validated so that you don't have to. It may sound like a lot of effort but it's important to make sure that there are no nasty suprises - and have evidence that you made sure.

Using this library allows you to safely exit with a non-zero code or just panic when you need to, without without having to worry about complex test code to raise your code coverage metrics.

Using gobail
package main

import "github.com/dnnrly/gobail"

var err = errors.New("an error")

func returnError() error                        { return err }
func returnValAndError() (int, error)           { return 0, err }
func return2ValsAndError() (int, string, error) { return 0, "str", err }

func main() {
    // Take a look at the package documentation for more details
    gobail.Run(returnError()).OrExit()
    result := gobail.Return(returnValAndError()).OrPanic()
    result1, result2 := gobail.Return(return2ValsAndError()).OrExitMsg("something went wrong: %v")
}
Running Unit Tests
$ make test
Running Acceptance tests
$ make acceptance-test

Important make targets

  • test - runs unit tests
  • ci-test - run tests for CI validation
  • acceptance-test - run the acceptance tests
  • lint - run linting
  • clean - clean project dependencies

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

  • Pascal Dennerly - Original library author - dnnrly

See also the list of contributors who participated in this project.

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Return2Evaluator

type Return2Evaluator[R, S any] struct {
	// contains filtered or unexported fields
}

RunEvaluator handles functions with 2 return values

func Return2

func Return2[R, S any](ret1 R, ret2 S, err error) Return2Evaluator[R, S]

Return2 collects the error and other return values for evaluation in the OrExit or OrPanic methods

func (Return2Evaluator[R, S]) OrExit

func (e Return2Evaluator[R, S]) OrExit() (R, S)

OrExit exits the application without an error message if there is an error, returning the values of the function if there is no error

func (Return2Evaluator[R, S]) OrExitMsg

func (e Return2Evaluator[R, S]) OrExitMsg(msg string) (R, S)

OrExitMsg exits the application with an error message if there is an error, returning the values of the function if there is no error You may use the %v placeholder to include the error in the message

func (Return2Evaluator[R, S]) OrPanic

func (e Return2Evaluator[R, S]) OrPanic() (R, S)

OrPanic panics without an error message if there is an error, returning the values of the function if there is no error

func (Return2Evaluator[R, S]) OrPanicMsg

func (e Return2Evaluator[R, S]) OrPanicMsg(msg string) (R, S)

OrPanicMsg exits the application with an error message if there is an error, returning the values of the function if there is no error You may use the %v placeholder to include the error in the message

type ReturnEvaluator

type ReturnEvaluator[R any] struct {
	// contains filtered or unexported fields
}

RunEvaluator handles functions with 1 return value

func Return

func Return[R any](ret R, err error) ReturnEvaluator[R]

Return collects the error and other return value for evaluation in the OrExit or OrPanic methods

func (ReturnEvaluator[R]) OrExit

func (e ReturnEvaluator[R]) OrExit() R

OrExit exits the application without an error message if there is an error, returning the value of the function if there is no error

func (ReturnEvaluator[R]) OrExitMsg

func (e ReturnEvaluator[R]) OrExitMsg(msg string) R

OrExitMsg exits the application with an error message if there is an error, returning the value of the function if there is no error You may use the %v placeholder to include the error in the message

func (ReturnEvaluator[R]) OrPanic

func (e ReturnEvaluator[R]) OrPanic() R

OrPanic panics without an error message if there is an error, returning the value of the function if there is no error

func (ReturnEvaluator[R]) OrPanicMsg

func (e ReturnEvaluator[R]) OrPanicMsg(msg string) R

OrPanicMsg exits the application with an error message if there is an error, returning the value of the function if there is no error You may use the %v placeholder to include the error in the message

type RunEvaluator

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

RunEvaluator handles functions with no return value

func Run

func Run(err error) RunEvaluator

Run collects the error for evaluation in the OrExit or OrPanic methods

func (RunEvaluator) OrExit

func (e RunEvaluator) OrExit()

OrExit exits the application without an error message if there is an error

func (RunEvaluator) OrExitMsg

func (e RunEvaluator) OrExitMsg(msg string)

OrExitMsg exits the application with an error message if there is an error You may use the %v placeholder to include the error in the message

func (RunEvaluator) OrPanic

func (e RunEvaluator) OrPanic()

OrPanic panics without an error message if there is an error

func (RunEvaluator) OrPanicMsg

func (e RunEvaluator) OrPanicMsg(msg string)

OrPanicMsg exits the application with an error message if there is an error You may use the %v placeholder to include the error in the message

Directories

Path Synopsis
Package test contains test definitions, code, and data.
Package test contains test definitions, code, and data.

Jump to

Keyboard shortcuts

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