Add error handling to actions in protocol controller [SCI-8073] (#5074)

This commit is contained in:
aignatov-bio 2023-03-03 15:33:08 +01:00 committed by GitHub
parent d1f26e1c7b
commit da31883ac3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 4 deletions

View file

@ -425,13 +425,16 @@ var ProtocolsIndex = (function() {
}
function initdeleteDraftModal() {
$('.protocols-index').on('click', '#protocol-versions-modal .delete-draft', function(e) {
$('.protocols-index').on('click', '#protocol-versions-modal .delete-draft', function() {
let url = this.dataset.url;
let modal = $('#deleteDraftModal');
$('#protocol-versions-modal').modal('hide');
modal.modal('show');
modal.find('form').attr('action', url);
});
$('#deleteDraftModal form').on('ajax:error', function(_ev, data) {
HelperModule.flashAlertMsg(data.responseJSON.message, 'danger');
});
}
function initProtocolsioModal() {

View file

@ -177,6 +177,11 @@ class ProtocolsController < ApplicationController
protocol: @protocol.id,
version_number: @protocol.version_number)
rescue ActiveRecord::RecordInvalid => e
flash[:error] = e.message
Rails.logger.error e.message
raise ActiveRecord::Rollback
rescue StandardError => e
flash[:error] = I18n.t('errors.general')
Rails.logger.error e.message
raise ActiveRecord::Rollback
end
@ -199,7 +204,11 @@ class ProtocolsController < ApplicationController
redirect_to protocols_path
rescue ActiveRecord::RecordNotDestroyed => e
Rails.logger.error e.message
render json: { status: 'error' }, status: :unprocessable_entity
render json: { message: e.message }, status: :unprocessable_entity
raise ActiveRecord::Rollback
rescue StandardError => e
render json: { message: I18n.t('errors.general') }, status: :unprocessable_entity
Rails.logger.error e.message
raise ActiveRecord::Rollback
end
end
@ -237,7 +246,7 @@ class ProtocolsController < ApplicationController
render json: @protocol, serializer: ProtocolSerializer, user: current_user
end
else
format.json { render json: {}, status: :unprocessable_entity }
format.json { render json: I18n.t('errors.general'), status: :unprocessable_entity }
end
end
end
@ -388,6 +397,8 @@ class ProtocolsController < ApplicationController
rescue StandardError => e
Rails.logger.error(e.message)
Rails.logger.error(e.backtrace.join("\n"))
flash[:error] = I18n.t('errors.general')
redirect_to protocols_path
raise ActiveRecord::Rollback
end
end
@ -1105,7 +1116,7 @@ class ProtocolsController < ApplicationController
respond_to do |format|
if rollbacked
format.json do
render json: {}, status: :bad_request
render json: { message: I18n.t('errors.general') }, status: :unprocessable_entity
end
else
format.json do

View file

@ -20,6 +20,10 @@ window.initProtocolComponent = () => {
modal.find('form').attr('action', url);
});
$('#deleteDraftModal form').on('ajax:error', function(_ev, data) {
HelperModule.flashAlertMsg(data.responseJSON.message, 'danger');
});
new Vue({
el: '#protocolContainer',
components: {