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 end
def protocolsio_import_create def protocolsio_import_create
@protocolsio_too_big = false begin
@protocolsio_invalid_file = false @protocolsio_too_big = false
@protocolsio_no_file = false @protocolsio_invalid_file = false
if params[:json_file].nil? @protocolsio_no_file = false
@protocolsio_no_file = true if params[:json_file].nil?
respond_to do |format| @protocolsio_no_file = true
format.js {} respond_to do |format|
format.js {}
end
return 0 # return 0 stops the rest of the controller code from executing
end end
return 0 # return 0 stops the rest of the controller code from executing extension = File.extname(params[:json_file].path)
end file_size = File.size(params[:json_file].path)
extension = File.extname(params[:json_file].path) if extension != '.txt' && extension != '.json'
file_size = File.size(params[:json_file].path) @protocolsio_invalid_file = true
if extension != '.txt' && extension != '.json' respond_to do |format|
@protocolsio_invalid_file = true format.js {}
respond_to do |format| end
format.js {} return 0 # return 0 stops the rest of the controller code from executing
end end
return 0 # return 0 stops the rest of the controller code from executing if file_size > Rails.configuration.x.file_max_size_mb.megabytes
end @protocolsio_too_big = true
if file_size > Rails.configuration.x.file_max_size_mb.megabytes respond_to do |format|
@protocolsio_too_big = true format.js {}
respond_to do |format| # if file is too big, default to the js.erb file,
format.js {} # named the same as this controller
# if file is too big, default to the js.erb file, # where a javascript alert is called
# named the same as this controller end
# where a javascript alert is called return 0 # return 0 stops the rest of the controller code from executing
end end
return 0 # return 0 stops the rest of the controller code from executing json_file_contents = File.read(params[:json_file].path)
end json_file_contents.gsub! '\"', "'"
json_file_contents = File.read(params[:json_file].path) # escaped double quotes too stressfull, html works with single quotes too
json_file_contents.gsub! '\"', "'" # json double quotes dont get escaped since they dont match \"
# escaped double quotes too stressfull, html works with single quotes too unless valid_protocol_json(json_file_contents)
# json double quotes dont get escaped since they dont match \" @protocolsio_invalid_file = true
unless valid_protocol_json(json_file_contents) respond_to do |format|
@protocolsio_invalid_file = true format.js {}
respond_to do |format| end
format.js {} 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 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
end end

View file

@ -5,6 +5,8 @@ $('#modal-import-json-protocol').on('hidden.bs.modal', function () {
$('#modal-import-json-protocol').modal('hide'); $('#modal-import-json-protocol').modal('hide');
HelperModule.flashAlertMsg(I18n.t('my_modules.protocols.load_from_file_size_error', HelperModule.flashAlertMsg(I18n.t('my_modules.protocols.load_from_file_size_error',
{ size: $(document.body).data('file-max-size-mb') ) }, 'danger'); { 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 %> <% elsif @protocolsio_invalid_file %>
$('#modal-import-json-protocol').modal('hide'); $('#modal-import-json-protocol').modal('hide');
HelperModule.flashAlertMsg(' <%= t('my_modules.protocols.load_from_file_invalid_error') %>','danger'); HelperModule.flashAlertMsg(' <%= t('my_modules.protocols.load_from_file_invalid_error') %>','danger');