Skip to content

Commit

Permalink
add oops.New
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Feb 3, 2025
1 parent a04cb8b commit d55824c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ GoDoc: [https://godoc.org/github.com/samber/oops](https://godoc.org/github.com/s

| Constructor | Description |
| ----------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| `.New(message string) error` | Formats returns `oops.OopsError` object that satisfies `error` |
| `.Errorf(format string, args ...any) error` | Formats an error and returns `oops.OopsError` object that satisfies `error` |
| `.Wrap(err error) error` | Wraps an error into an `oops.OopsError` object that satisfies `error` |
| `.Wrapf(err error, format string, args ...any) error` | Wraps an error into an `oops.OopsError` object that satisfies `error` and formats an error message |
Expand Down Expand Up @@ -231,6 +232,18 @@ err2 := oops.
oops.Assertf(time.Now().Weekday() == 1, "This code should run on Monday only.")
// ...
})

// oops.New
err3 := oops.
In("repository").
Tags("database", "sql").
New("an error message")

// oops.Errorf
err3 := oops.
In("repository").
Tags("database", "sql").
Errorf("an error message: %d", 42)
```

### Context
Expand Down Expand Up @@ -269,7 +282,7 @@ err0 := oops.
Errorf("sql: bad connection")

// simple error with stacktrace
err1 := oops.Errorf("could not fetch user")
err1 := oops.New("could not fetch user")

// with optional domain
err2 := oops.
Expand Down
11 changes: 11 additions & 0 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ func (o OopsErrorBuilder) Wrapf(err error, format string, args ...any) error {
return OopsError(o2)
}

// New returns `oops.OopsError` object that satisfies `error`.
func (o OopsErrorBuilder) New(message string) error {
o2 := o.copy()
o2.err = errors.New(message)
if o2.span == "" {
o2.span = ulid.Make().String()
}
o2.stacktrace = newStacktrace(o2.span)
return OopsError(o2)
}

// Errorf formats an error and returns `oops.OopsError` object that satisfies `error`.
func (o OopsErrorBuilder) Errorf(format string, args ...any) error {
o2 := o.copy()
Expand Down
5 changes: 5 additions & 0 deletions oops.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func Wrapf(err error, format string, args ...any) error {
return new().Wrapf(err, format, args...)
}

// New returns `oops.OopsError` object that satisfies `error`.
func New(message string) error {
return new().New(message)
}

// Errorf formats an error and returns `oops.OopsError` object that satisfies `error`.
func Errorf(format string, args ...any) error {
return new().Errorf(format, args...)
Expand Down
11 changes: 11 additions & 0 deletions oops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package oops
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
Expand Down Expand Up @@ -86,6 +87,16 @@ func TestOopsFromContext(t *testing.T) {
is.Equal(val, err.(OopsError).context[key])
}

func TestOopsNew(t *testing.T) {
is := assert.New(t)

err := new().New("a message")
is.Error(err)
is.Equal(errors.New("a message"), err.(OopsError).err)
is.Empty(err.(OopsError).msg)
is.Equal("a message", err.Error())
}

func TestOopsErrorf(t *testing.T) {
is := assert.New(t)

Expand Down

0 comments on commit d55824c

Please sign in to comment.