errors

package module
v2.0.3+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2018 License: Apache-2.0 Imports: 3 Imported by: 0

README

go-errors

GitHub release GitHub issues GitHub issues closed Travis branch Coverage Status Go Report Card license

This library contains simple error handling primitives.


Getting started / Prerequisites / Dependencies

go get -u github.com/amsokol/go-errors

See usage example:

package errors

import (
    "fmt"
)

func Function(val1 int, val2 string, val3 bool) (string, error) {
    var err error

    // check input params
    Check(&err, val1 > 0, "val1 must be greater than 0")
    Check(&err, len(val2) > 0, "val2 can't be empty")
    Check(&err, val3, "val3 should be 'true'")
    if err != nil {
        return "", err
    }

    return fmt.Sprintf("val1=%d, val2='%s', val3=%t", val1, val2, val3), nil
}

func Example() {
    err := Error("error message")
    fmt.Println(err)
    // Output: error message

    err = Errorf("error message: %s", "reason")
    fmt.Println(err)
    // Output: error message: reason

    err = Wrap(err, "new error message")
    fmt.Println(err)
    // Output: new error message-> error message: reason

    err = Wrapf(err, "one more error message: %s", "another reason")
    fmt.Println(err)
    // Output: "one more error message: another reason-> new error message-> error message: reason

    // val1 is invalid
    _, err = Function(0, "text", true)
    if err != nil {
        fmt.Println(err)
        // Output: al1 must be greater than 0
    }

    // val2 is invalid
    _, err = Function(10, "", true)
    if err != nil {
        fmt.Println(err)
        // Output: val2 can't be empty
    }

    // val3 is invalid
    _, err = Function(10, "text", false)
    if err != nil {
        fmt.Println(err)
        // Output: val3 should be 'true'
    }

    // all values are valid
    res, err := Function(10, "text", true)
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println(res)
}
Testing
go test ./...

Documentation

Overview

Package errors provides primitives for simple error handling primitives.

Example
package main

import (
	"fmt"
)

func Function(val1 int, val2 string, val3 bool) (string, error) {
	var err error

	// check input params
	Check(&err, val1 > 0, "val1 must be greater than 0")
	Check(&err, len(val2) > 0, "val2 can't be empty")
	Check(&err, val3, "val3 should be 'true'")
	if err != nil {
		return "", err
	}

	return fmt.Sprintf("val1=%d, val2='%s', val3=%t", val1, val2, val3), nil
}

func main() {
	err := Error("error message")
	fmt.Println(err)
	// Output: error message

	err = Errorf("error message: %s", "reason")
	fmt.Println(err)
	// Output: error message: reason

	err = Wrap(err, "new error message")
	fmt.Println(err)
	// Output: new error message-> error message: reason

	err = Wrapf(err, "one more error message: %s", "another reason")
	fmt.Println(err)
	// Output: "one more error message: another reason-> new error message-> error message: reason

	// val1 is invalid
	_, err = Function(0, "text", true)
	if err != nil {
		fmt.Println(err)
		// Output: al1 must be greater than 0
	}

	// val2 is invalid
	_, err = Function(10, "", true)
	if err != nil {
		fmt.Println(err)
		// Output: val2 can't be empty
	}

	// val3 is invalid
	_, err = Function(10, "text", false)
	if err != nil {
		fmt.Println(err)
		
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Check

func Check(err *error, cond bool, text string)

Check function raises error if condition is false. If *err is not nil (means error has raised early) function does nothing and returns immediately. Otherwise if case of cond is false functions create Error and puts to *err.

Example
// get fake invalid value
r := rand.New(rand.NewSource(15))
val := r.Intn(99)

// check value
var err error
Check(&err, val > 100, "rand value must be more than 100")
fmt.Println(err)
Output:

rand value must be more than 100

func Checkf

func Checkf(err *error, cond bool, format string, a ...interface{})

Checkf function raises error if condition is false. If *err is not nil (means error has raised early) function does nothing and returns immediately. Otherwise if case of cond is false functions create Error (format with parameters) and puts to *err.

Example
// get fake invalid value
r := rand.New(rand.NewSource(15))
val := r.Intn(99)

// check value
var err error
Checkf(&err, val > 100, "rand value must be more than %d", 100)
fmt.Println(err)
Output:

rand value must be more than 100

func Error

func Error(text string) error

Error returns an error that formats as the given text.

Example
err := Error("error message")
fmt.Println(err)
Output:

error message

func Errorf

func Errorf(format string, a ...interface{}) error

Errorf formats according to a format specifier and returns the string as a value that satisfies error.

Example
err := Errorf("error message: %s", "reason")
fmt.Println(err)
Output:

error message: reason

func Wrap

func Wrap(err error, text string) error

Wrap wraps error and returns the string as a value that satisfies error.

Example
err := Error("error message 1")
err = Wrap(err, "error message 2")
fmt.Println(err)
Output:

error message 2-> error message 1

func Wrapf

func Wrapf(err error, format string, a ...interface{}) error

Wrapf wraps error, formats according to a format specifier and returns the string as a value that satisfies error.

Example
err := Error("error message 1")
err = Wrapf(err, "error message 2: %s", "reason")
fmt.Println(err)
Output:

error message 2: reason-> error message 1

Types

This section is empty.

Jump to

Keyboard shortcuts

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