multierr

package module
v0.0.0-...-0ef9c39 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2018 License: MIT Imports: 2 Imported by: 0

README

multierr

GoDoc Build Status

Package multierr introduce a simple way to join multiple errors as an error.

Installation

Run go get github.com/getogrand/multierr.

Usage

The multierr.Join() combine multiple errors to an error.

You can report multiple errors as an joined error to the caller using multierr.Join().

import "github.com/getogrand/multierr"

var ee []error

up := ReservationUserPush{}
n := ReservationAlarmNoti{}

if err := up.Send(); err != nil {
  ee = append(ee,
    fmt.Errorf("send reservation %v push to user: %v", n.Reservation.ID, err))
}

if err := n.sendShopAlarm(); err != nil {
  ee = append(ee, fmt.Errorf(
    "send shop alarm message of reservation %v: %v", n.Reservation.ID, err))
}

if err := n.sendUserAlarm(); err != nil {
  ee = append(ee, fmt.Errorf(
    "send user alarm message of reservation %v: %v", n.Reservation.ID, err))
}

return multierr.Join(ee) // error{"3 errors occured: send reservation 1 push to user: connection fail, send shop alarm message of reservation 1: connection fail, send user alarm message of reservation 1: connection fail"}

It is really useful when you run errorable operation in for-loop.

import "github.com/getogrand/multierr"

func SliceConvAtoi32(aa []string) ([]int32, error) {
	errs := []error{}
	ii := []int32{}
	for _, a := range aa {
		i, err := strconv.Atoi(a)
		if err != nil {
			errs = append(errs, fmt.Errorf("convert %q to int: %v", a, err))
			continue
		}
		ii = append(ii, int32(i))
	}

	if len(errs) > 0 {
		return []int32{}, multierr.Join(errs)
	}
	return ii, nil
}

Extracted From heybeauty

License

Released under the MIT License

Documentation

Overview

Package multierr introduce a simple way to join multiple errors as an error.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Join

func Join(errs []error) error

Join combine multiple errors to an error.

Error that joined have error string follow below format.

`2 errors occured: open file abc.png, close connection of db 'test_db'`

Types

This section is empty.

Jump to

Keyboard shortcuts

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