Skip to Content
NexPDF

Error handling

The exceptions NexPDF for Python raises, the engine's error messages, license error codes, and patterns for retrying and reporting failures.

English

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

ExceptionRaised when
nexpdf.NexPdfErrorAn engine call failed. str(e) is the engine's message. Subclass of RuntimeError.
nexpdf.LicenseErroractivate_license rejected the key. Subclass of NexPdfError with a negative code.
OSErrorThe native library could not be found or loaded.
ValueErrorA method was called on a closed Document.
TypeErrorDocument() 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

MessageCauseWhat to do
password requiredThe file is encrypted and no password was givenAsk for the password and pass it to Document.open
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 activate_license at start-up; see Running without a key
the active license does not include the '<module>' moduleYour plan does not include the moduleCheck nexpdf.is_licensed(module), or upgrade the plan
internal error: ...The engine caught an unexpected failure on this inputThe 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:

CodeMeaning
-1Malformed key
-2Bad signature (the key was changed, or it is not a NexPDF key)
-3Unsupported key version (update the SDK)
-4Expired
-5Not yet valid (check the system clock)
-6Wrong platform
-7Wrong 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 NexPdfError per 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 with block 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

On this page