Error handling
How NexPDF for React Native reports failures - NexPdfError, the engine's messages, license errors, native module errors, and patterns for untrusted files.
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
| Error | Rejected when |
|---|---|
NexPdfError | An 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
| Message | Cause | What to do |
|---|---|---|
password required | The file is encrypted and no password was given | Ask for the password and pass it to NexPdf.open(base64, password) |
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 at start-up; 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 |
internal error: ... | The engine caught an unexpected failure on this input | The app keeps running; report the file to support |
The bridge adds a few messages of its own:
| Message | Cause |
|---|---|
unknown or closed document handle <n> | A method was called on a document after close() |
argument <i>: invalid base64 | A byte argument is not valid base64 |
argument <i>: number expected, string expected | A wrong argument type, usually undefined where a number is required |
call failed | The 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:
| Message | Code in other SDKs | Meaning |
|---|---|---|
malformed license key | -1 | The text is not a NexPDF 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 device clock is behind |
license is not valid on platform '<platform>' | -6 | The key does not list ios or android |
license is not valid for application '<id>' | -7 | The 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
finallyblock, 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();
}
}