Catch all errors on PIO import and show message

This commit is contained in:
Jure Grabnar 2018-11-23 20:47:48 +01:00
parent 2834edb286
commit 1c7a051551
2 changed files with 59 additions and 48 deletions

View file

@ -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

View file

@ -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('<span class="help-block"> <%= t('protocols.import_export.load_file_error') %></span>');
<% elsif @protocolsio_invalid_file %>
$('#modal-import-json-protocol').modal('hide');
HelperModule.flashAlertMsg(' <%= t('my_modules.protocols.load_from_file_invalid_error') %>','danger');