errors

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithErrorHandler

func WithErrorHandler[T, U any](p rivo.Pipeable[T, U], errHandler rivo.Sync[struct{}]) rivo.Pipeable[T, U]

WithErrorHandler returns a pipeable that connects the input pipeable to an error handling pipeable. The items that don't contain errors are passed to the output stream, while the items that contain errors are passed to the error handling pipeable.

Example
package main

import (
	"context"
	"fmt"
	"strconv"

	"github.com/agiac/rivo"
	"github.com/agiac/rivo/errors"
)

func main() {
	ctx := context.Background()

	g := rivo.Of("1", "2", "3_", "4", "5**")

	toInt := rivo.Map(func(ctx context.Context, i rivo.Item[string]) (int, error) {
		return strconv.Atoi(i.Val)
	})

	logValue := rivo.Do[int](func(ctx context.Context, i rivo.Item[int]) {
		fmt.Printf("Value: %d\n", i.Val)
	})

	errs := make([]error, 0)
	errorHandler := rivo.Do[struct{}](func(ctx context.Context, i rivo.Item[struct{}]) {
		errs = append(errs, i.Err)
	})

	<-rivo.Pipe3(g, errors.WithErrorHandler(toInt, errorHandler), logValue)(ctx, nil)

	for _, err := range errs {
		fmt.Printf("Error: %v\n", err)
	}

}
Output:

Value: 1
Value: 2
Value: 4
Error: strconv.Atoi: parsing "3_": invalid syntax
Error: strconv.Atoi: parsing "5**": invalid syntax

Types

This section is empty.

Jump to

Keyboard shortcuts

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