Error handling
How NexPDF for Node.js reports failures, the engine's error messages, why a license key was rejected, and patterns for retrying and reporting failures.
Every NexPDF call either returns its result or throws an Error. There are no status codes to
check by hand: the WebAssembly bindings read the engine's error and throw it with the engine's
message.
What throws
| Error | Thrown when |
|---|---|
Error with the engine's message | An engine call failed: on PdfDocument, on doc.raw, or a package function (activateLicense, verifySignatures, ...). e.message is the engine's message. |
Node.js file system errors (ENOENT, EACCES, ...) | PdfDocument.open(path) could not read the file, or doc.save(path) could not write it. They have a code. |
Cannot find module './wasm/nexpdf.js' | The package was imported without its wasm/ folder; see Troubleshooting. |
The engine has no error codes in JavaScript: a rejected license key, a missing module and a damaged
file all throw an Error, and the message tells them apart.
import { PdfDocument } from '@nexdevit/nexpdf-node';
try {
const doc = await PdfDocument.open('upload.pdf');
doc.addWatermark('CONFIDENTIAL');
await doc.save('out.pdf');
doc.close();
} catch (e) {
if (e.code === 'ENOENT') log.error(`no such file: ${e.path}`);
else log.warn(`NexPDF could not process upload.pdf: ${e.message}`);
}Common engine messages
| Message | Cause | What to do |
|---|---|---|
password required | The file is encrypted and no password was given | Ask for the password and pass { password } to PdfDocument.open |
incorrect password | The password matches neither the user nor the owner password | Ask again |
no license activated: '<module>' requires a NexPDF license key | The call needs a module and no key is active | Call activateLicense at start-up; see Running without a key |
the active license does not include the '<module>' module | Your plan does not include the module | Check licenseStatus().modules, or upgrade the plan |
The two password errors thrown when opening also carry flags, so you do not need to compare
messages: passwordRequired is true for both, and wrongPassword only for incorrect password.
See Opening and saving.
Other messages describe the input: a malformed or truncated PDF, a page index out of range, invalid JSON options. They are meant to be logged as they are.
Why a key was rejected
activateLicense throws when it rejects a key. The message gives the reason; the number is the code
the native SDKs return for the same reason.
| Message | Code | What to do |
|---|---|---|
malformed license key | -1 | Pass the whole XML file or the whole NEX2. key |
license key signature is not valid | -2 | The key was changed, or it is not a NexPDF key |
license key format version is not supported by this SDK | -3 | Update the SDK |
license expired at <time> (Unix time) | -4 | Renew the key |
license is not valid yet (issued at <time>); check the device clock | -5 | Fix the system clock (NTP) |
license is not valid on platform '<platform>' | -6 | Use a key issued for platform web; see License |
license is not valid for application '<appId>' | -7 | Pass the app id the key was issued for as appId |
A rejected key leaves the previous license state as it was, so a process that had no key stays in view-only mode.
Untrusted input
The engine is written in Rust and fuzzed; a malformed PDF throws an Error, not a crash of the
process. When you process uploads:
- catch errors per file, so one bad file does not stop a batch;
- limit the file size you accept before opening it, since the whole file is copied into WebAssembly memory;
- call
doc.close()in afinallyblock so memory is released on failure too.
function pageCountOrNull(bytes) {
let doc;
try {
doc = PdfDocument.fromBytes(bytes);
return doc.pageCount;
} catch {
return null;
} finally {
doc?.close();
}
}On native builds, an unexpected failure inside the engine is caught and reported as
internal error: .... Under WebAssembly such a failure stops the module with a
WebAssembly.RuntimeError instead. Treat the engine as unusable after one: run risky batches in a
worker_threads worker or a child process, and replace it when this happens.