The personal finance app for everyone (by everyone)
Ruby
fix(messages): handle chat not found during message creation (#2896)
sentry[bot]521946b9d659
diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rbindex 71c2107f31..252d6645cf 100644--- a/app/controllers/messages_controller.rb+++ b/app/controllers/messages_controller.rb@@ -15,6 +15,8 @@ class MessagesController < ApplicationControllerelseredirect_to chat_path(@chat), alert: @message.errors.full_messages.to_sentenceend+ rescue ActiveRecord::InvalidForeignKey+ redirect_to chats_path, alert: t(".chat_not_found")end# Called by the chat watchdog when an assistant "Thinking…" bubble has waiteddiff --git a/config/locales/views/messages/en.yml b/config/locales/views/messages/en.ymlindex dee3765681..c0c6603eda 100644--- a/config/locales/views/messages/en.yml+++ b/config/locales/views/messages/en.yml@@ -1,6 +1,8 @@---en:messages:+ create:+ chat_not_found: This chat is no longer available.chat_form:placeholder: "Ask anything ..."disclaimer: "AI responses are informational only. Not financial advice!"diff --git a/test/controllers/messages_controller_test.rb b/test/controllers/messages_controller_test.rbindex d97fd3f961..6fdc425a4c 100644--- a/test/controllers/messages_controller_test.rb+++ b/test/controllers/messages_controller_test.rb@@ -12,6 +12,15 @@ class MessagesControllerTest < ActionDispatch::IntegrationTestassert_redirected_to chat_path(@chat, thinking: true)end+ test "redirects to chats when message save races with chat deletion" do+ UserMessage.any_instance.stubs(:save).raises(ActiveRecord::InvalidForeignKey)++ post chat_messages_url(@chat), params: { message: { content: "Hello", ai_model: "gpt-4.1" } }++ assert_redirected_to chats_path+ assert_equal I18n.t("messages.create.chat_not_found"), flash[:alert]+ end+test "cannot create a message if AI is disabled" do@user.update!(ai_enabled: false)