Skip to Content
NexPDF

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.

English

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

ErrorThrown when
Error with the engine's messageAn 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.CompileErrorloadNexPdf() 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

MessageCauseWhat to do
password requiredThe file is encrypted and no password was givenAsk for the password and pass { password } to fromFile, fromBytes or fromUrl
incorrect passwordThe password matches neither the user nor the owner passwordAsk again
no license activated: '<module>' requires a NexPDF license keyThe call needs a module and no key is activeCall activateLicense after loadNexPdf(); see Running without a key
the active license does not include the '<module>' moduleYour plan does not include the moduleCheck 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.

MessageCodeWhat to do
malformed license key-1Pass the whole XML file or the whole NEX2. key
license key signature is not valid-2The key was changed, or it is not a NexPDF key
license key format version is not supported by this SDK-3Update the SDK
license expired at <time> (Unix time)-4Renew the key
license is not valid yet (issued at <time>); check the device clock-5The clock of the user's device is wrong
license is not valid on platform '<platform>'-6Use a key issued for platform web
license is not valid for application '<host>'-7The 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 a finally block 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.

On this page