Skip to content

Commit

Permalink
cm fix error wraping & unwrapping of stderrors (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
deankarn committed Aug 17, 2023
1 parent 2ff27f9 commit 24132cb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [5.3.3] - 2023-08-16
### Fixed
- First error inconsistently wrapped with error and prefix instead of err then prefix in the chain.
- Corrected Is & As functions to directly causal error.

## [5.3.2] - 2023-08-16
### Fixed
- Link Error output missing error when prefix was blank.
Expand Down Expand Up @@ -41,7 +46,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updated deps.


[Unreleased]: https://github.com/go-playground/errors/compare/v5.3.2...HEAD
[Unreleased]: https://github.com/go-playground/errors/compare/v5.3.3...HEAD
[5.3.3]: https://github.com/go-playground/errors/compare/v5.3.2...v5.3.3
[5.3.2]: https://github.com/go-playground/errors/compare/v5.3.1...v5.3.2
[5.3.1]: https://github.com/go-playground/errors/compare/v5.3.0...v5.3.1
[5.3.0]: https://github.com/go-playground/errors/compare/v5.2.3...v5.3.0
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package errors
============
![Project status](https://img.shields.io/badge/version-5.3.2-green.svg)
![Project status](https://img.shields.io/badge/version-5.3.3-green.svg)
[![Build Status](https://travis-ci.org/go-playground/errors.svg?branch=master)](https://travis-ci.org/go-playground/errors)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/errors)](https://goreportcard.com/report/github.com/go-playground/errors)
[![GoDoc](https://godoc.org/github.com/go-playground/errors?status.svg)](https://pkg.go.dev/github.com/go-playground/errors/v5)
Expand Down
17 changes: 14 additions & 3 deletions chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func newLink(err error, prefix string, skipFrames int) *Link {
Prefix: prefix,
Source: runtimeext.StackLevel(skipFrames),
}

}

// Chain contains the chained errors, the links, of the chains if you will
Expand Down Expand Up @@ -214,7 +213,16 @@ func (c Chain) Unwrap() error {

// Is reports whether any error in error chain matches target.
func (c Chain) Is(target error) bool {
return stderrors.Is(c[len(c)-1].Err, target)
if len(c) == 0 {
return false
}
if innerErr, ok := target.(Chain); ok {
if len(innerErr) == 0 {
return false
}
target = innerErr[0].Err
}
return stderrors.Is(c[0].Err, target)
}

// As finds the first error in the error chain that matches target, and if so, sets
Expand All @@ -234,7 +242,10 @@ func (c Chain) Is(target error) bool {
// As panics if target is not a non-nil pointer to either a type that implements
// error, or to any interface type.
func (c Chain) As(target any) bool {
return stderrors.As(c[len(c)-1].Err, target)
if len(c) == 0 {
return false
}
return stderrors.As(c[0].Err, target)
}

func defaultFormatFn(c Chain) string {
Expand Down
8 changes: 5 additions & 3 deletions errors.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package errors

import (
"errors"
stderrors "errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -38,7 +37,7 @@ func RegisterErrorFormatFn(fn ErrorFormatFn) {

// New creates an error with the provided text and automatically wraps it with line information.
func New(s string) Chain {
return wrap(errors.New(s), "", 3)
return wrap(stderrors.New(s), "", 3)
}

// Newf creates an error with the provided text and automatically wraps it with line information.
Expand Down Expand Up @@ -74,12 +73,15 @@ func wrap(err error, prefix string, skipFrames int) (c Chain) {
if c, ok = err.(Chain); ok {
c = append(c, newLink(nil, prefix, skipFrames))
} else {
c = Chain{newLink(err, prefix, skipFrames)}
c = Chain{newLink(err, "", skipFrames)}
for _, h := range helpers {
if !h(c, err) {
break
}
}
if prefix != "" {
c = append(c, &Link{Prefix: prefix, Source: c[0].Source})
}
}
return
}
Expand Down

0 comments on commit 24132cb

Please sign in to comment.