diff --git a/app/controllers/protocols_controller.rb b/app/controllers/protocols_controller.rb index fd5cd3136..b4e5d17e8 100644 --- a/app/controllers/protocols_controller.rb +++ b/app/controllers/protocols_controller.rb @@ -603,58 +603,67 @@ class ProtocolsController < ApplicationController end def protocolsio_import_create - @protocolsio_too_big = false - @protocolsio_invalid_file = false - @protocolsio_no_file = false - if params[:json_file].nil? - @protocolsio_no_file = true - respond_to do |format| - format.js {} + begin + @protocolsio_too_big = false + @protocolsio_invalid_file = false + @protocolsio_no_file = false + if params[:json_file].nil? + @protocolsio_no_file = true + respond_to do |format| + format.js {} + end + return 0 # return 0 stops the rest of the controller code from executing end - return 0 # return 0 stops the rest of the controller code from executing - end - extension = File.extname(params[:json_file].path) - file_size = File.size(params[:json_file].path) - if extension != '.txt' && extension != '.json' - @protocolsio_invalid_file = true - respond_to do |format| - format.js {} + extension = File.extname(params[:json_file].path) + file_size = File.size(params[:json_file].path) + if extension != '.txt' && extension != '.json' + @protocolsio_invalid_file = true + respond_to do |format| + format.js {} + end + return 0 # return 0 stops the rest of the controller code from executing end - return 0 # return 0 stops the rest of the controller code from executing - end - if file_size > Rails.configuration.x.file_max_size_mb.megabytes - @protocolsio_too_big = true - respond_to do |format| - format.js {} - # if file is too big, default to the js.erb file, - # named the same as this controller - # where a javascript alert is called + if file_size > Rails.configuration.x.file_max_size_mb.megabytes + @protocolsio_too_big = true + respond_to do |format| + format.js {} + # if file is too big, default to the js.erb file, + # named the same as this controller + # where a javascript alert is called + end + return 0 # return 0 stops the rest of the controller code from executing end - return 0 # return 0 stops the rest of the controller code from executing - end - json_file_contents = File.read(params[:json_file].path) - json_file_contents.gsub! '\"', "'" - # escaped double quotes too stressfull, html works with single quotes too - # json double quotes dont get escaped since they dont match \" - unless valid_protocol_json(json_file_contents) - @protocolsio_invalid_file = true - respond_to do |format| - format.js {} + json_file_contents = File.read(params[:json_file].path) + json_file_contents.gsub! '\"', "'" + # escaped double quotes too stressfull, html works with single quotes too + # json double quotes dont get escaped since they dont match \" + unless valid_protocol_json(json_file_contents) + @protocolsio_invalid_file = true + respond_to do |format| + format.js {} + end + return 0 # return 0 stops the rest of the controller code from executing + end + @json_object = JSON.parse(json_file_contents) + unless step_hash_null?(@json_object['steps']) + @json_object['steps'] = protocols_io_guid_reorder_step_json( + @json_object['steps'] + ) + end + @protocol = Protocol.new + respond_to do |format| + format.js {} # go to the js.erb file named the same as this controller, + # where a preview modal is rendered, + # and some modals get closed and opened + end + rescue => e + Rails.logger.error(e.message) + @protocolsio_general_error = true + respond_to do |format| + format.js {} # go to the js.erb file named the same as this controller, + # where a preview modal is rendered, + # and some modals get closed and opened end - return 0 # return 0 stops the rest of the controller code from executing - end - @json_object = JSON.parse(json_file_contents) - unless step_hash_null?(@json_object['steps']) - @json_object['steps'] = protocols_io_guid_reorder_step_json( - @json_object['steps'] - ) - end - - @protocol = Protocol.new - respond_to do |format| - format.js {} # go to the js.erb file named the same as this controller, - # where a preview modal is rendered, - # and some modals get closed and opened end end diff --git a/app/views/protocols/protocolsio_import_create.js.erb b/app/views/protocols/protocolsio_import_create.js.erb index 1f7e89acf..2936d6757 100644 --- a/app/views/protocols/protocolsio_import_create.js.erb +++ b/app/views/protocols/protocolsio_import_create.js.erb @@ -5,6 +5,8 @@ $('#modal-import-json-protocol').on('hidden.bs.modal', function () { $('#modal-import-json-protocol').modal('hide'); HelperModule.flashAlertMsg(I18n.t('my_modules.protocols.load_from_file_size_error', { size: $(document.body).data('file-max-size-mb') ) }, 'danger'); +<% elsif @protocolsio_general_error %> + $('#pio_no_file_error_span').addClass('has-error').html(' <%= t('protocols.import_export.load_file_error') %>'); <% elsif @protocolsio_invalid_file %> $('#modal-import-json-protocol').modal('hide'); HelperModule.flashAlertMsg(' <%= t('my_modules.protocols.load_from_file_invalid_error') %>','danger');