Dev Helpers

Node.js / URLs

Safely parse malformed input

Dev Helpers · November 14, 2026

Parsing user-provided JSON needs a failure path. Successful parsing alone does not establish that the result has the expected shape.

Worked example

import {
  URL,
  domainToASCII,
  domainToUnicode,
  pathToFileURL,
  fileURLToPath,
} from "node:url";
console.log(URL.parse("broken url"));

Result

null

Implementation notes

Distinguish parse failure from a valid null result. A fallback that uses null for both cases can make downstream error handling ambiguous.

Try it yourself

Try malformed text and valid JSON containing the wrong type.

Open the Node.js example →

Editorial date November 14, 2026 · Published December 6, 2026

Related articles

Checksum bytes with CRC32

CRC32 is useful for detecting accidental changes to a byte sequence. Record the input encoding so different producers compute the same checksum.

Attach details to a mark

Performance marks can carry details about the operation they describe. Small stable identifiers are easier to aggregate than large payloads.

Capture positional arguments

Process arguments include runtime and entry-point information before user arguments. Slice off those positions before interpreting input.