An open API service indexing awesome lists of open source software.

https://github.com/zgiber/batcherror

Handle individual errors in batch operations where signature doesn't accommodate linking errors to results.
https://github.com/zgiber/batcherror

Last synced: 5 months ago
JSON representation

Handle individual errors in batch operations where signature doesn't accommodate linking errors to results.

Awesome Lists containing this project

README

          

# batcherror

Keep track of individual errors within a batched process.

## Why?

Sometimes a batch operation is required to implement an existing interface that has a singular error return type.
This package can help with conveying and consulting individual errors in the returned error.
It works well with the errors package by the standard library.

## Why not?

Often it is simpler to return a custom type from a batch process, something like:

```go

type batchResult struct {
outputData any
err error
}

func myBatchFunc(inputData []any) []batchResult {
// ...
}
```

## Usage

```go
package main

import (
"errors"
"fmt"

"github.com/zgiber/batcherror"
)

func main() {
items := []any{1, 2, 3, "foo", 4, "bar", 5, 6}
if err := processItems(items); err != nil {
fmt.Println(err)
// not an integer at [3]
// not an integer at [5]

fmt.Println(batcherror.At(err, 1) == nil)
// true

fmt.Println(batcherror.At(err, 3))
// not an integer at [3]

for k, v := range batcherror.Map(err) {
fmt.Println(k, v)
// 3 not an integer at [3]
// 5 not an integer at [5]
}
}
}

func processItems(items []any) error {
var batchErr error
for idx, item := range items {
if _, ok := item.(int); !ok {
batchErr = errors.Join(batchErr, batcherror.New(fmt.Errorf("not an integer"), idx))
}
}
return batchErr
}
```