{"record":{"id":"59b825f92ee696ed","repo":"upstash/context7","slug":"query-and-libraryname-are-required","errorCode":null,"errorMessage":"query and libraryName are required","messagePattern":"query and libraryName are required","errorType":"validation","errorClass":"Context7Error","httpStatus":null,"severity":"error","filePath":"packages/sdk/src/commands/search-library/index.ts","lineNumber":15,"sourceCode":"import { Command } from \"@commands/command\";\nimport type { Library, SearchLibraryOptions } from \"@commands/types\";\nimport type { ApiSearchResponse } from \"./types\";\nimport type { Requester } from \"@http\";\nimport { Context7Error } from \"@error\";\nimport { formatLibrary, formatLibrariesAsText } from \"@utils/format\";\n\nconst DEFAULT_TYPE = \"json\";\n\nexport class SearchLibraryCommand extends Command<Library[] | string> {\n  private readonly responseType: \"json\" | \"txt\";\n\n  constructor(query: string, libraryName: string, options?: SearchLibraryOptions) {\n    if (!query || !libraryName) {\n      throw new Context7Error(\"query and libraryName are required\");\n    }\n\n    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    });","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/upstash/context7/blob/ca15df0443ee770506fc4eb270d1efc71d483933/packages/sdk/src/commands/search-library/index.ts#L1-L33","documentation":"Thrown by the SearchLibraryCommand constructor when query or libraryName is falsy (empty string, null, undefined). This is input validation at construction time, before any network call: the SDK refuses to build a command that could not produce a valid v2/libs/search request.","triggerScenarios":"Calling `new SearchLibraryCommand('', 'react')`, `new SearchLibraryCommand('react', '')`, omitting an argument, or passing null/undefined for either required parameter.","commonSituations":"Dynamic code building a search from user input that was empty; destructuring mismatch that left a variable undefined; form submitted with a blank field whose value flowed straight into the constructor.","solutions":["Validate query and libraryName are non-empty strings before constructing the command.","Default to a sane value or surface a form error to the user instead of calling the SDK.","Trim whitespace and reject blank strings explicitly.","Add a type guard so empty/null inputs are caught at the boundary."],"exampleFix":"// before\nconst cmd = new SearchLibraryCommand(query, libName); // throws if either is ''\n\n// after\nif (!query?.trim() || !libName?.trim()) {\n  throw new Error(\"query and libraryName must be non-empty strings\");\n}\nconst cmd = new SearchLibraryCommand(query.trim(), libName.trim(), { type: \"json\" });","handlingStrategy":"validation","validationCode":"function requireSearchParams(query: unknown, libraryName: unknown): [string, string] {\n  if (typeof query !== \"string\" || !query.trim()) {\n    throw new Error(\"query must be a non-empty string\");\n  }\n  if (typeof libraryName !== \"string\" || !libraryName.trim()) {\n    throw new Error(\"libraryName must be a non-empty string\");\n  }\n  return [query.trim(), libraryName.trim()];\n}\nconst [q, lib] = requireSearchParams(query, libraryName);\nconst cmd = new SearchLibraryCommand(q, lib, { type: \"json\" });","typeGuard":"function isNonEmptyString(v: unknown): v is string {\n  return typeof v === \"string\" && v.trim().length > 0;\n}","tryCatchPattern":"try {\n  const cmd = new SearchLibraryCommand(query, libraryName);\n} catch (e) {\n  if (e instanceof Context7Error && /query and libraryName are required/.test(e.message)) {\n    // Surface a user-facing form error instead of letting the SDK exception propagate.\n    return showUserError(\"Please enter both a query and a library name.\");\n  }\n  throw e;\n}","preventionTips":["Validate inputs at the form/UI boundary so the SDK constructor never sees empties.","Trim and reject blank strings before constructing commands.","Unit-test the boundary guard separately from SDK integration tests."],"tags":["validation","sdk","search","constructor"],"backgroundTag":null,"analyzedSha":"ca15df0443ee770506fc4eb270d1efc71d483933","analyzedAt":"2026-08-12T13:31:48.440Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}