{"record":{"id":"d4127ed5724862fd","repo":"Tencent/WeKnora","slug":"failed-to-marshal-metaso-request-w","errorCode":null,"errorMessage":"failed to marshal Metaso request: %w","messagePattern":"failed to marshal Metaso request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/infrastructure/web_search/metaso.go","lineNumber":91,"sourceCode":"\nfunc (p *MetasoProvider) Search(ctx context.Context, query string, maxResults int, includeDate bool) ([]*types.WebSearchResult, error) {\n\tquery = strings.TrimSpace(query)\n\tif query == \"\" {\n\t\treturn nil, fmt.Errorf(\"query is empty\")\n\t}\n\tif maxResults <= 0 {\n\t\tmaxResults = defaultMetasoResults\n\t}\n\tif maxResults > maxMetasoResults {\n\t\tmaxResults = maxMetasoResults\n\t}\n\n\tbody, err := json.Marshal(metasoSearchRequest{\n\t\tQuery: query, Scope: p.scope, Size: maxResults,\n\t\tIncludeSummary: true, IncludeRawContent: false, ConciseSnippet: true,\n\t})\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to marshal Metaso request: %w\", err)\n\t}\n\treq, err := http.NewRequestWithContext(ctx, http.MethodPost, p.baseURL, bytes.NewReader(body))\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to create Metaso request: %w\", err)\n\t}\n\treq.Header.Set(\"Authorization\", \"Bearer \"+p.apiKey)\n\treq.Header.Set(\"Accept\", \"application/json\")\n\treq.Header.Set(\"Content-Type\", \"application/json\")\n\n\tlogger.Infof(ctx, \"[WebSearch][Metaso] query=%q maxResults=%d scope=%s\", query, maxResults, p.scope)\n\tresp, err := p.client.Do(req)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to execute Metaso request: %w\", err)\n\t}\n\tdefer resp.Body.Close()\n\trespBody, err := readMetasoResponseBody(resp.Body)\n\tif err != nil {\n\t\treturn nil, err","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/infrastructure/web_search/metaso.go#L73-L109","documentation":"Returned by MetasoProvider.Search when json.Marshal of the metasoSearchRequest struct fails before any HTTP call is made. With a fixed struct of string/int/bool fields this should practically never happen (encoding/json only errors on unsupported types like channels or cyclic values), so encountering it indicates corrupted or non-serializable data injected into the request struct. The marshal error is wrapped via %w.","triggerScenarios":"Search (internal/infrastructure/web_search/metaso.go:91) calls json.Marshal(metasoSearchRequest{...}) and it returns an error — realistically only if a field carries an unsupported value, e.g. query containing invalid UTF-8 that a custom MarshalJSON rejects, or the struct being modified to hold non-serializable types.","commonSituations":"Query string containing invalid UTF-8 bytes from binary input; a fork/customization that added a func or chan field to metasoSearchRequest; a custom MarshalJSON implementation returning an error.","solutions":["Inspect the wrapped error (%w) to see which value json.Marshal rejected.","Sanitize the query: strip or replace invalid UTF-8 (e.g. strings.ToValidUTF8(query, \"\\uFFFD\")).","Review any local modifications to metasoSearchRequest for fields json cannot encode (func, chan, cycle).","If using a custom MarshalJSON, fix it to return valid JSON or nil error."],"exampleFix":"// before: pass raw input straight through\nresults, err := provider.Search(ctx, rawInput, 10, false)\n\n// after: ensure valid UTF-8\nclean := strings.ToValidUTF8(strings.TrimSpace(rawInput), \"\\uFFFD\")\nresults, err := provider.Search(ctx, clean, 10, false)","handlingStrategy":"validation","validationCode":"if !utf8.ValidString(query) {\n    query = strings.ToValidUTF8(query, \"\\uFFFD\")\n}\nif query == \"\" {\n    return fmt.Errorf(\"query must be non-empty UTF-8 text\")\n}","typeGuard":null,"tryCatchPattern":"results, err := provider.Search(ctx, query, 10, false)\nif err != nil {\n    var jsonErr *json.UnsupportedTypeError\n    if errors.As(err, &jsonErr) {\n        return nil, fmt.Errorf(\"request serialization bug: %w\", err)\n    }\n    return nil, err\n}","preventionTips":["Sanitize all user input to valid UTF-8 before passing it to Search.","Keep metasoSearchRequest limited to JSON-serializable field types.","Add a unit test marshalling a maximal request struct to catch serialization regressions.","Avoid custom MarshalJSON on request structs unless covered by tests."],"tags":["json","marshal","serialization","web-search"],"backgroundTag":"json-marshal-failed","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}