Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 5cfd9fc

Browse files
committed
DEV: Improve error handling and add spec
1 parent 2c99aa0 commit 5cfd9fc

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

app/controllers/discourse_ai/admin/ai_spam_controller.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,28 @@ def test
8787
end
8888

8989
def fix_errors
90-
if params[:error] == "spam_scanner_not_admin"
91-
DiscourseAi::AiModeration::SpamScanner.fix_spam_scanner_not_admin
90+
case params[:error]
91+
when "spam_scanner_not_admin"
92+
begin
93+
DiscourseAi::AiModeration::SpamScanner.fix_spam_scanner_not_admin
94+
render json: success_json
95+
rescue ActiveRecord::RecordInvalid
96+
render_json_error(
97+
I18n.t("discourse_ai.spam_detection.bot_user_update_failed"),
98+
status: :unprocessable_entity,
99+
)
100+
rescue StandardError
101+
render_json_error(
102+
I18n.t("discourse_ai.spam_detection.unexpected"),
103+
status: :internal_server_error,
104+
)
105+
end
106+
else
107+
render_json_error(
108+
I18n.t("discourse_ai.spam_detection.invalid_error_type"),
109+
status: :bad_request,
110+
)
92111
end
93-
94-
render json: success_json
95112
end
96113

97114
private

config/locales/server.en.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ en:
255255
spam_detection:
256256
flag_reason: "Flagged as spam by <a href='%{url}'>Discourse AI</a>"
257257
silence_reason: "User silenced automatically by <a href='%{url}'>Discourse AI</a>"
258+
invalid_error_type: "Invalid error type provided"
259+
unexpected: "An unexpected error occured"
260+
bot_user_update_failed: "Failed to update the spam scanning bot user"
261+
258262
ai_bot:
259263
reply_error: "Sorry, it looks like our system encountered an unexpected issue while trying to reply.\n\n[details='Error details']\n%{details}\n[/details]"
260264
default_pm_prefix: "[Untitled AI bot PM]"

lib/ai_moderation/spam_scanner.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,12 @@ def self.perform_scan(post)
243243

244244
def self.fix_spam_scanner_not_admin
245245
user = DiscourseAi::AiModeration::SpamScanner.flagging_user
246-
user.update!(admin: true) if user.present?
246+
247+
if user.present?
248+
user.update!(admin: true)
249+
else
250+
raise Discourse::NotFound
251+
end
247252
end
248253

249254
private

spec/requests/admin/ai_spam_controller_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,49 @@
306306
end
307307
end
308308
end
309+
310+
describe "#fix_errors" do
311+
fab!(:setting) do
312+
AiModerationSetting.create(
313+
{
314+
setting_type: :spam,
315+
llm_model_id: llm_model.id,
316+
data: {
317+
custom_instructions: "custom instructions",
318+
},
319+
},
320+
)
321+
fab!(:llm_model)
322+
323+
before do
324+
sign_in(admin)
325+
DiscourseAi::AiModeration::SpamScanner.flagging_user.update!(admin: false)
326+
end
327+
328+
it "resolves spam scanner not admin error" do
329+
post "/admin/plugins/discourse-ai/ai-spam/fix-errors",
330+
params: {
331+
error: "spam_scanner_not_admin",
332+
}
333+
334+
expect(response.status).to eq(200)
335+
expect(DiscourseAi::AiModeration::SpamScanner.flagging_user.reload.admin).to eq(true)
336+
end
337+
338+
it "returns an error when it can't update the user" do
339+
DiscourseAi::AiModeration::SpamScanner.flagging_user.destroy
340+
341+
post "/admin/plugins/discourse-ai/ai-spam/fix-errors",
342+
params: {
343+
error: "spam_scanner_not_admin",
344+
}
345+
346+
expect(response.status).to eq(422)
347+
expect(response.parsed_body["errors"]).to be_present
348+
expect(response.parsed_body["errors"].first).to eq(
349+
I18n.t("discourse_ai.spam_detection.bot_user_update_failed"),
350+
)
351+
end
352+
end
353+
end
309354
end

0 commit comments

Comments
 (0)