Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper error message when PDF doesn't exist #19

Closed
duskybomb opened this issue May 22, 2018 · 4 comments
Closed

Proper error message when PDF doesn't exist #19

duskybomb opened this issue May 22, 2018 · 4 comments
Assignees
Labels

Comments

@duskybomb
Copy link

If there is no pdf named inv2.pdf in the directory

  File "my_file.py", line 4, in <module>
    factx = FacturX('inv2.pdf')
  File "D:\GSoC\factur-x\facturx\facturx.py", line 59, in __init__
    xml = self._xml_from_file(pdf_file)
  File "D:\GSoC\factur-x\facturx\facturx.py", line 80, in _xml_from_file
    pdf = PdfFileReader(pdf_file)
  File "D:\Projects\Invoice2data-GUI\venv\lib\site-packages\PyPDF2\pdf.py", line 1084, in __init__
    self.read(stream)
  File "D:\Projects\Invoice2data-GUI\venv\lib\site-packages\PyPDF2\pdf.py", line 1697, in read
    line = self.readNextEndLine(stream)
  File "D:\Projects\Invoice2data-GUI\venv\lib\site-packages\PyPDF2\pdf.py", line 1937, in readNextEnd
Line
    raise utils.PdfReadError("Could not read malformed PDF file")
PyPDF2.utils.PdfReadError: Could not read malformed PDF file
@m3nu
Copy link
Collaborator

m3nu commented May 22, 2018

I'm aware. The sample code was just a sample.

There should still a better error message when the input file doesn't exist.

@m3nu m3nu changed the title PdfReadError: Could not read malformed PDF file Proper error message when PDF doesn't exist May 22, 2018
@m3nu
Copy link
Collaborator

m3nu commented May 22, 2018

As I said there need to be some checks to see:

  • is the input a path
  • does it exist
  • is it a BytesIO or file point?
  • ...

@duskybomb
Copy link
Author

My observations:

  • if the path is a string and exists then it is handled by first if condition: isinstance(pdf_invoice, str) and pdf_invoice.endswith('.pdf') and os.path.isfile(pdf_invoice)
  • if the path was in form of BytesIO or file point it is handled by third condition: isinstance(pdf_invoice, file_types)
  • when path is a string and doesn't exist or it isn't of pdf format, then second condition was handling it: isinstance(pdf_invoice, str)

Hence I added a commit (#18) to raise FileNotFoundError in second condition: isinstance(pdf_invoice, str)

@m3nu
Copy link
Collaborator

m3nu commented May 22, 2018

Cool. To put the icing on the cake you could also make a test that triggers this exception. Would be a good habit to always do feature + test together. Else tests generally get neglected.

@m3nu m3nu assigned duskybomb and m3nu and unassigned duskybomb May 23, 2018
@m3nu m3nu closed this as completed May 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants