Documentation
¶
Overview ¶
Package errlay wraps errors with layer and code metadata and supports field filters through errors.Is and metadata extraction through errors.As.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
New creates a layered error wrapping err. A nil cause creates a leaf error.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/cohesian-tools/typy/errlay"
)
func main() {
cause := errors.New("connection refused")
err := errlay.New("database", 10, "query failed", cause)
fmt.Println(err)
fmt.Println(errors.Is(err, cause))
}
Output: |layer database - 10| query failed connection refused true
func Target ¶
Target returns a field filter for errors.Is. Nil fields are wildcards; two nil fields match any *Error. Both specified fields must match the same error in the chain. Target retains the supplied pointers.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/cohesian-tools/typy/errlay"
"github.com/cohesian-tools/typy/ptr"
)
func main() {
err := errlay.New("service", 1, "request failed",
errlay.New("database", 10, "query failed", nil),
)
target := errlay.Target(ptr.New("database"), ptr.New(uint8(10)))
fmt.Println(errors.Is(err, target))
fmt.Println(errors.Is(err, errlay.Target(nil, nil)))
fmt.Println(errors.Is(errors.New("ordinary error"), errlay.Target(nil, nil)))
}
Output: true true false
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error associates a layer, code, and message with an optional underlying cause.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/cohesian-tools/typy/errlay"
)
func main() {
err := fmt.Errorf("context: %w", errlay.New("database", 10, "query failed", nil))
var detail *errlay.Error
if errors.As(err, &detail) {
fmt.Println(detail.Layer(), detail.Code(), detail.Message())
}
}
Output: database 10 query failed
func (*Error) Is ¶
Is Chain
Is reports whether this error matches a filter constructed by Target. Use errors.Is to search the full error chain.