'pyrevit run' does not raise exception on fail to open document

I am trying to use ‘pyrevit run’ to batch process models copied from Autodesk Desktop Connector to another local folder (downloading the files with APS api calls is not an option).

Sometimes (but not always) Desktop Connector silently packages Revit files as zip archives without changing the file extension (see Revit Journal records “COleException”, code=0x80004005: Unspecified error). When this happens, Revit fails to open the .rvt file, but the issue can be solved by changing the file extension of the copied file to .zip, extracting the archive, then opening the extracted .rvt file of the same name.

Since this doesn’t happen every time, I want to handle it with a try/catch block in PowerShell, but ‘pyrevit run’ does not raise an exception when failing to open the document, so it never triggers the try/catch block. See typical output below:

Debug: Attempting to read “BasicFileInfo” stream from structured storage file at “C:\Users\...\RVT\MyModel.rvt”
Error: File is not a structured storage file (System.NotSupportedException)
at pyRevitLabs.Common.CommonUtils.GetStructuredStorageStream(String filePath, String streamName)
at pyRevitLabs.TargetApps.Revit.RevitModelFile..ctor(String filePath)
at pyRevitCLI.PyRevitCLIRevitCmds.RunExtensionCommand(String commandName, String targetFile, String revitYear, PyRevitRunnerOptions runOptions, Boolean targetIsFileList)
at pyRevitCLI.PyRevitCLI.ProcessArguments()
at pyRevitCLI.PyRevitCLI.Main(String[] args)

Can you direct me to the relevant part of the source code, so I can try to feed that error message back to the host shell?

I do a similar process for files that are downloaded from the APS in a Python process, not pyRevit but I am sure it will work the same.

Rather than trying to open the file, first try to extract the files from the zip.

import zipfile
try:
      with zipfile.ZipFile(file_path) as zip_ref:
           for file in zip_ref.namelist():
               if file == file_Name:
                     zip_ref.extract(file, path=temp_path)
Except:
     print('not a zip')

Not 100% sure this will work for you, but it does for me. After the above, I try to open the file. Doing it in this order is also helpful, as you now do not have a zip, and the next step is to open it up.

P.S. there are much cleaner ways to do this.

A cleaner way may be to just check if its a zip file first.

if zipfile.is_zipfile(file_path):
extract zip