Error handling
The exceptions NexPDF for Flutter throws, the engine's error messages, license errors, errors from dart:ffi calls and the native reader, and patterns for untrusted files.
The Dart API either returns its result or throws. The SDK reads the engine's per-thread error right
after a failed call and throws NexPdfException with that message. For C functions you call
yourself through dart:ffi, the nexpdfCall helper from
Calling the C API does the same.
Exception types
| Exception | Thrown when |
|---|---|
NexPdfException | An engine call failed. e.message is the engine's message. Thrown by open, openFile, renderPagePng, save, ask and activateLicense, and by nexpdfCall. |
StateError | A NexPdfDocument was used after dispose() (document was disposed). |
ArgumentError | dart:ffi could not load the engine library or find a function in it (NexPdfFlutter(), DynamicLibrary.open, lookupFunction). |
FileSystemException | openFile could not read the file. |
PlatformException | NexPdfReader.open failed on the native side; code is NEXPDF. |
MissingPluginException | NexPdfReader was called where the plugin has no native side (macOS, Windows, Linux), or before a full rebuild after adding the plugin. |
import 'dart:typed_data';
import 'package:flutter/foundation.dart';
import 'package:nexpdf_flutter/nexpdf_flutter.dart';
final sdk = NexPdfFlutter();
Uint8List? stampUpload(Uint8List data) {
NexPdfDocument? doc;
try {
doc = sdk.open(data);
// ... engine calls through nexpdfCall ...
return doc.save();
} on NexPdfException catch (e) {
debugPrint('NexPDF could not process the upload: ${e.message}');
return null;
} finally {
doc?.dispose();
}
}Calls that do not throw
A few methods return an empty result instead of throwing:
| Method | On failure |
|---|---|
search(query) | [] (also for an empty query) |
toMarkdown() | '' |
pageView(i) | A plain view built from pageSize(i) |
Call sdk.lastError() straight after them, on the same isolate, if you need to know why.
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 sdk.open(bytes, 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 the license status 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 |
empty document | open was given no bytes | Check the file was read |
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
activateLicense returns 2 for a commercial key and 1 for a trial key. A rejected key throws
NexPdfException with the engine's message; the numeric code is not part of the exception. The
messages map to the codes like this:
| Message | Code | 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 NexPdfFlutter.platform |
license is not valid for application '<id>' | -7 | The app id does not match the key |
To get the code itself, call nexpdf_license_activate through dart:ffi as shown in
License. A rejected key leaves the previous license state as it was, so an
app that had no key stays in view-only mode.
The last error
sdk.lastError() returns the engine's last error message for the current thread, or null. Errors
are kept per thread, so read it on the isolate that made the failing call, straight after the call.
The SDK and nexpdfCall already do this for you.
The native reader
NexPdfReader.open throws PlatformException with code NEXPDF when the reader cannot be shown,
for example no view controller to present the reader from on iOS, or when the document cannot be
written to the cache on Android. 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 NexPdfException, not a crash.
When you open files the user picks or downloads:
- catch
NexPdfExceptionper file, so one bad file does not stop a batch; - check the file size before you open it, since documents are read into memory;
- dispose documents in a
finallyblock, so native memory is released on failure too; - open large or unknown files in a background isolate, so a slow file does not freeze the UI.
int? pageCountOrNull(Uint8List data) {
NexPdfDocument? doc;
try {
doc = sdk.open(data);
return doc.pageCount;
} on NexPdfException {
return null;
} finally {
doc?.dispose();
}
}