{"record":{"id":"42fd1b0f8382ac2f","repo":"denoland/deno","slug":"err-invalid-arg-type-42fd1b","errorCode":"ERR_INVALID_ARG_TYPE","errorMessage":"The \"candidate\" argument must be ${expected}. Received ${candidate}","messagePattern":"The \"candidate\" argument must be (.+?)\\. Received (.+?)","errorType":"exception","errorClass":"NodeTypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/internal/crypto/random.ts","lineNumber":109,"sourceCode":"  let candidateBytes: ArrayBufferView | ArrayBuffer;\n  if (typeof candidate === \"bigint\") {\n    if (candidate < 0) {\n      throw new ERR_OUT_OF_RANGE(\"candidate\", \">= 0\", candidate);\n    }\n    candidateBytes = bigintToBytes(candidate);\n  } else if (isAnyArrayBuffer(candidate) || isArrayBufferView(candidate)) {\n    const byteLength = isArrayBufferView(candidate)\n      ? arrayBufferViewByteLength(candidate as ArrayBufferView)\n      : ArrayBufferPrototypeGetByteLength(candidate as ArrayBuffer);\n    if (byteLength > OPENSSL_BIGNUM_MAX_BYTES) {\n      throw new NodeError(\n        \"ERR_OSSL_BN_BIGNUM_TOO_LONG\",\n        \"bignum too long\",\n      );\n    }\n    candidateBytes = candidate;\n  } else {\n    throw new ERR_INVALID_ARG_TYPE(\n      \"candidate\",\n      [\n        \"ArrayBuffer\",\n        \"TypedArray\",\n        \"Buffer\",\n        \"DataView\",\n        \"bigint\",\n      ],\n      candidate,\n    );\n  }\n\n  PromisePrototypeCatch(\n    PromisePrototypeThen(\n      op_node_check_prime_bytes_async(candidateBytes, checks),\n      (result) => {\n        callback?.(null, result);\n      },","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/internal/crypto/random.ts#L91-L127","documentation":"Thrown by checkPrime() in Deno's node:crypto polyfill (ext/node/polyfills/internal/crypto/random.ts:109) when candidate is neither a BigInt nor an ArrayBuffer/TypedArray/Buffer/DataView. The API accepts only binary or BigInt forms; plain numbers, strings, and objects are rejected because primality testing requires arbitrary-precision input.","triggerScenarios":"crypto.checkPrime(17, cb), crypto.checkPrime('17', cb), or crypto.checkPrime(BigInt('17').toString(), cb). Passing null/undefined or a Number from JSON.parse also triggers it.","commonSituations":"JSON configuration holding a large number as a string that is passed directly; refactoring code that used a different primality library accepting numbers; forgetting to convert a hex string with BigInt('0x'+hex).","solutions":["Convert numbers/strings to BigInt first: BigInt(value) or BigInt('0x' + hexString).","Pass binary data as Buffer/Uint8Array when the value already exists as bytes.","If the value may arrive in several forms, normalize it with a small coercion helper before calling checkPrime."],"exampleFix":"// before\ncrypto.checkPrime(input /* '0x...f' string */, cb);\n\n// after\nconst candidate = typeof input === 'string' ? BigInt(input) : input;\ncrypto.checkPrime(candidate, cb);","handlingStrategy":"type-guard","validationCode":"if (typeof candidate !== 'bigint' && !ArrayBuffer.isView(candidate) && !(candidate instanceof ArrayBuffer)) throw new TypeError('candidate must be bigint or binary');","typeGuard":"function isPrimeCandidate(v) { return typeof v === 'bigint' || ArrayBuffer.isView(v) || v instanceof ArrayBuffer; }","tryCatchPattern":"try { crypto.checkPrime(c, cb); } catch (e) { if (e.code === 'ERR_INVALID_ARG_TYPE') c = BigInt(c); else throw e; }","preventionTips":["Convert strings/numbers to BigInt at the system boundary.","Never pass JSON-parsed numbers straight into crypto APIs."],"tags":["node-compat","crypto","prime","type-error","argument-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}