Stop form submission and return custom error message

I am trying to stop the form submission process (with my custom error message) if for some reason a hooked function isn’t able to complete.

Example:
I have a function which creates a CSV based on the form inputs (hooked to gform_pre_submission). If the file isn’t able to be created (e.g. file permission issue), I wasn’t to stop the form submission process and present an error.