Skip to content

Commit

Permalink
Merge pull request #2319 from mamhoff/raise-on-wrong-locale
Browse files Browse the repository at this point in the history
Raise on non-existing locale
  • Loading branch information
tvdeyen committed Apr 26, 2022
1 parent facf2ba commit 3438d0e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
7 changes: 4 additions & 3 deletions app/controllers/alchemy/api/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ class Api::BaseController < Alchemy::BaseController
layout false
respond_to :json

rescue_from CanCan::AccessDenied, with: :render_not_authorized
rescue_from CanCan::AccessDenied, with: :render_not_authorized
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found
rescue_from ActionController::RoutingError, with: :render_not_found

private

def render_not_authorized
render json: {error: "Not authorized"}, status: 403
render json: { error: "Not authorized" }, status: 403
end

def render_not_found
render json: {error: "Record not found"}, status: 404
render json: { error: "Record not found" }, status: 404
end
end
end
3 changes: 2 additions & 1 deletion lib/alchemy/controller_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ def set_alchemy_language(lang = nil)

def load_alchemy_language_from_params
if params[:locale].present?
Language.find_by_code(params[:locale])
Language.find_by_code(params[:locale]) ||
raise(ActionController::RoutingError, "Language not found")
end
end

Expand Down
9 changes: 4 additions & 5 deletions spec/libraries/controller_actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,12 @@
context "for language that does not exist" do
before do
allow(controller).to receive(:params).and_return(locale: "fo")
controller.send :set_alchemy_language
end

it "should set the language to default" do
expect(assigns(:language)).to eq(default_language)
expect(Alchemy::Language.current).to eq(default_language)
expect(controller.session).to include_language_information_for(default_language)
it "should raise an error" do
expect do
controller.send :set_alchemy_language
end.to raise_exception(ActionController::RoutingError, "Language not found")
end
end
end
Expand Down

0 comments on commit 3438d0e

Please sign in to comment.