santifer/career-ops · error · Error

H1B_API_BASE is not a valid URL

Error message

H1B_API_BASE is not a valid URL: ${trimmed}

What it means

resolveBase throws this when H1B_API_BASE is set to a non-empty string that the URL constructor cannot parse. The plugin validates the variable up front so a malformed base fails with a named configuration error instead of a bare 'fetch failed' later.

Solutions

  1. Prefix the value with its scheme, e.g. H1B_API_BASE=https://my-worker.example.com.
  2. Print the variable (`echo "$H1B_API_BASE"`) and check for hidden whitespace, quotes, or truncation.
  3. Re-copy the URL from your worker's deployment output.

Example fix

// before
H1B_API_BASE=my-worker.example.com
// after
H1B_API_BASE=https://my-worker.example.com
Defensive patterns

Strategy: validation

Validate before calling

const raw = process.env.H1B_API_BASE;
if (raw !== undefined) { try { new URL(raw.trim()); } catch { throw new Error(`H1B_API_BASE is not a URL: ${raw}`); } }

Type guard

function isUsableUrl(s) {
  if (typeof s !== 'string') return false;
  try { new URL(s.trim()); return true; } catch { return false; }
}

Try / catch

try {
  await lookup(name);
} catch (e) {
  if (e.message.includes('not a valid URL')) {
    console.error('Add the scheme, e.g. H1B_API_BASE=https://your-worker.example.com');
  }
}

Prevention

When it happens

Trigger: H1B_API_BASE contains a value like 'my-worker.example.com' (missing scheme), 'https://oops', or any string that fails `new URL(trimmed)`.

Common situations: Typing the host without the https:// prefix; stray characters or quotes pasted from documentation; a truncated value from a secrets manager.

Understand the failure class

Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.

Related errors


AI-assisted analysis of santifer/career-ops@aac998c7ed (2026-09-16). Data as JSON: /api/errors/c0cd18473b2098c3. Report an issue: GitHub.

Appendix: source

Thrown at plugins/h1b-sponsor/lib/api.mjs:50

}

function resolveBase() {
  const raw = process.env.H1B_API_BASE;
  // Absent means "use the default". Present but blank is a misconfiguration
  // (an unset shell variable, an empty .env line, a CI secret that did not
  // populate), and silently falling back would send someone's shortlist and
  // their token to a host they believed they had replaced.
  if (raw === undefined) return DEFAULT_BASE;
  const trimmed = String(raw).trim();
  if (!trimmed) {
    throw new Error('H1B_API_BASE is set but empty. Unset it to use the default endpoint.');
  }

  let parsed;
  try {
    parsed = new URL(trimmed);
  } catch {
    throw new Error(`H1B_API_BASE is not a valid URL: ${trimmed}`);
  }
  if (parsed.username || parsed.password) {
    // Undici refuses a credentialed Request anyway, and the value reaches
    // stdout through the source field, so this would print a password.
    throw new Error('H1B_API_BASE must not embed credentials.');
  }
  if (parsed.search || parsed.hash) {
    // Paths are appended, so a query or fragment swallows them: the request
    // would go to the base itself and answer about a company never asked for.
    throw new Error('H1B_API_BASE must not contain a query string or a fragment.');
  }
  // Plain http would put an Authorization header on the wire in the clear.
  // Loopback is exempt so a self-hoster can develop against a local worker,
  // but only for http. Exempting every scheme on a loopback host let
  // ftp://localhost and ws://localhost past validation, and those die later
  // inside fetch as a bare "fetch failed", which is the opaque failure this
  // check exists to replace with a named configuration error.
  const loopback = /^(localhost|127\.\d+\.\d+\.\d+|\[::1\]|::1)$/i.test(parsed.hostname);

View on GitHub (pinned to aac998c7ed)