Skip to content

Commit ce006a7

Browse files
alfadbclaude
andcommitted
fix(ops): validate error_type against known whitelist before classification
Upstream proxies (account 4, 112) return `"<nil>"` as the error.type in their JSON responses — a Go fmt.Sprintf("%v", nil) artifact. Since `normalizeOpsErrorType` only checked for empty string, the literal "<nil>" passed through and poisoned the entire classification chain: error_phase was misclassified as "internal" (instead of "request"), severity was inflated to P2, and the stored error_type was meaningless. Add `isKnownOpsErrorType` whitelist so any unrecognised type falls through to the code-based or default "api_error" classification. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9d79506 commit ce006a7

2 files changed

Lines changed: 79 additions & 1 deletion

File tree

backend/internal/handler/ops_error_logger.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,8 +939,29 @@ func guessPlatformFromPath(path string) string {
939939
}
940940
}
941941

942+
// isKnownOpsErrorType returns true if t is a recognized error type used by the
943+
// ops classification pipeline. Upstream proxies sometimes return garbage values
944+
// (e.g. the Go-serialized literal "<nil>") which would pollute phase/severity
945+
// classification if accepted blindly.
946+
func isKnownOpsErrorType(t string) bool {
947+
switch t {
948+
case "invalid_request_error",
949+
"authentication_error",
950+
"rate_limit_error",
951+
"billing_error",
952+
"subscription_error",
953+
"upstream_error",
954+
"overloaded_error",
955+
"api_error",
956+
"not_found_error",
957+
"forbidden_error":
958+
return true
959+
}
960+
return false
961+
}
962+
942963
func normalizeOpsErrorType(errType string, code string) string {
943-
if errType != "" {
964+
if errType != "" && isKnownOpsErrorType(errType) {
944965
return errType
945966
}
946967
switch strings.TrimSpace(code) {

backend/internal/handler/ops_error_logger_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,60 @@ func TestOpsErrorLoggerMiddleware_DoesNotBreakOuterMiddlewares(t *testing.T) {
214214
})
215215
require.Equal(t, http.StatusNoContent, rec.Code)
216216
}
217+
218+
func TestIsKnownOpsErrorType(t *testing.T) {
219+
known := []string{
220+
"invalid_request_error",
221+
"authentication_error",
222+
"rate_limit_error",
223+
"billing_error",
224+
"subscription_error",
225+
"upstream_error",
226+
"overloaded_error",
227+
"api_error",
228+
"not_found_error",
229+
"forbidden_error",
230+
}
231+
for _, k := range known {
232+
require.True(t, isKnownOpsErrorType(k), "expected known: %s", k)
233+
}
234+
235+
unknown := []string{"<nil>", "null", "", "random_error", "some_new_type", "<nil>\u003e"}
236+
for _, u := range unknown {
237+
require.False(t, isKnownOpsErrorType(u), "expected unknown: %q", u)
238+
}
239+
}
240+
241+
func TestNormalizeOpsErrorType(t *testing.T) {
242+
tests := []struct {
243+
name string
244+
errType string
245+
code string
246+
want string
247+
}{
248+
// Known types pass through.
249+
{"known invalid_request_error", "invalid_request_error", "", "invalid_request_error"},
250+
{"known rate_limit_error", "rate_limit_error", "", "rate_limit_error"},
251+
{"known upstream_error", "upstream_error", "", "upstream_error"},
252+
253+
// Unknown/garbage types are rejected and fall through to code-based or default.
254+
{"nil literal from upstream", "<nil>", "", "api_error"},
255+
{"null string", "null", "", "api_error"},
256+
{"random string", "something_weird", "", "api_error"},
257+
258+
// Unknown type but known code still maps correctly.
259+
{"nil with INSUFFICIENT_BALANCE code", "<nil>", "INSUFFICIENT_BALANCE", "billing_error"},
260+
{"nil with USAGE_LIMIT_EXCEEDED code", "<nil>", "USAGE_LIMIT_EXCEEDED", "subscription_error"},
261+
262+
// Empty type falls through to code-based mapping.
263+
{"empty type with balance code", "", "INSUFFICIENT_BALANCE", "billing_error"},
264+
{"empty type with subscription code", "", "SUBSCRIPTION_NOT_FOUND", "subscription_error"},
265+
{"empty type no code", "", "", "api_error"},
266+
}
267+
for _, tt := range tests {
268+
t.Run(tt.name, func(t *testing.T) {
269+
got := normalizeOpsErrorType(tt.errType, tt.code)
270+
require.Equal(t, tt.want, got)
271+
})
272+
}
273+
}

0 commit comments

Comments
 (0)