Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion bot/command/context/replyable.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"net/http"
"runtime"
"strings"
"time"

Expand All @@ -28,6 +29,7 @@ import (
"github.com/TicketsBot-cloud/worker/bot/utils"
"github.com/TicketsBot-cloud/worker/config"
"github.com/TicketsBot-cloud/worker/i18n"
"go.uber.org/zap"
)

type Replyable struct {
Expand Down Expand Up @@ -134,7 +136,21 @@ func (r *Replyable) HandleError(err error) {
fmt.Printf("ctx.HandleError: %s\n", err.Error())
}

eventId := logging.ErrorWithContext(err, r.ctx.ToErrorContext())
var eventId string
if isExpectedDiscordError(err) {
// No Sentry stack trace on this path, so record which handler raised it.
var restError request.RestError
errors.As(err, &restError)
_, file, line, _ := runtime.Caller(1)

eventId = logging.LocalOnly(err, r.ctx.ToErrorContext(),
zap.Int("discord_code", restError.ApiError.Code),
zap.Int("status_code", restError.StatusCode),
zap.String("call_site", fmt.Sprintf("%s:%d", file, line)),
)
} else {
eventId = logging.ErrorWithContext(err, r.ctx.ToErrorContext())
}

if errors.Is(err, ErrReplyLimitReached) {
return
Expand All @@ -151,6 +167,27 @@ func (r *Replyable) HandleError(err error) {
_, _ = r.ctx.ReplyWith(res)
}

// isExpectedDiscordError reports guild state/config errors that buildErrorResponse
// already answers with a friendly message and no event ID, so Sentry gains nothing.
// Kept out on purpose: 10062 and 40060 (slow or double ack, our fault), 429s, and
// 50035, whose fallback branch shows the event ID.
func isExpectedDiscordError(err error) bool {
var restError request.RestError
if !errors.As(err, &restError) {
return false
}

switch restError.ApiError.Code {
case 10003, 10004, 10007, 10008, 10011, 10013, 10059,
30007, 30013,
50001, 50013,
160005, 160006, 160007:
return true
default:
return false
}
}

func (r *Replyable) HandleWarning(err error) {
eventId := logging.WarnWithContext(err, r.ctx.ToErrorContext())

Expand Down
68 changes: 68 additions & 0 deletions bot/command/context/replyable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package context

import (
"context"
"errors"
"fmt"
"net/http"
"testing"

"github.com/TicketsBot-cloud/gdl/rest/request"
"github.com/stretchr/testify/require"
)

func TestIsExpectedDiscordError(t *testing.T) {
restErr := func(status, code int) error {
return request.RestError{
StatusCode: status,
ApiError: request.ApiV8Error{Code: code},
}
}

type testCase struct {
name string
err error
expected bool
}

tests := []testCase{
{name: "nil error", err: nil, expected: false},
{name: "plain error", err: errors.New("boom"), expected: false},
{name: "deadline exceeded", err: context.DeadlineExceeded, expected: false},
{name: "unknown interaction 10062", err: restErr(http.StatusNotFound, 10062), expected: false},
{name: "already acknowledged 40060", err: restErr(http.StatusBadRequest, 40060), expected: false},
{name: "invalid form body 50035", err: restErr(http.StatusBadRequest, 50035), expected: false},
{name: "rate limited with no code", err: restErr(http.StatusTooManyRequests, 0), expected: false},
{name: "unclassified code 50083", err: restErr(http.StatusBadRequest, 50083), expected: false},
{
name: "wrapped expected error",
err: fmt.Errorf("creating channel: %w", restErr(http.StatusForbidden, 50001)),
expected: true,
},
{
name: "wrapped excluded error",
err: fmt.Errorf("replying: %w", restErr(http.StatusNotFound, 10062)),
expected: false,
},
}

expectedCodes := []int{
10003, 10004, 10007, 10008, 10011, 10013, 10059,
30007, 30013,
50001, 50013,
160005, 160006, 160007,
}
for _, code := range expectedCodes {
tests = append(tests, testCase{
name: fmt.Sprintf("expected code %d", code),
err: restErr(http.StatusBadRequest, code),
expected: true,
})
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.expected, isExpectedDiscordError(tt.err))
})
}
}
5 changes: 5 additions & 0 deletions bot/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ func WarnWithContext(err error, ctx sentry.ErrorContext, fields ...zap.Field) st
return logLocally(err, ctx, sentry.LogWithContext(err, ctx), fields)
}

// LocalOnly skips Sentry entirely; the returned ID is a local-<uuid> one.
func LocalOnly(err error, ctx sentry.ErrorContext, fields ...zap.Field) string {
return logLocally(err, ctx, "", fields)
}

// logLocally always logs at zap Warn, deliberately never Error: common/observability's
// ZapSentryAdapter hook (wired in cmd/worker/main.go) forwards every Error-level zap
// line into its own independent, separately-sampled sentry.CaptureEvent call whose
Expand Down
Loading