multierror

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2020 License: Apache-2.0 Imports: 4 Imported by: 42

Documentation

Overview

Package multierror can be leveraged as an opinionated to handle multiple errors providing appropriate wrapping for them.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FormatFunc

type FormatFunc func(es []error) string

FormatFunc defines a format function which should format a slice of errors into a string.

type Prefixed

type Prefixed struct {
	Prefix     string
	Errors     []error
	FormatFunc FormatFunc
}

Prefixed is a multierror which will prefix the error output message with the specified prefix.

Example
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package main

import (
	"errors"
	"fmt"
	"io"
	"os"
)

var output io.Writer = os.Stdout

func main() {
	err := NewPrefixed("config validation")
	err = err.Append(errors.New("some validation error"))

	if err.ErrorOrNil() != nil {
		fmt.Fprintln(output, err)
	}
}
Output:

func NewPrefixed

func NewPrefixed(prefix string, errs ...error) *Prefixed

NewPrefixed creates a new pointer to Prefixed w

func (*Prefixed) Append

func (p *Prefixed) Append(errs ...error) *Prefixed

Append appends a number of errors to the current instance of Prefixed. It'll unwrap any wrapped errors in the form of *Prefixed or *multierror.Error.

func (*Prefixed) Error

func (p *Prefixed) Error() string

Error returns the stored slice of error formatted using a set FormatFunc or multierror.ListFormatFunc when no FormatFunc is specified.

func (*Prefixed) ErrorOrNil

func (p *Prefixed) ErrorOrNil() error

ErrorOrNil either returns nil when the type is nil or when there's no Errors. Otherwise, the type is returned.

Jump to

Keyboard shortcuts

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