Error handling
How NexPDF for the web reports failures, the engine's error messages, why a license key was rejected, and patterns for handling files users pick.
Every engine call either returns its result or throws a JavaScript 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 on the engine object (activateLicense, verifySignatures, ...). e.message is the engine's message. |
Error('HTTP <status>') | PdfDocument.fromUrl got a response that is not OK. |
Error("failed to fetch Wasm: ..."), WebAssembly.CompileError | loadNexPdf() could not download or compile the WebAssembly module. |
Error('NexPDF: could not fetch <file> (<status>)') | loadStandardFonts(baseUrl) could not download one of the twelve font files. |
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.
try {
const doc = await nex.PdfDocument.fromFile(file);
doc.raw.addWatermark('CONFIDENTIAL');
download(doc.toBlob(), file.name);
doc.close();
} catch (e) {
const message = (e as Error).message;
if (message.includes('license')) showUpgradeHint(message);
else showError(`NexPDF could not process ${file.name}: ${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 fromFile, fromBytes or fromUrl |
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 after loadNexPdf(); 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 | The clock of the user's device is wrong |
license is not valid on platform '<platform>' | -6 | Use a key issued for platform web |
license is not valid for application '<host>' | -7 | The page's host name is not one the key was issued for |
A rejected key leaves the previous license state as it was, so a page that had no key stays in view-only mode.
Loading the module
loadNexPdf() keeps the promise of its first call and returns it on every later call. If the first
load fails (a wrong wasmUrl, a network error), later calls return the same rejected promise; fix
the cause and reload the page.
let nex;
try {
nex = await loadNexPdf('/assets/nexpdf_bg.wasm');
} catch (e) {
showError(`The PDF engine could not be loaded: ${(e as Error).message}`);
}Untrusted input
The engine is written in Rust and fuzzed; a malformed PDF throws an Error, not a crash of the page.
When users pick or drop files:
- 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.
async function pageCountOrNull(file: File): Promise<number | null> {
let doc;
try {
doc = await nex.PdfDocument.fromFile(file);
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 instance as unusable after one: if you run it
in a Web Worker, start a new worker; otherwise reload the page.