Error handling
The exceptions NexPDF for C++ throws, how failures surface in the plain C ABI, the engine's error messages, license error codes, and patterns for untrusted input.
In C++, every NexPDF call either returns its result or throws. There are no status codes to check
by hand: nexpdf.hpp reads the engine's per-thread error after each call and throws it as a
nexdevit::nexpdf::Error. In C you check the return value and read the error yourself; see
Plain C.
Exception types
| Exception | Thrown when |
|---|---|
nexdevit::nexpdf::Error | An engine call failed. what() is the engine's message. Derives from std::runtime_error. |
nexdevit::nexpdf::Error with code() < 0 | activate_license rejected the key. code() is the negative status (see below); it is 0 for every other failure. |
nexdevit::nexpdf::Error | Document::open could not read the file (cannot open <path>) or save could not write it (cannot write <path>). |
std::logic_error | A method was called on a moved-from or reset() document. |
#include <iostream>
#include <nexpdf.hpp>
namespace nexpdf = nexdevit::nexpdf;
try {
auto doc = nexpdf::Document::open("upload.pdf");
doc.add_watermark("CONFIDENTIAL");
doc.save("out.pdf");
} catch (const nexpdf::Error& e) {
if (e.code() < 0) {
std::cerr << "license rejected (" << e.code() << "): " << e.what() << "\n";
} else {
std::cerr << "NexPDF could not process upload.pdf: " << e.what() << "\n";
}
}Document frees its native handle in its destructor, so nothing leaks when an exception unwinds
the stack.
page_width, page_height, trim_memory and memory_usage_bytes never throw Error. For a page
that does not exist, page_width and page_height return the A4 size (595 x 842 points), so check
the index against page_count() first.
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 |
try {
auto doc = nexpdf::Document::open("locked.pdf", password);
} catch (const nexpdf::Error& e) {
std::string msg = e.what();
if (msg == "password required" || msg == "incorrect password") {
ask_for_the_password_again();
} else {
throw;
}
}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
Error::code() after activate_license, and the return value of nexpdf_license_activate in C,
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.
Plain C
C functions do not throw. A failed call returns NULL (strings and documents), an empty
NexPdfBytes (ptr is NULL), or 0 or -1 (numbers), and records a message for the current
thread:
nexpdf_last_error()returns that message, orNULL. The string is a copy: free it withnexpdf_free_string.- A successful call does not clear the message. Call
nexpdf_clear_error()before a call whose return value cannot tell failure from success (a count that can be0, for example), then checknexpdf_last_error()after it. - Free every string the engine returns with
nexpdf_free_string, everyNexPdfByteswithnexpdf_free_bytesand every document withnexpdf_free.nexpdf_version()returns a static string; do not free it.
#include <stdio.h>
#include "nexpdf.h"
static void report(const char *what) {
char *err = nexpdf_last_error();
fprintf(stderr, "%s failed: %s\n", what, err ? err : "unknown error");
nexpdf_free_string(err); /* NULL is fine */
}
int stamp(NexPdfDocument *doc) {
nexpdf_clear_error();
nexpdf_add_watermark(doc, "CONFIDENTIAL");
char *err = nexpdf_last_error();
if (err) {
fprintf(stderr, "watermark failed: %s\n", err);
nexpdf_free_string(err);
return -1;
}
NexPdfBytes pdf = nexpdf_save_bytes(doc);
if (!pdf.ptr) {
report("save");
return -1;
}
/* ... write pdf.ptr, pdf.len ... */
nexpdf_free_bytes(pdf);
return 0;
}The C++ layer does exactly this for you: it calls nexpdf_clear_error before each call and throws
when nexpdf_last_error returns a message. To mix the two, doc.handle() gives the C pointer of a
Document (still owned by it), and Document::adopt(ptr) takes ownership of a document returned
by a C call.
Per-thread errors
The engine records the last error per thread, so an error on one thread never leaks into another.
Read it on the thread that made the call. nexpdf::invoke(request_json) (nexpdf_invoke in C)
reports failures inside its JSON result ({"ok":false,"error":"..."}) instead of throwing.
Untrusted input
The engine is written in Rust and fuzzed; a malformed PDF produces an error, not a crash, and engine
panics are caught and reported as internal error: ... instead of unwinding into your program.
When you process uploads:
- catch
nexpdf::Errorper file, so one bad file does not stop a batch; - limit the size you accept before opening it, since documents are read into memory;
- keep
Documentobjects on the stack or in RAII owners so memory is released on failure too.
std::optional<int32_t> page_count_or_none(const std::vector<uint8_t>& data) {
try {
return nexpdf::Document::open_bytes(data).page_count();
} catch (const nexpdf::Error&) {
return std::nullopt;
}
}