Error handling
How NexPDF for Electron reports failures - rejected promises from the engine and the window, open errors for encrypted files, license errors in the reader, and the license codes.
Every window and engine function of @nexdevit/nexpdf-electron returns a promise (only
findBinary is synchronous). A failure rejects the promise with an Error. Inside the window, the reader shows the engine's
message to the user itself.
What rejects
| Error | When |
|---|---|
NexPDF engine not found. Looked in: ... | findBinary, startEngine or openPdfWindow found no nexpdf-webviewer binary. The message lists every path tried. |
NexPDF engine did not start in time | The engine did not report its address within 15 seconds; the process is killed. |
NexPDF engine exited (<code>): <output> | The engine stopped before it started listening, with what it printed. |
SyntaxError | config or uiConfig is a string that starts with { but is not valid JSON. |
OpenError | openDocument, pdf.open, or startEngine / openPdfWindow with file and password, could not open the document. |
Error('HTTP <status>') | documentBytes, pdf.bytes() or pdf.save() could not get the document from the engine. |
Node.js file system errors (ENOENT, ...) | pdf.open(file) or pdf.save(file) could not read or write the file. |
NexPDF document API not found: install @nexdevit/nexpdf-node ... | documents() or openDocumentFile() could not load the Node.js SDK; install it next to this package. |
Error with the engine's message | A call of the document API failed: on a PdfDocument, on doc.raw, or a function such as activateLicense. These calls throw synchronously (only the ones that read or write files, such as open and save, reject). See the Node.js error handling. |
Open errors and passwords
OpenError is an Error with three extra fields:
| Field | Meaning |
|---|---|
status | The HTTP status of the engine's reply, 401 when a password is needed |
passwordRequired | The file is encrypted and the password was missing or wrong |
wrongPassword | A password was given and it was wrong |
Its message is could not open <name>: <reason>, for example
could not open locked.pdf: password required.
async function openWithPassword(pdf, file, password) {
try {
await pdf.open(file, { password });
} catch (e) {
if (!e.passwordRequired) throw e;
const again = await askForPassword(e.wrongPassword ? 'Wrong password, try again' : 'Password');
if (again != null) return openWithPassword(pdf, file, again);
}
}askForPassword stands for your own prompt, for example a small BrowserWindow. When the user
opens a file with the window's own Open button, the reader asks for the password itself.
Errors inside the window
When the user runs an action the license does not cover, the engine refuses it and the reader shows its message in an error toast:
| Message | Cause |
|---|---|
no license activated: '<module>' requires a NexPDF license key | The engine has no key; see Running without a key |
the active license does not include the '<module>' module | The plan does not include the module; see Modules and tiers |
Leave out of modeConfig.availableModes the modes your key does not include, so users do not reach
these messages. Other messages describe the document, for example a damaged file; the engine keeps
running.
License errors
A rejected key does not make openPdfWindow or startEngine fail: the engine starts in view-only
mode and prints NexPDF: license rejected (<reason>) - running view-only with a watermark on its
standard error. To find out from code, read the status from the engine:
const status = await (await fetch(`${pdf.engine.url}/api/license`)).json();
if (status.state === 'unlicensed') console.warn('NexPDF is running view-only');The reasons, with the code the other SDKs return for each:
| Code | Reason |
|---|---|
| -1 | malformed license key |
| -2 | license key signature is not valid (the key was changed, or it is not a NexPDF key) |
| -3 | license key format version is not supported by this SDK (update the engine) |
| -4 | license expired at <time> (Unix time) |
| -5 | license is not valid yet (issued at <time>); check the device clock |
| -6 | license is not valid on platform 'server' (the key is not for platform server) |
| -7 | license is not valid for application '<id>' (the key is not for nexpdf-webviewer or your NEXPDF_APP_ID) |
A rejected key leaves the engine in the state it had, view-only at start-up.
Untrusted input
The engine is written in Rust and fuzzed, and runs in its own process: a malformed file makes
openDocument reject with the engine's reason, and neither the engine nor your app crashes. When you
open files users choose:
- catch the rejection per file and show its message;
- limit the size of files you pass to
openDocument, since the engine reads the whole file into memory; - check
passwordRequiredbefore treating the failure as a damaged file.