forked from fixin.me/fixin.me
Handle exceptions during TURBO_STREAM requests
This commit is contained in:
parent
93929f2c07
commit
9a9a139aa7
@ -3,8 +3,19 @@ class ApplicationController < ActionController::Base
|
|||||||
|
|
||||||
before_action :authenticate_user!
|
before_action :authenticate_user!
|
||||||
|
|
||||||
class AccessForbidden < StandardError
|
class AccessForbidden < StandardError; end
|
||||||
end
|
class ParameterInvalid < StandardError; end
|
||||||
|
|
||||||
|
# Exceptions are handled depending on request format:
|
||||||
|
# * HTML is handled by PublicExceptions, resulting in display of
|
||||||
|
# 'public/<status-code>.html' template.
|
||||||
|
# * TURBO_STREAM is handled by method specified below, which writes flash
|
||||||
|
# message and forces redirect to referer - to display flash and make page
|
||||||
|
# content consistent with database (which may or may not have been
|
||||||
|
# modified before exception).
|
||||||
|
# This requires referer to be available in TURBO_STREAM format. Otherwise
|
||||||
|
# Turbo will reload 2nd time with HTML format and flashes will be lost.
|
||||||
|
rescue_from *ActionDispatch::ExceptionWrapper.rescue_responses.keys, with: :rescue_turbo
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
@ -23,4 +34,14 @@ class ApplicationController < ActionController::Base
|
|||||||
def after_sign_out_path_for(scope)
|
def after_sign_out_path_for(scope)
|
||||||
new_user_session_path
|
new_user_session_path
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def rescue_turbo(exception)
|
||||||
|
raise unless request.format.to_sym == :turbo_stream
|
||||||
|
|
||||||
|
message_id = ActionDispatch::ExceptionWrapper.rescue_responses[exception.class.to_s]
|
||||||
|
flash.alert = t("actioncontroller.exceptions.status.#{message_id}")
|
||||||
|
redirect_to request.referer
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -17,12 +17,12 @@ class UsersController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
raise ArgumentError if current_user == @user
|
raise ParameterInvalid if current_user == @user
|
||||||
@user.update!(params.require(:user).permit(:status))
|
@user.update!(params.require(:user).permit(:status))
|
||||||
end
|
end
|
||||||
|
|
||||||
def disguise
|
def disguise
|
||||||
raise ArgumentError unless allow_disguise?(@user)
|
raise ParameterInvalid unless allow_disguise?(@user)
|
||||||
session[:revert_to_id] = current_user.id
|
session[:revert_to_id] = current_user.id
|
||||||
bypass_sign_in(@user)
|
bypass_sign_in(@user)
|
||||||
redirect_to root_url
|
redirect_to root_url
|
||||||
@ -35,8 +35,8 @@ class UsersController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
# NOTE: limited actions availabe to :admin by design. Users are meant to
|
# NOTE: limited actions availabe to :admin by design. Users are meant to
|
||||||
# manage their accounts by themselves through registrations. In future :admin
|
# manage their accounts by themselves through registrations. :admin
|
||||||
# may be allowed to sing-in as user and make changes there.
|
# is allowed to sign-in (disguise) as user and make changes from there.
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
|
@ -13,10 +13,25 @@ en:
|
|||||||
created_at: registered
|
created_at: registered
|
||||||
confirmed_at: confirmed
|
confirmed_at: confirmed
|
||||||
unconfirmed_email: Awaiting confirmation for
|
unconfirmed_email: Awaiting confirmation for
|
||||||
|
actioncontroller:
|
||||||
|
exceptions:
|
||||||
|
status:
|
||||||
|
forbidden: >
|
||||||
|
You have not been granted access to this action (403 Forbidden).
|
||||||
|
This should not happen, please notify site administrator.
|
||||||
|
unprocessable_entity: >
|
||||||
|
The request is semantically incorrect and was rejected (422 Unprocessable Entity).
|
||||||
|
This should not happen, please notify site administrator.
|
||||||
helpers:
|
helpers:
|
||||||
submit:
|
submit:
|
||||||
create: Create
|
create: Create
|
||||||
update: Update
|
update: Update
|
||||||
|
layouts:
|
||||||
|
application:
|
||||||
|
revert: Revert
|
||||||
|
sign_out: Sign out
|
||||||
|
units: Units
|
||||||
|
users: Users
|
||||||
units:
|
units:
|
||||||
index:
|
index:
|
||||||
add_unit: Add unit
|
add_unit: Add unit
|
||||||
@ -53,12 +68,6 @@ en:
|
|||||||
sessions:
|
sessions:
|
||||||
new:
|
new:
|
||||||
remember_me: Remember me
|
remember_me: Remember me
|
||||||
layouts:
|
|
||||||
application:
|
|
||||||
revert: Revert
|
|
||||||
sign_out: Sign out
|
|
||||||
units: Units
|
|
||||||
users: Users
|
|
||||||
actions: Actions
|
actions: Actions
|
||||||
add: Add
|
add: Add
|
||||||
back: Back
|
back: Back
|
||||||
|
Loading…
x
Reference in New Issue
Block a user