Error handling
The exceptions NexPDF for Python raises, the engine's error messages, license error codes, and patterns for retrying and reporting failures.
Every NexPDF call either returns its result or raises an exception. There are no status codes to check by hand: the SDK reads the engine's per-thread error and turns it into an exception.
Exception types
| Exception | Raised when |
|---|---|
nexpdf.NexPdfError | An engine call failed. str(e) is the engine's message. Subclass of RuntimeError. |
nexpdf.LicenseError | activate_license rejected the key. Subclass of NexPdfError with a negative code. |
OSError | The native library could not be found or loaded. |
ValueError | A method was called on a closed Document. |
TypeError | Document() was called directly; use Document.open or Document.from_bytes. |
import nexpdf
try:
with nexpdf.Document.open("upload.pdf") as doc:
doc.add_watermark("CONFIDENTIAL")
doc.save("out.pdf")
except nexpdf.LicenseError as e:
log.error("license rejected (%d): %s", e.code, e)
except nexpdf.NexPdfError as e:
log.warning("NexPDF could not process upload.pdf: %s", e)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 Document.open |
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 activate_license 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 nexpdf.is_licensed(module), or upgrade the plan |
internal error: ... | The engine caught an unexpected failure on this input | The process keeps running; report the file to support |
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 error codes
LicenseError.code is one of:
| Code | Meaning |
|---|---|
| -1 | Malformed key |
| -2 | Bad signature (the key was changed, or it is not a NexPDF key) |
| -3 | Unsupported key version (update the SDK) |
| -4 | Expired |
| -5 | Not yet valid (check the system clock) |
| -6 | Wrong platform |
| -7 | Wrong application id |
A rejected key leaves the previous license state as it was, so a process that had no key stays in view-only mode.
The last error
nexpdf.last_error() returns the engine's last error message for the current thread, or None. You
rarely need it, because the SDK raises with that message already, but it is useful when you call
nexpdf.invoke directly.
Untrusted input
The engine is written in Rust and fuzzed; a malformed PDF produces a NexPdfError, not a crash.
When you process uploads:
- catch
NexPdfErrorper file, so one bad file does not stop a batch; - limit the file size you accept before opening it, since documents are read into memory;
- close documents in a
withblock so memory is released on failure too.
def page_count_or_none(data: bytes) -> int | None:
try:
with nexpdf.Document.from_bytes(data) as doc:
return doc.page_count()
except nexpdf.NexPdfError:
return None