Don't panic in case of an unknown API error code

This commit is contained in:
Tim Hockin
2025-06-14 18:12:36 -07:00
parent 4ca91a0305
commit e68d601344
2 changed files with 14 additions and 3 deletions

View File

@@ -107,8 +107,8 @@ func (e *Error) ErrorBody() string {
s = fmt.Sprintf("%s: %s", e.Type, valstr)
}
default:
// NOTE: This panics if we find a code that truly is not supported.
s = e.Type.String()
internal := InternalError(nil, fmt.Errorf("unhandled error code: %s: please report this", e.Type))
s = internal.ErrorBody()
}
if len(e.Detail) != 0 {
s += fmt.Sprintf(": %s", e.Detail)
@@ -195,7 +195,7 @@ func (t ErrorType) String() string {
case ErrorTypeTypeInvalid:
return "Invalid value"
default:
panic(fmt.Sprintf("unrecognized validation error: %q", string(t)))
return fmt.Sprintf("<unknown error %q>", string(t))
}
}

View File

@@ -630,6 +630,17 @@ func TestErrorFormatting(t *testing.T) {
CoveredByDeclarative: true,
},
expect: `path.to.field: Invalid value: field.SelfMarshalerNonStringer{S:"visible"}: the details`,
}, {
name: "unknown error type",
input: &Error{
Type: "not real",
Field: "path.to.field",
BadValue: SelfMarshalerNonStringer{"visible"},
Detail: "the details",
Origin: "theOrigin",
CoveredByDeclarative: true,
},
expect: `path.to.field: Internal error: unhandled error code: <unknown error "not real">: please report this: the details`,
}}
for _, tc := range cases {