Skip to content

Commit b728fd6

Browse files
authored
fix: ignore context.Canceled errors in Sentry reporting (#4909)
* fix: ignore `context.Canceled` errors in Sentry reporting * fix lint and add changelog * fix missing comment * rollback comments changes
1 parent 748172e commit b728fd6

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Fixes
6+
7+
- [#4909](https://github.com/ignite/cli/pull/4909) Ignore `context.Canceled` errors in Sentry reporting.
8+
59
## [`v29.9.2`](https://github.com/ignite/cli/releases/tag/v29.9.2)
610

711
- [#4904](https://github.com/ignite/cli/pull/4904) Add variadic options in `modulecreate.AddModuleToAppConfig`.

ignite/pkg/errors/xerrors.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
package errors
1414

1515
import (
16+
"context"
17+
1618
"github.com/cockroachdb/errors"
1719
"github.com/getsentry/sentry-go"
1820
)
@@ -21,30 +23,28 @@ import (
2123
// A stack trace is retained.
2224
func New(msg string) error {
2325
err := errors.New(msg)
24-
sentry.CaptureException(err)
26+
captureException(err)
2527
return err
2628
}
2729

2830
// Errorf aliases Newf().
2931
func Errorf(format string, args ...any) error {
3032
err := errors.Errorf(format, args...)
31-
sentry.CaptureException(err)
33+
captureException(err)
3234
return err
3335
}
3436

3537
// WithStack annotates err with a stack trace at the point WithStack was called.
3638
func WithStack(err error) error {
3739
errWithStack := errors.WithStack(err)
38-
sentry.CaptureException(errWithStack)
40+
captureException(errWithStack)
3941
return errWithStack
4042
}
4143

4244
// Wrap wraps an error with a message prefix. A stack trace is retained.
4345
func Wrap(err error, msg string) error {
4446
errWrap := errors.Wrap(err, msg)
45-
if err != nil {
46-
sentry.CaptureException(errWrap)
47-
}
47+
captureException(errWrap)
4848
return errWrap
4949
}
5050

@@ -53,12 +53,20 @@ func Wrap(err error, msg string) error {
5353
// but the extra arguments are still processed for reportable strings.
5454
func Wrapf(err error, format string, args ...any) error {
5555
errWrap := errors.Wrapf(err, format, args...)
56-
if err != nil {
57-
sentry.CaptureException(errWrap)
58-
}
56+
captureException(errWrap)
5957
return errWrap
6058
}
6159

60+
func captureException(err error) {
61+
if shouldCaptureException(err) {
62+
sentry.CaptureException(err)
63+
}
64+
}
65+
66+
func shouldCaptureException(err error) bool {
67+
return err != nil && !Is(err, context.Canceled)
68+
}
69+
6270
// Unwrap accesses the direct cause of the error if any, otherwise
6371
// returns nil.
6472
func Unwrap(err error) error { return errors.Unwrap(err) }

ignite/pkg/errors/xerrors_test.go

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

33
import (
4+
"context"
45
stdErrors "errors"
56
"testing"
67

@@ -55,3 +56,16 @@ func TestWithStack(t *testing.T) {
5556
base := stdErrors.New("base")
5657
require.True(t, Is(WithStack(base), base))
5758
}
59+
60+
func TestShouldCaptureException(t *testing.T) {
61+
t.Run("canceled errors are ignored", func(t *testing.T) {
62+
require.False(t, shouldCaptureException(context.Canceled))
63+
require.False(t, shouldCaptureException(Wrap(context.Canceled, "prefix")))
64+
require.False(t, shouldCaptureException(Wrapf(context.Canceled, "prefix %s", "x")))
65+
require.False(t, shouldCaptureException(WithStack(context.Canceled)))
66+
})
67+
68+
t.Run("other errors are reported", func(t *testing.T) {
69+
require.True(t, shouldCaptureException(stdErrors.New("boom")))
70+
})
71+
}

ignite/templates/module/create/app_config_ast.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func SkipConfigEntry() AddModuleAppConfigOption {
2626
}
2727

2828
// SpecifyModuleEntry allows to define to which field the module should be added in the app config.
29-
// E.g. "PreBlockers", "InitGenesis", "BeginBlockers", "EndBlockers"
29+
// E.g. "PreBlockers", "InitGenesis", "BeginBlockers", "EndBlockers".
3030
func SpecifyModuleEntry(fields ...string) AddModuleAppConfigOption {
3131
return func(opts *addModuleAppConfigOptions) {
3232
opts.runtimeFields = fields

0 commit comments

Comments
 (0)