Skip to content

Commit f45b4ac

Browse files
committed
Refactor Wrap to accept only error, update tests, changelog, and imports
1 parent 6c35c60 commit f45b4ac

4 files changed

Lines changed: 27 additions & 33 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

88

9-
## [Unreleased](https://github.com/gravitton/errors/compare/v1.1.0...master)
9+
## [Unreleased](https://github.com/gravitton/errors/compare/v1.1.1...master)
10+
11+
## v1.1.1 (2026-04-23)(https://github.com/gravitton/errors/compare/v1.01.0...v1.1.1)
12+
### Changed
13+
- `Wrap` now accepts only `error` instead of `any`
1014

1115
## v1.1.0 (2026-04-22)(https://github.com/gravitton/errors/compare/v1.0.0...v1.1.0)
1216
### Added

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ func Process() error {
4242
package main
4343

4444
import (
45-
"github.com/gravitton/errors"
4645
"sync"
46+
47+
"github.com/gravitton/errors"
4748
)
4849

4950
func Process() error {

error.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,24 @@ func Newf(format string, v ...any) *DataError {
3535
}
3636
}
3737

38-
// Wrap converts any value into a *DataError and captures the current stack
38+
// Wrap converts an error into a *DataError and captures the current stack
3939
// trace. If err is nil, Wrap returns nil. If err is already a *DataError it is
40-
// returned unchanged. If err implements error it is wrapped directly; otherwise
41-
// its string representation is used as the error message.
42-
func Wrap(err any) *DataError {
40+
// returned unchanged. Otherwise the error is wrapped directly.
41+
//
42+
// Warning: the returned *DataError nil is a typed nil pointer. When assigned to
43+
// or returned as an error interface it will not equal nil. Prefer checking the
44+
// error before passing it to Wrap rather than checking the result afterwards.
45+
func Wrap(err error) *DataError {
4346
if err == nil {
4447
return nil
4548
}
4649

47-
var e error
48-
switch t := err.(type) {
49-
case *DataError:
50-
return t
51-
case error:
52-
e = t
53-
default:
54-
e = fmt.Errorf("%v", err)
50+
if dataErr, ok := err.(*DataError); ok {
51+
return dataErr
5552
}
5653

5754
return &DataError{
58-
err: e,
55+
err: err,
5956
stack: callers(1),
6057
}
6158
}

error_test.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,20 @@ func TestNewf(t *testing.T) {
2828
assert.Equal(t, reflect.TypeOf(cause).String(), "*errors.errorString")
2929
}
3030

31-
func TestWrapNil(t *testing.T) {
32-
err := Wrap(nil)
33-
34-
assert.NoError(t, err)
31+
func testMethod(err error) error {
32+
return Wrap(err)
3533
}
3634

37-
func TestWrapNonError(t *testing.T) {
38-
err := Wrap("something went wrong")
39-
40-
assert.Equal(t, err.Error(), "something went wrong")
41-
42-
cause := err.Unwrap()
43-
assert.Equal(t, reflect.TypeOf(cause).String(), "*errors.errorString")
44-
}
35+
func TestWrapNil(t *testing.T) {
36+
err1 := Wrap(nil)
4537

46-
func TestWrapNonErrorNonString(t *testing.T) {
47-
err := Wrap(159.5)
38+
assert.NoError(t, err1)
39+
assert.True(t, err1 == nil)
4840

49-
assert.Equal(t, err.Error(), "159.5")
41+
err2 := testMethod(nil)
5042

51-
cause := err.Unwrap()
52-
assert.Equal(t, reflect.TypeOf(cause).String(), "*errors.errorString")
43+
assert.NoError(t, err2)
44+
assert.False(t, err2 == nil) // typed *DataError(nil) nil pointer
5345
}
5446

5547
func TestWrapError(t *testing.T) {
@@ -58,7 +50,7 @@ func TestWrapError(t *testing.T) {
5850

5951
assert.Equal(t, err.Error(), "original")
6052

61-
cause := err.Unwrap()
53+
cause := errors.Unwrap(err)
6254
assert.Equal(t, cause, original)
6355
}
6456

0 commit comments

Comments
 (0)