pub.file:getFile as Stream, can't close

I have a service doing a getFile loadAs = Stream

If for some reason the convertToValues fails, the flow drops down to the catch block. The file then stays locked. I can not delete or change.

I have a closeStream in the try block that works fine but when I copy it to my catch block and try to dig the stream out of lastError/pipeline, the file remains locked.

Any ideas on how to cleanup the stream/file in this scenario?

Thanks!
Cort

Cort,

The problem you’re running into is that it can be tricky to retrieve elements from the lastError/pipeline document, ESPECIALLY when dealing with objects.

I, for one, try to avoid retrieving elements from the lastError/pipeline as much as possible. The way I accomplish this is by initializing every variable I may have to use in the catch block BEFORE the try block. In your case, however, you’re dealing with an InputStream object so it’s NOT as easy as setting a string to an empty value with a simple MAP step in the beginning of the service.

My suggestion to you then is to get the file before you jump into the try block. You could, however, get an exception when simply trying to open the file, so you’re going to need two sets of try-catch blocks. Here’s what your code should end up looking like:

main: SEQUENCE (Exit on SUCCESS)
— try: SEQUENCE (Exit on FAILURE)
------ getFile (this initializes the stream object)
— catch: SEQUENCE (Exit on DONE)
------ getLastError
------ Handle error (ex. send notification, signal failure, etc.)

main: SEQUENCE (Exit on SUCCESS)
— try: SEQUENCE (Exit on FAILURE)
------ Code that is throwing the exception
— catch: SEQUENCE (Exit on DONE)
------ getLastError
------ closeFile using the stream object initialized via getFile
------ Handle error (ex. send notification, signal failure, etc.)

Let me know if I can help further.

  • Percio

Hi Percio,

Thanks for the tip! I tested this and it’s working great.

-Cort-