Skip to Content
NexPDF

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.

English

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

ErrorThrown when
NexPdfErrorAn 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 errorsopen(url:password:) could not read the file, or write(to:) could not write it
NexPdfReaderController.OpenErrorThe 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

MessageCauseWhat to do
password requiredThe file is encrypted and no password was givenAsk for the password and pass it to open(url:password:)
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 activateLicense at launch; see Running without a key
the active license does not include the '<module>' moduleYour plan does not include the moduleCheck NexPdf.isLicensed(module), or upgrade the plan
internal error: ...The engine caught an unexpected failure on this inputThe 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 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 device clock)
-6Wrong platform (ios or macos)
-7Wrong 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 NexPdfError per 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
}

On this page