pypdfium2-team / pypdfium2

Python bindings to PDFium
https://pypdfium2.readthedocs.io/
425 stars 17 forks source link

expose more details in PdfiumError #308

Closed elonzh closed 7 months ago

elonzh commented 7 months ago

Checklist

Description

For example error_code when available.

An common user case is pypdfium2 will raise PdfiumError when document is encrypted and we didn't provide a password.

pypdfium2._helpers.misc.PdfiumError: Failed to load document (PDFium: Incorrect password error).

...
    if pdfium_c.FPDF_GetPageCount(pdf) < 1:
        err_code = pdfium_c.FPDF_GetLastError()
        raise PdfiumError(f"Failed to load document (PDFium: {pdfium_i.ErrorToStr.get(err_code)}).")
...

Right now if we want to detect such exceptions, the only way is matching the error string which is inefficient and unstable.


Prefer solution:

Add a enum class for ErrorToStr and expose a error_code in PdfiumError`.

Install Info

pypdfium2 4.27.0
pdfium 123.0.6281.0
Linux-5.15.146.1-microsoft-standard-WSL2-x86_64-with-glibc2.35

Validity

mara004 commented 7 months ago

Good point. I agree matching against the error string is unfortunate. TBH, I never really thought much about programmatic use of exceptions or handling of error subtypes. However, I think I'd not add an enum, but just ask callers to check against the constants from the raw API (in this case, FPDF_ERR_*).

Do you have any more places other than document loading where this may be applicable? Because most pdfium APIs are sort of limited to error or success (i.e. no distinction of specific errors), where there isn't really any further use for the return code.

elonzh commented 7 months ago

Do you have any more places other than document loading where this may be applicable?

No other error cases have been found yet for me.

mara004 commented 7 months ago

Will become available when the v5 development work is finished.