I have a stand alone script, that opens a QGIS project file in order to extract details.
I accidentally moved a file and the script broke. Below is the interesting part.
QgsApplication.setPrefixPath("/usr", True)
project = QgsProject.instance()
qgs = QgsApplication([], False)
qgs.initQgis()
project.read('/home/jdoe/textproj.qgz')
# invalid path, but does not fail or warn in any way
print(project)
# prints: <QgsProject:'/home/jdoe/textproj.qgz'(singleton instance)>
# no indication of any problems with loading file
Question 1 - Why no error?
I think everyone would expect it to fail (throwing an exception). Is there any good reason for it to behave different than failing?
Question 2 - How to properly load a project from a file?
Is there anything in my code that is is using the library differently than intended, which may partly explain why I got no errors?
Question 3 - How do I make sure it fails?
Of course I could write my own assertion to test if the path does point to a file. But I guess there could be other ways in which reading/loading a project file could fail. So what is the proper way of catching this right away?