Skip to Content
NexPDF

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.

English

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

ErrorWhen
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 timeThe 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.
SyntaxErrorconfig or uiConfig is a string that starts with { but is not valid JSON.
OpenErroropenDocument, 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 messageA 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:

FieldMeaning
statusThe HTTP status of the engine's reply, 401 when a password is needed
passwordRequiredThe file is encrypted and the password was missing or wrong
wrongPasswordA 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:

MessageCause
no license activated: '<module>' requires a NexPDF license keyThe engine has no key; see Running without a key
the active license does not include the '<module>' moduleThe 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:

CodeReason
-1malformed license key
-2license key signature is not valid (the key was changed, or it is not a NexPDF key)
-3license key format version is not supported by this SDK (update the engine)
-4license expired at <time> (Unix time)
-5license is not valid yet (issued at <time>); check the device clock
-6license is not valid on platform 'server' (the key is not for platform server)
-7license 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 passwordRequired before treating the failure as a damaged file.

On this page