errhelp

package
Version: v0.0.0-...-0d16973 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2021 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package errhelp contains error helper functions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FirstError

func FirstError(errs ...error) error

FirstError gets the first error in errs that is non-nil, or nil if none exist.

Example

ExampleFirstError is a testable example for FirstError.

package main

import (
	"errors"
	"fmt"

	"github.com/c4-project/c4t/internal/helper/errhelp"
)

func main() {
	fmt.Println("FirstError() == nil:", errhelp.FirstError() == nil)

	fmt.Println("FirstError(x, y) ==", errhelp.FirstError(errors.New("x"), errors.New("y")))

}
Output:

FirstError() == nil: true
FirstError(x, y) == x

func TimeoutOrFirstError

func TimeoutOrFirstError(ctx context.Context, errs ...error) error

TimeoutOrFirstError returns ctx.Err() if it is a deadline-exceeded error, or the first error in errs that is non-nil, if any exist.

Example

ExampleTimeoutFirstError is a testable example for FirstError.

package main

import (
	"context"
	"errors"
	"fmt"

	"github.com/c4-project/c4t/internal/helper/errhelp"
)

func main() {
	ctx := context.Background()
	fmt.Println("TimeoutOrFirstError(ok ctx) ==", errhelp.TimeoutOrFirstError(ctx))
	fmt.Println("TimeoutOrFirstError(ok ctx, x, y) ==", errhelp.TimeoutOrFirstError(ctx, errors.New("x"), errors.New("y")))

	tctx, cf := context.WithTimeout(ctx, 0)
	defer cf()
	fmt.Println("TimeoutOrFirstError(t/o ctx) ==", errhelp.TimeoutOrFirstError(tctx))
	fmt.Println("TimeoutOrFirstError(t/o ctx, x, y) ==", errhelp.TimeoutOrFirstError(tctx, errors.New("x"), errors.New("y")))

}
Output:

TimeoutOrFirstError(ok ctx) == <nil>
TimeoutOrFirstError(ok ctx, x, y) == x
TimeoutOrFirstError(t/o ctx) == context deadline exceeded
TimeoutOrFirstError(t/o ctx, x, y) == context deadline exceeded

Types

This section is empty.

Source Files

Jump to

Keyboard shortcuts

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