I’m writing a simple app that processes POSTed CSV files and am testing it against invalid input (e.g. non-CSV files). I’m using the CSV::Reader.parse command to parse the CSV in a controller method, as follows:
@parsed_file = CSV::Reader.parse(params[:file]) rescue []
However, despite the rescue statement, I’m still getting an uncaught CSV::IllegalFormatError when improper submissions are entered. What am I missing here?
Thanks!
I ended up having to monkey-patch the CSV::Reader class to properly handle the exception. I’m still not sure why it wasn’t being caught in the controller, but here’s the code I ended up writing:
Note the
rescue 0after the call toget_row, which isn’t present in the original. Definitely an ugly hack, but it’ll serve my purposes.If anyone can explain why the exception wasn’t being caught in the controller, I’ll gladly give them points for having the correct answer.