feat(sentry): enhance error handling by skipping user input errors in CaptureError
This commit is contained in:
parent
8205924917
commit
5e4dbc78ed
2 changed files with 22 additions and 3 deletions
|
|
@ -25,8 +25,9 @@ var (
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd = &cobra.Command{
|
rootCmd = &cobra.Command{
|
||||||
Use: "unarr",
|
Use: "unarr",
|
||||||
Short: "unarr — torrent search and management",
|
Version: Version,
|
||||||
|
Short: "unarr — torrent search and management",
|
||||||
Long: `unarr is a powerful terminal tool for torrent search and management.
|
Long: `unarr is a powerful terminal tool for torrent search and management.
|
||||||
|
|
||||||
Search 30+ torrent sources, inspect torrent quality, discover popular content,
|
Search 30+ torrent sources, inspect torrent quality, discover popular content,
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,14 @@
|
||||||
package sentry
|
package sentry
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
gosentry "github.com/getsentry/sentry-go"
|
gosentry "github.com/getsentry/sentry-go"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
// dsn is injected at build time via ldflags. If empty, Sentry is disabled.
|
// dsn is injected at build time via ldflags. If empty, Sentry is disabled.
|
||||||
|
|
@ -45,8 +47,10 @@ func Close() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// CaptureError sends a non-fatal error to Sentry with optional command context.
|
// CaptureError sends a non-fatal error to Sentry with optional command context.
|
||||||
|
// User-input errors (unknown flag/command, bad value) are skipped — they are
|
||||||
|
// not bugs, just noise.
|
||||||
func CaptureError(err error, command string) {
|
func CaptureError(err error, command string) {
|
||||||
if err == nil {
|
if err == nil || isUserInputError(err) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -58,6 +62,20 @@ func CaptureError(err error, command string) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isUserInputError(err error) bool {
|
||||||
|
var notExist *pflag.NotExistError
|
||||||
|
var valueReq *pflag.ValueRequiredError
|
||||||
|
var invalidVal *pflag.InvalidValueError
|
||||||
|
var invalidSyn *pflag.InvalidSyntaxError
|
||||||
|
if errors.As(err, ¬Exist) || errors.As(err, &valueReq) ||
|
||||||
|
errors.As(err, &invalidVal) || errors.As(err, &invalidSyn) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
msg := err.Error()
|
||||||
|
return strings.HasPrefix(msg, "unknown command ") ||
|
||||||
|
strings.HasPrefix(msg, "required flag(s)")
|
||||||
|
}
|
||||||
|
|
||||||
// RecoverPanic captures a panic and re-panics after reporting.
|
// RecoverPanic captures a panic and re-panics after reporting.
|
||||||
// Usage: defer sentry.RecoverPanic()
|
// Usage: defer sentry.RecoverPanic()
|
||||||
func RecoverPanic() {
|
func RecoverPanic() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue