{"record":{"id":"17cb3b7d078ba7e2","repo":"temporalio/temporal","slug":"panic-err","errorCode":null,"errorMessage":"panic(err)","messagePattern":"panic\\(err\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/searchattribute/sadefs/encode_value.go","lineNumber":31,"sourceCode":"\n// EncodeValue encodes search attribute value and IndexedValueType to Payload.\nfunc EncodeValue(val any, t enumspb.IndexedValueType) (*commonpb.Payload, error) {\n\tvalPayload, err := payload.Encode(val)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tSetMetadataType(valPayload, t)\n\treturn valPayload, nil\n}\n\n// MustEncodeValue encodes search attribute value and IndexedValueType to Payload.\n// Panics if it fails to encode.\nfunc MustEncodeValue(val any, t enumspb.IndexedValueType) *commonpb.Payload {\n\tvalPayload, err := EncodeValue(val, t)\n\tif err != nil {\n\t\t// nolint:forbidigo\n\t\tpanic(err)\n\t}\n\treturn valPayload\n}\n\n// DecodeValue decodes search attribute value from Payload using (in order):\n// 1. passed type t.\n// 2. type from MetadataType field, if t is not specified.\n// allowList allows list of values when it's not keyword list type.\nfunc DecodeValue(\n\tvalue *commonpb.Payload,\n\tt enumspb.IndexedValueType,\n\tallowList bool,\n) (any, error) {\n\tif t == enumspb.INDEXED_VALUE_TYPE_UNSPECIFIED {\n\t\tt = GetMetadataType(value)\n\t}\n\tif t == enumspb.INDEXED_VALUE_TYPE_UNSPECIFIED {\n\t\treturn nil, fmt.Errorf(\"%w: %v\", ErrInvalidType, t)","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/temporalio/temporal/blob/bde624efd13fbd3843654058db6d9c716166318b/common/searchattribute/sadefs/encode_value.go#L13-L49","documentation":"MustEncodeValue encodes a search attribute value into a Payload and is the 'must' variant: it panics instead of returning an error. Any encoding failure (unsupported value type, value not matching the declared IndexedValueType) is raised as a panic.","triggerScenarios":"Calling MustEncodeValue with a Go value whose type does not match the given enumspb.IndexedValueType (e.g. MustEncodeValue(\"abc\", enumspb.INDEXED_VALUE_TYPE_INT)), or with a nil/unsupported type the encoder cannot serialize.","commonSituations":"Populating search attributes from untyped user data; schema/type changes between versions where an attribute's declared type changed; reflecting on struct fields whose runtime type differs from the declared attribute type.","solutions":["Pre-validate the value type matches the IndexedValueType before calling (or use non-panicking EncodeValue and handle err)","Use EncodeValue instead of MustEncodeValue where the value's type is not statically known","Fix the search attribute type mapping so the declared type matches the actual value"],"exampleFix":"// before\npayload := sadefs.MustEncodeValue(val, enumspb.INDEXED_VALUE_TYPE_INT) // panics if val is string\n// after\npayload, err := sadefs.EncodeValue(val, enumspb.INDEXED_VALUE_TYPE_INT)\nif err != nil {\n    return fmt.Errorf(\"encode search attribute: %w\", err)\n}","handlingStrategy":"validation","validationCode":"switch val.(type) {\ncase string, int, bool, float64, time.Time, []string:\n    // plausibly encodable\ndefault:\n    return fmt.Errorf(\"unsupported search attribute value %T\", val)\n}\npayload, err := sadefs.EncodeValue(val, saType)\nif err != nil { return err }","typeGuard":null,"tryCatchPattern":"func() (p *commonpb.Payload) {\n    defer func() {\n        if recover() != nil { p = nil }\n    }()\n    return sadefs.MustEncodeValue(val, saType)\n}()","preventionTips":["Prefer EncodeValue + explicit error handling over Must variants for untrusted input","Keep search attribute type maps in sync with actual Go value types","Validate user-supplied search attributes before encoding"],"tags":["go","panic","search-attributes","encoding"],"backgroundTag":"search-attribute-type-mismatch","analyzedSha":"bde624efd13fbd3843654058db6d9c716166318b","analyzedAt":"2026-09-01T07:18:39.080Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}