upstash/context7 · error · Context7Error
query and libraryName are required
Error message
query and libraryName are required
What it means
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.
Source
Thrown at packages/sdk/src/commands/search-library/index.ts:15
import { Command } from "@commands/command";
import type { Library, SearchLibraryOptions } from "@commands/types";
import type { ApiSearchResponse } from "./types";
import type { Requester } from "@http";
import { Context7Error } from "@error";
import { formatLibrary, formatLibrariesAsText } from "@utils/format";
const DEFAULT_TYPE = "json";
export class SearchLibraryCommand extends Command<Library[] | string> {
private readonly responseType: "json" | "txt";
constructor(query: string, libraryName: string, options?: SearchLibraryOptions) {
if (!query || !libraryName) {
throw new Context7Error("query and libraryName are required");
}
const queryParams: Record<string, string | number | undefined> = {};
queryParams.query = query;
queryParams.libraryName = libraryName;
super({ method: "GET", query: queryParams }, "v2/libs/search");
this.responseType = options?.type ?? DEFAULT_TYPE;
}
public override async exec(client: Requester): Promise<Library[] | string> {
const { result } = await client.request<ApiSearchResponse>({
method: this.request.method || "GET",
path: [this.endpoint],
query: this.request.query,
});View on GitHub (pinned to ca15df0443)
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.
Example fix
// before
const cmd = new SearchLibraryCommand(query, libName); // throws if either is ''
// after
if (!query?.trim() || !libName?.trim()) {
throw new Error("query and libraryName must be non-empty strings");
}
const cmd = new SearchLibraryCommand(query.trim(), libName.trim(), { type: "json" }); Defensive patterns
Strategy: validation
Validate before calling
function requireSearchParams(query: unknown, libraryName: unknown): [string, string] {
if (typeof query !== "string" || !query.trim()) {
throw new Error("query must be a non-empty string");
}
if (typeof libraryName !== "string" || !libraryName.trim()) {
throw new Error("libraryName must be a non-empty string");
}
return [query.trim(), libraryName.trim()];
}
const [q, lib] = requireSearchParams(query, libraryName);
const cmd = new SearchLibraryCommand(q, lib, { type: "json" }); Type guard
function isNonEmptyString(v: unknown): v is string {
return typeof v === "string" && v.trim().length > 0;
} Try / catch
try {
const cmd = new SearchLibraryCommand(query, libraryName);
} catch (e) {
if (e instanceof Context7Error && /query and libraryName are required/.test(e.message)) {
// Surface a user-facing form error instead of letting the SDK exception propagate.
return showUserError("Please enter both a query and a library name.");
}
throw e;
} Prevention
- 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.
When it happens
Trigger: Calling `new SearchLibraryCommand('', 'react')`, `new SearchLibraryCommand('react', '')`, omitting an argument, or passing null/undefined for either required parameter.
Common situations: 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.
Related errors
- Request did not return a result
- Skill file path "${file.path}" resolves outside the target d
- Unsafe skill name: ${JSON.stringify(skillName)}
- Skill name "${skillName}" escapes the skills root
- API key is required. Pass it in the config or set CONTEXT7_A
AI-assisted analysis of upstash/context7@ca15df0443 (2026-08-12).
Data as JSON: /api/errors/59b825f92ee696ed.
Report an issue: GitHub.