Error handling
The errors NexPDF for Go returns, the engine's error messages, license error codes, and patterns for handling failures and untrusted input.
Every NexPDF call that can fail returns an error as its last result. There are no status codes to
check by hand: the SDK reads the engine's per-thread error after each call and returns it as a
*nexpdf.Error.
Error types
| Error | Returned when |
|---|---|
*nexpdf.Error | An engine call failed. Message is the engine's message; Error() prefixes it with nexpdf: . |
*nexpdf.Error with Code < 0 | ActivateLicense rejected the key. Code is the negative status (see below). |
nexpdf: document is closed | A method was called on a document after Close. This is a plain error, not a *nexpdf.Error. |
*fs.PathError | Open could not read the file or Save could not write it (from os.ReadFile and os.WriteFile). |
A few methods return no error. PageWidth and PageHeight return 0 for a closed document and the
A4 size (595 x 842 points) for a page that does not exist, so check the index against PageCount
first. TrimMemory and MemoryUsageBytes return 0 for a closed document.
import (
"errors"
"fmt"
"io/fs"
"log"
nexpdf "github.com/nexdevit/nexpdf-go"
)
func stamp(path string) error {
doc, err := nexpdf.Open(path)
if err != nil {
var pe *fs.PathError
if errors.As(err, &pe) {
return fmt.Errorf("cannot read %s: %w", path, err)
}
return fmt.Errorf("NexPDF cannot open %s: %w", path, err)
}
defer doc.Close()
if _, err := doc.AddWatermark("CONFIDENTIAL"); err != nil {
var e *nexpdf.Error
if errors.As(err, &e) {
log.Printf("NexPDF could not process %s: %s", path, e.Message)
}
return err
}
return doc.Save("out.pdf", false)
}Use errors.As rather than a type assertion, so errors you wrap with fmt.Errorf("...: %w", err)
are still recognised.
Common engine messages
Compare e.Message, which has no nexpdf: prefix.
| Message | Cause | What to do |
|---|---|---|
password required | The file is encrypted and no password was given | Ask for the password and pass it to Open or OpenBytes |
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 nexpdf.IsLicensed(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 |
doc, err := nexpdf.Open("locked.pdf", password)
var e *nexpdf.Error
if errors.As(err, &e) && (e.Message == "password required" || e.Message == "incorrect password") {
askForThePasswordAgain()
}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
ActivateLicense returns the negative status both as its first result and in Error.Code:
| 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 |
if _, err := nexpdf.ActivateLicense(key, "server", "com.acme.invoicing"); err != nil {
var e *nexpdf.Error
if errors.As(err, &e) {
log.Fatalf("NexPDF license rejected (%d): %s", e.Code, e.Message)
}
}A rejected key leaves the previous license state as it was, so a process that had no key stays in
view-only mode. Errors from every other call have Code 0.
Per-thread errors
The engine records the last error per OS thread. Each SDK call locks its goroutine to its OS
thread for the duration (runtime.LockOSThread), clears the error, calls the engine and reads the
error back, so the error you get is always the one of that call, even with many goroutines. You do
not need to lock threads yourself.
nexpdf.Invoke(requestJSON) reports failures inside its JSON result
({"ok":false,"error":"..."}) instead of as an error.
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 returned as internal error: .... When you process uploads:
- handle the 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;
defer doc.Close()right after a successful open, so native memory is released on every return path.
func pageCountOrZero(data []byte) int {
doc, err := nexpdf.OpenBytes(data)
if err != nil {
return 0
}
defer doc.Close()
n, err := doc.PageCount()
if err != nil {
return 0
}
return int(n)
}