Skip to Content
NexPDF

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.

English

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

ExceptionThrown when
nexdevit::nexpdf::ErrorAn engine call failed. what() is the engine's message. Derives from std::runtime_error.
nexdevit::nexpdf::Error with code() < 0activate_license rejected the key. code() is the negative status (see below); it is 0 for every other failure.
nexdevit::nexpdf::ErrorDocument::open could not read the file (cannot open <path>) or save could not write it (cannot write <path>).
std::logic_errorA 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

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
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:

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.

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, or NULL. The string is a copy: free it with nexpdf_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 be 0, for example), then check nexpdf_last_error() after it.
  • Free every string the engine returns with nexpdf_free_string, every NexPdfBytes with nexpdf_free_bytes and every document with nexpdf_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::Error per 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 Document objects 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;
    }
}

On this page