Skip to Content
NexPDF

Error handling

How NexPDF for React Native reports failures - NexPdfError, the engine's messages, license errors, native module errors, and patterns for untrusted files.

English

Every NexPDF method returns a Promise. It resolves with the result, or rejects. There are no status codes to check by hand: the engine's error travels back in the bridge reply, and the SDK rejects with it.

Error types

ErrorRejected when
NexPdfErrorAn engine call failed. e.message is the engine's message; e.license is true when the message is about the license. Subclass of Error, name is "NexPdfError".
NexPdfError "NexPDF native module not linked (ios)"The native module is missing from the app binary. Rebuild the app; see Troubleshooting.
Error with code "NEXPDF"The native side itself failed before the engine could answer, for example the Android engine library could not be loaded, or the reader could not be presented.
import { NexPdf, NexPdfError } from '@nexdevit/nexpdf-react-native';

try {
  const doc = await NexPdf.open(uploadBase64);
  try {
    await doc.addWatermark('CONFIDENTIAL');
    return await doc.saveBytes();
  } finally {
    await doc.close();
  }
} catch (e) {
  if (e instanceof NexPdfError && e.license) {
    showUpgradeMessage(e.message);
  } else if (e instanceof NexPdfError) {
    console.warn('NexPDF could not process the file:', e.message);
  } else {
    throw e;   // native module or app error
  }
}

e.license is a text match on the message (license or licence). Use it to tell license failures from problems with the file.

Common engine messages

MessageCauseWhat to do
password requiredThe file is encrypted and no password was givenAsk for the password and pass it to NexPdf.open(base64, password)
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 at start-up; 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
internal error: ...The engine caught an unexpected failure on this inputThe app keeps running; report the file to support

The bridge adds a few messages of its own:

MessageCause
unknown or closed document handle <n>A method was called on a document after close()
argument <i>: invalid base64A byte argument is not valid base64
argument <i>: number expected, string expectedA wrong argument type, usually undefined where a number is required
call failedThe engine returned no result and no message

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.

License errors

A rejected key rejects activateLicense with NexPdfError. The bridge passes the message, not the numeric code that other SDKs return. The messages map to the codes like this:

MessageCode in other SDKsMeaning
malformed license key-1The text is not a NexPDF 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 device clock is behind
license is not valid on platform '<platform>'-6The key does not list ios or android
license is not valid for application '<id>'-7The app id does not match the key

A rejected key leaves the previous license state as it was, so an app that had no key stays in view-only mode. See License.

The reader

NexPdfReader.open rejects with an Error whose code is "NEXPDF" when the reader cannot be shown, for example no view controller to present the reader from or document is not valid base64 on iOS. Errors inside the reader (a wrong password, a missing module) are shown to the user by the reader itself.

Untrusted input

The engine is written in Rust and fuzzed; a malformed PDF produces a NexPdfError, not a crash. When you open files the user picks or downloads:

  • catch errors per file, so one bad file does not stop a batch;
  • check the file size before you read it as base64, since the whole file is held in JavaScript memory and then in native memory;
  • close documents in a finally block, so native memory is released on failure too.
async function pageCountOrNull(base64: string): Promise<number | null> {
  let doc;
  try {
    doc = await NexPdf.open(base64);
    return await doc.pageCount();
  } catch (e) {
    if (e instanceof NexPdfError) return null;
    throw e;
  } finally {
    await doc?.close();
  }
}

On this page