{"record":{"id":"d38611759f05da8d","repo":"upstash/context7","slug":"request-did-not-return-a-result-d38611","errorCode":null,"errorMessage":"Request did not return a result","messagePattern":"Request did not return a result","errorType":"exception","errorClass":"Context7Error","httpStatus":null,"severity":"error","filePath":"packages/sdk/src/commands/search-library/index.ts","lineNumber":36,"sourceCode":"    const queryParams: Record<string, string | number | undefined> = {};\n\n    queryParams.query = query;\n    queryParams.libraryName = libraryName;\n\n    super({ method: \"GET\", query: queryParams }, \"v2/libs/search\");\n\n    this.responseType = options?.type ?? DEFAULT_TYPE;\n  }\n\n  public override async exec(client: Requester): Promise<Library[] | string> {\n    const { result } = await client.request<ApiSearchResponse>({\n      method: this.request.method || \"GET\",\n      path: [this.endpoint],\n      query: this.request.query,\n    });\n\n    if (result === undefined) {\n      throw new Context7Error(\"Request did not return a result\");\n    }\n\n    const libraries = result.results.map(formatLibrary);\n\n    if (this.responseType === \"txt\") {\n      return formatLibrariesAsText(libraries);\n    }\n\n    return libraries;\n  }\n}\n","sourceCodeStart":18,"sourceCodeEnd":48,"githubUrl":"https://github.com/upstash/context7/blob/ca15df0443ee770506fc4eb270d1efc71d483933/packages/sdk/src/commands/search-library/index.ts#L18-L48","documentation":"Thrown by SearchLibraryCommand.exec() (the override) when client.request() returns result === undefined. The very next line dereferences result.results.map(...), so without this guard an undefined result would throw a less clear TypeError. Applies specifically to the v2/libs/search endpoint.","triggerScenarios":"GET v2/libs/search returns 2xx with an undefined result (empty body, non-JSON content-type parsed to undefined, or a custom Requester returning { result: undefined }); 204 from a gateway; test mock missing the result field.","commonSituations":"Server/gateway returning an empty 200; proxy stripping the response body; unit test with an incomplete mock; a Response wrapper that resolves to undefined.","solutions":["Reproduce the search call with curl and inspect status, Content-Type, and body.","In tests, make the mock Requester return { result: { results: [...] } }.","Verify the API key and query params produce a real JSON search response.","If the server legitimately returns empty, consider treating it as { results: [] } upstream after confirming the contract."],"exampleFix":"// before\nconst mock = { request: async () => ({}) };\nawait cmd.exec(mock as any); // Context7Error\n\n// after\nconst mock = { request: async () => ({ result: { results: [] } }) };\nawait cmd.exec(mock as any); // -> []","handlingStrategy":"type-guard","validationCode":"import type { ApiSearchResponse } from \"./types\";\nfunction isSearchResponse(v: unknown): v is ApiSearchResponse {\n  return typeof v === \"object\" && v !== null && Array.isArray((v as any).results);\n}\nconst res = await client.request<ApiSearchResponse>({ method: \"GET\", path: [\"v2/libs/search\"], query });\nif (!isSearchResponse(res.result)) {\n  throw new Error(\"v2/libs/search did not return a JSON body with a results array.\");\n}","typeGuard":"function isApiSearchResponse(v: unknown): v is { results: unknown[] } {\n  return typeof v === \"object\" && v !== null && Array.isArray((v as any).results);\n}","tryCatchPattern":"try {\n  const libs = await searchCmd.exec(client);\n} catch (e) {\n  if (e instanceof Context7Error && /did not return a result/i.test(e.message)) {\n    throw new Error(\"Search returned an empty body — confirm the API key and params, then retry.\");\n  }\n  throw e;\n}","preventionTips":["Make mock Requesters return { result: { results: [...] } } in tests.","Validate the parsed body with isApiSearchResponse before relying on .results.","Confirm v2/libs/search returns application/json with a non-empty body."],"tags":["sdk","http","search","type-guard"],"backgroundTag":null,"analyzedSha":"ca15df0443ee770506fc4eb270d1efc71d483933","analyzedAt":"2026-08-12T13:31:48.440Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}