{"record":{"id":"79f60e691b90842c","repo":"jackwener/OpenCLI","slug":"openalex-label-cannot-be-empty","errorCode":null,"errorMessage":"openalex ${label} cannot be empty","messagePattern":"openalex (.+?) cannot be empty","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"clis/openalex/utils.js","lineNumber":22,"sourceCode":"// unauthenticated; passing an email via `mailto=` opts into the polite pool\n// (faster). Work IDs are `W` followed by digits (`W2741809807`) and\n// round-trip via `https://api.openalex.org/works/<id>` or\n// `https://openalex.org/W…`.\nimport { ArgumentError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';\n\nexport const OPENALEX_BASE = 'https://api.openalex.org';\nconst UA = 'opencli-openalex-adapter (+https://github.com/jackwener/opencli)';\n\n// OpenAlex stable IDs: a single-letter prefix (`W` works, `A` authors, `S`\n// sources, `I` institutions…) + at least 4 digits. We accept just `W` here.\nconst WORK_ID = /^W\\d{4,}$/;\n// DOIs are loose — accept anything starting with \"10.\" after the optional\n// `doi.org/` prefix; OpenAlex itself does the normalization.\nconst DOI_BARE = /^10\\.\\S+$/;\n\nexport function requireString(value, label) {\n    const s = String(value ?? '').trim();\n    if (!s) throw new ArgumentError(`openalex ${label} cannot be empty`);\n    return s;\n}\n\nexport function requireBoundedInt(value, defaultValue, maxValue, label = 'limit') {\n    const raw = value ?? defaultValue;\n    const n = typeof raw === 'number' ? raw : Number(raw);\n    if (!Number.isInteger(n) || n <= 0) {\n        throw new ArgumentError(`openalex ${label} must be a positive integer`);\n    }\n    if (n > maxValue) {\n        throw new ArgumentError(`openalex ${label} must be <= ${maxValue}`);\n    }\n    return n;\n}\n\n/**\n * Resolve a user-supplied work identifier to OpenAlex's canonical path\n * segment. Accepts `W…` IDs, `doi:10.…`, raw DOIs, or full","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/openalex/utils.js#L4-L40","documentation":"requireString validates that a labeled user-supplied argument (e.g. the search `query`) is a non-empty, non-whitespace string, and throws ArgumentError otherwise. The library throws it early so an empty request never reaches the OpenAlex API. It coerces null/undefined to '' first, so any falsy or whitespace-only input triggers the error.","triggerScenarios":"Passing `''`, `'   '`, `null`, or `undefined` as the value for a labeled argument — concretely, an empty `query` to openalex search, since requireString is called by query.","commonSituations":"Shell variable holding the query is unset/empty (`$Q` expands to nothing); CLI flag provided without a value; upstream pipeline produced no query text; reading query from config where the key is missing.","solutions":["Provide a non-empty query string to the search command","Check that the environment variable or config value feeding the query is set","Trim the input yourself and fail with a clearer message before invoking the library"],"exampleFix":"// before\nconst q = process.env.QUERY; // '' when unset\nawait search(q);\n// after\nconst q = (process.env.QUERY ?? '').trim();\nif (!q) { console.error('QUERY env var must be set'); process.exit(1); }\nawait search(q);","handlingStrategy":"validation","validationCode":"function requireNonEmpty(value, label) {\n  const s = String(value ?? '').trim();\n  if (!s) throw new Error(`${label} must be a non-empty string`);\n  return s;\n}\nrequireNonEmpty(process.env.QUERY, 'query');","typeGuard":"function isNonEmptyString(v) {\n  return typeof v === 'string' && v.trim().length > 0;\n}","tryCatchPattern":"try {\n  await search(query);\n} catch (e) {\n  if (e.name === 'ArgumentError' && e.message.includes('cannot be empty')) {\n    console.error('Supply a non-empty query'); process.exitCode = 2;\n  } else throw e;\n}","preventionTips":["Always trim and check user-supplied strings before calling","Validate shell/env inputs at script startup","Fail fast with your own message instead of letting the library throw"],"tags":["openalex","argument-validation","empty-input"],"backgroundTag":"missing-required-argument","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}