ensure

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2020 License: Apache-2.0 Imports: 3 Imported by: 14

README

Ensure for Go

A simple ensure package for Golang

Use case

Writing a Go code makes a lot of repetition, regarding error checking. That's especially true for end user code like e2e tests when we expect that something will work. In other cases that's a bug and should code should panic then.

Usage

Instead of writing:

func DeployAllComponents() error {
  alpha, err := deployAlpha()
  if err != nil {
    return errors.WithMessage(err, "unexpected error")
  }
  beta, err := deployBeta(alpha)
  if err != nil {
    return errors.WithMessage(err, "unexpected error")
  }
  _, err := deployGamma(beta)
  if err != nil {
    return errors.WithMessage(err, "unexpected error")
  }
  return nil
}

// execution isn't simple
err = DeployAllComponents()
if err != nil {
  panic(err)
}

with this PR I can write it like:

func DeployAllComponents() {
  alpha, err := deployAlpha()
  ensure.NoError(err)
  beta, err := deployBeta(alpha)
  ensure.NoError(err)
  _, err := deployGamma(beta)
  ensure.NoError(err)
}

// execution is simple
DeployAllComponents()

Above is much more readable and pleasant to see.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(err error)

Error will panic if given no error, as it expected one

func ErrorWithMessage

func ErrorWithMessage(err error, messageRegexp string)

ErrorWithMessage will panic if given no error, or error message don't match provided regexp

func NoError

func NoError(err error)

NoError will panic if given an error, as it was unexpected

Types

This section is empty.

Jump to

Keyboard shortcuts

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