{"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 { type = DEFAULT_TYPE, ...requestOptions } = options ?? {};\n\n    super(\n      {\n        method: \"GET\",\n        query: { query, libraryName },\n        ...requestOptions,\n      },\n      \"v2/libs/search\"\n    );\n\n    this.responseType = type;\n  }\n\n  public override async exec(client: Requester): Promise<Library[] | string> {\n    const result = await this.requestResult<ApiSearchResponse>(client);","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/upstash/context7/blob/4416fb855b8f752be735e34f943b5d0762701aad/packages/sdk/src/commands/search-library/index.ts#L1-L33","documentation":"SearchLibraryCommand's constructor requires both a query and a libraryName string and throws Context7Error immediately if either is falsy. This is fail-fast input validation so malformed searches never reach the network.","triggerScenarios":"Calling the search-library command with an empty string, undefined, or null for query or libraryName — e.g. searchLibrary('', 'react'), searchLibrary('hooks', undefined), or values read from unset variables.","commonSituations":"Building the query/libraryName from CLI args or form input without checking for empty values; destructuring options where a field is optional and missing; template strings left empty after interpolation; automation scripts with blank config entries.","solutions":["Pass non-empty strings for both arguments, e.g. searchLibrary('react hooks', 'react')","Validate inputs before constructing the command and surface a user-friendly message","If one parameter is genuinely optional in your flow, supply a sensible default instead of an empty string","Check where the values originate (argv, env, form) and guard against empty/undefined at the source"],"exampleFix":"// before\nawait client.searchLibrary(query, name); // query or name may be ''\n// after\nif (!query?.trim() || !name?.trim()) throw new Error('query and libraryName are required');\nawait client.searchLibrary(query.trim(), name.trim());","handlingStrategy":"validation","validationCode":"function assertSearchArgs(query, libraryName) {\n  if (typeof query !== 'string' || !query.trim() || typeof libraryName !== 'string' || !libraryName.trim()) {\n    throw new Error('query and libraryName must be non-empty strings');\n  }\n}","typeGuard":"function isNonEmptyString(v: unknown): v is string {\n  return typeof v === 'string' && v.trim().length > 0;\n}","tryCatchPattern":"try {\n  await client.searchLibrary(query, libraryName);\n} catch (e) {\n  if (e instanceof Context7Error && e.message.includes('query and libraryName are required')) {\n    console.error('Both --query and library name must be provided');\n  } else { throw e; }\n}","preventionTips":["Validate CLI args/form input for non-empty strings before building commands","Provide defaults for optional inputs instead of passing empty strings","Use argument parsers (yargs/commander) with required-option enforcement","Sanitize with .trim() since whitespace-only strings are still falsy after trimming"],"tags":["validation","arguments","search","api"],"backgroundTag":"missing-required-argument","analyzedSha":"4416fb855b8f752be735e34f943b5d0762701aad","analyzedAt":"2026-09-16T20:28:07.148Z","contentChangedAt":"2026-09-16T20:28:07.148Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}