Skip to content

Commit 24132cb

Browse files
author
Dean Karn
authored
cm fix error wraping & unwrapping of stderrors (#22)
1 parent 2ff27f9 commit 24132cb

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [5.3.3] - 2023-08-16
10+
### Fixed
11+
- First error inconsistently wrapped with error and prefix instead of err then prefix in the chain.
12+
- Corrected Is & As functions to directly causal error.
13+
914
## [5.3.2] - 2023-08-16
1015
### Fixed
1116
- Link Error output missing error when prefix was blank.
@@ -41,7 +46,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4146
- Updated deps.
4247

4348

44-
[Unreleased]: https://github.com/go-playground/errors/compare/v5.3.2...HEAD
49+
[Unreleased]: https://github.com/go-playground/errors/compare/v5.3.3...HEAD
50+
[5.3.3]: https://github.com/go-playground/errors/compare/v5.3.2...v5.3.3
4551
[5.3.2]: https://github.com/go-playground/errors/compare/v5.3.1...v5.3.2
4652
[5.3.1]: https://github.com/go-playground/errors/compare/v5.3.0...v5.3.1
4753
[5.3.0]: https://github.com/go-playground/errors/compare/v5.2.3...v5.3.0

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package errors
22
============
3-
![Project status](https://img.shields.io/badge/version-5.3.2-green.svg)
3+
![Project status](https://img.shields.io/badge/version-5.3.3-green.svg)
44
[![Build Status](https://travis-ci.org/go-playground/errors.svg?branch=master)](https://travis-ci.org/go-playground/errors)
55
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/errors)](https://goreportcard.com/report/github.com/go-playground/errors)
66
[![GoDoc](https://godoc.org/github.com/go-playground/errors?status.svg)](https://pkg.go.dev/github.com/go-playground/errors/v5)

chain.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ func newLink(err error, prefix string, skipFrames int) *Link {
3434
Prefix: prefix,
3535
Source: runtimeext.StackLevel(skipFrames),
3636
}
37-
3837
}
3938

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

215214
// Is reports whether any error in error chain matches target.
216215
func (c Chain) Is(target error) bool {
217-
return stderrors.Is(c[len(c)-1].Err, target)
216+
if len(c) == 0 {
217+
return false
218+
}
219+
if innerErr, ok := target.(Chain); ok {
220+
if len(innerErr) == 0 {
221+
return false
222+
}
223+
target = innerErr[0].Err
224+
}
225+
return stderrors.Is(c[0].Err, target)
218226
}
219227

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

240251
func defaultFormatFn(c Chain) string {

errors.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package errors
22

33
import (
4-
"errors"
54
stderrors "errors"
65
"fmt"
76
"reflect"
@@ -38,7 +37,7 @@ func RegisterErrorFormatFn(fn ErrorFormatFn) {
3837

3938
// New creates an error with the provided text and automatically wraps it with line information.
4039
func New(s string) Chain {
41-
return wrap(errors.New(s), "", 3)
40+
return wrap(stderrors.New(s), "", 3)
4241
}
4342

4443
// Newf creates an error with the provided text and automatically wraps it with line information.
@@ -74,12 +73,15 @@ func wrap(err error, prefix string, skipFrames int) (c Chain) {
7473
if c, ok = err.(Chain); ok {
7574
c = append(c, newLink(nil, prefix, skipFrames))
7675
} else {
77-
c = Chain{newLink(err, prefix, skipFrames)}
76+
c = Chain{newLink(err, "", skipFrames)}
7877
for _, h := range helpers {
7978
if !h(c, err) {
8079
break
8180
}
8281
}
82+
if prefix != "" {
83+
c = append(c, &Link{Prefix: prefix, Source: c[0].Source})
84+
}
8385
}
8486
return
8587
}

0 commit comments

Comments
 (0)