Error handling
The errors NexPDF for iOS (Swift) throws, the engine's error messages, license rejections, the reader's open errors, and patterns for untrusted files.
Every NexPDF call that can fail is marked throws. There are no status codes to check by hand:
the SDK reads the engine's per-thread error right after the call and throws it as NexPdfError.
Error types
| Error | Thrown when |
|---|---|
NexPdfError | An engine call failed. message is the engine's text (also localizedDescription). isLicense is true when the message is about the license |
CocoaError and other Foundation errors | open(url:password:) could not read the file, or write(to:) could not write it |
NexPdfReaderController.OpenError | The reader could not open a document: .passwordRequired, .failed(message) or .notReady |
import NexPdf
do {
let doc = try NexPdfDocument.open(url: uploadURL)
try doc.addWatermark(text: "CONFIDENTIAL")
try doc.write(to: outURL)
} catch let error as NexPdfError where error.isLicense {
log.error("license: \(error.message)")
} catch let error as NexPdfError {
log.warning("NexPDF could not process the file: \(error.message)")
} catch {
log.error("file error: \(error)")
}A few calls never throw: pages, pageSize(_:), pageWidth(pageIndex:),
pageHeight(pageIndex:), trimMemory(), memoryUsageBytes() and NexPdf.isLicensed(_:).
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 open(url:password:) |
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 launch; 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 app 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, not shown to users word for word.
License rejections
NexPdf.activateLicense(_:appId:platform:) returns 2 (commercial) or 1 (trial) and throws
NexPdfError for anything else. The Swift API does not expose the engine's numeric code; the
message gives the reason, which is one of:
| Engine 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 device clock) |
| -6 | Wrong platform (ios or macos) |
| -7 | Wrong application id (the bundle id) |
A rejected key leaves the previous license state as it was, so an app that had no key stays in view-only mode, and an app that had a valid key keeps it.
Password prompts
Tell a missing or wrong password from other failures by the message:
func open(_ url: URL, password: String? = nil) throws -> NexPdfDocument? {
do {
return try NexPdfDocument.open(url: url, password: password)
} catch let error as NexPdfError where error.message == "password required" || error.message == "incorrect password" {
return nil // ask the user, then call open(url, password: typed)
}
}In the reader, NexPdfReaderController.open(data:name:password:) throws
OpenError.passwordRequired for both cases.
Untrusted input
The engine is written in Rust and fuzzed; a malformed PDF throws NexPdfError, it does not crash
the app. When you open files from the share sheet, the Files app or a server:
- catch
NexPdfErrorper file, so one bad file does not stop a batch; - check the file size before opening it, since the whole file is read into memory;
- open and process files off the main thread, so a large or slow file does not freeze the UI (see Platform notes).
func pageCount(of data: Data) -> Int? {
guard let doc = try? NexPdfDocument.open(data: data) else { return nil }
return doc.pages
}