{"record":{"id":"cc8bf35079ade528","repo":"Tencent/WeKnora","slug":"api-key-is-required-for-metaso-provider","errorCode":null,"errorMessage":"API key is required for Metaso provider","messagePattern":"API key is required for Metaso provider","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/infrastructure/web_search/metaso.go","lineNumber":56,"sourceCode":"}\n\nfunc NewMetasoProvider(params types.WebSearchProviderParameters) (interfaces.WebSearchProvider, error) {\n\tif err := ValidateMetasoParameters(params); err != nil {\n\t\treturn nil, err\n\t}\n\tclient, err := NewSearchHTTPClient(defaultMetasoTimeout, params.ProxyURL)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn &MetasoProvider{\n\t\tclient: client, baseURL: defaultMetasoSearchURL,\n\t\tapiKey: strings.TrimSpace(params.APIKey), scope: metasoScope(params.ExtraConfig),\n\t}, nil\n}\n\nfunc ValidateMetasoParameters(params types.WebSearchProviderParameters) error {\n\tif strings.TrimSpace(params.APIKey) == \"\" {\n\t\treturn fmt.Errorf(\"API key is required for Metaso provider\")\n\t}\n\tscope := metasoScope(params.ExtraConfig)\n\tif _, ok := validMetasoScopes[scope]; !ok {\n\t\treturn fmt.Errorf(\"invalid Metaso search scope: %s\", scope)\n\t}\n\treturn nil\n}\n\nfunc metasoScope(extraConfig map[string]string) string {\n\tif scope := strings.TrimSpace(extraConfig[\"scope\"]); scope != \"\" {\n\t\treturn scope\n\t}\n\treturn defaultMetasoScope\n}\n\nfunc (p *MetasoProvider) Name() string { return \"metaso\" }\n\nfunc (p *MetasoProvider) Search(ctx context.Context, query string, maxResults int, includeDate bool) ([]*types.WebSearchResult, error) {","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/infrastructure/web_search/metaso.go#L38-L74","documentation":"Returned by ValidateMetasoParameters (and therefore by NewMetasoProvider) when the Metaso provider is constructed without a non-empty APIKey. Metaso's API is authenticated exclusively via a Bearer token, so the provider refuses to initialize rather than failing later at request time with a 401. This is a fail-fast configuration validation error.","triggerScenarios":"NewMetasoProvider calls ValidateMetasoParameters; strings.TrimSpace(params.APIKey) == \"\" — i.e. WebSearchProviderParameters.APIKey unset, empty string, or whitespace only.","commonSituations":"METASO_API_KEY (or equivalent env var) not set in the environment; config file missing the api_key field; secret injected as empty string because of a failed secret-manager lookup; trailing whitespace-only key after sanitization.","solutions":["Set the Metaso API key in the provider parameters (APIKey field) before calling NewMetasoProvider.","Verify the environment variable / secret holding the key is actually set and exported where the app runs.","Check that secret injection (CI variables, .env file, secret manager) resolved to a non-empty value.","Trim and check the value yourself before constructing the provider to fail with a clearer app-level message."],"exampleFix":"// before\nprovider, err := NewMetasoProvider(types.WebSearchProviderParameters{})\n\n// after\napiKey := os.Getenv(\"METASO_API_KEY\")\nif strings.TrimSpace(apiKey) == \"\" {\n    return nil, fmt.Errorf(\"METASO_API_KEY not configured\")\n}\nprovider, err := NewMetasoProvider(types.WebSearchProviderParameters{APIKey: apiKey})","handlingStrategy":"validation","validationCode":"func requireAPIKey(params types.WebSearchProviderParameters) error {\n    if strings.TrimSpace(params.APIKey) == \"\" {\n        return fmt.Errorf(\"API key is required for Metaso provider\")\n    }\n    return nil\n}\n\n// call before NewMetasoProvider:\nif err := requireAPIKey(params); err != nil { return err }","typeGuard":null,"tryCatchPattern":"provider, err := NewMetasoProvider(params)\nif err != nil {\n    if strings.Contains(err.Error(), \"API key is required\") {\n        return nil, fmt.Errorf(\"configuration error: set METASO_API_KEY (%w)\", err)\n    }\n    return nil, err\n}","preventionTips":["Fail fast at application startup by constructing all providers during init.","Use secret-manager checks in CI to assert required secrets are present before deploy.","Document required env vars and validate them with a startup config checklist.","Never build provider parameters from values that can silently resolve to empty strings."],"tags":["config","api-key","validation","web-search"],"backgroundTag":"missing-api-key","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}