FIX: better chat-api error handling (#19550)

- chat-message is not using chat-api yet and the `jsonMode` shouldn't have been added
- correctly error on `getChannel` not found
- adds/correct relevant system tests
This commit is contained in:
Joffrey JAFFEUX 2022-12-21 16:11:35 +01:00 committed by GitHub
parent 4f04a51e17
commit c15b4212b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 12 deletions

View File

@ -1,8 +1,6 @@
import RESTAdapter from "discourse/adapters/rest"; import RESTAdapter from "discourse/adapters/rest";
export default class ChatMessage extends RESTAdapter { export default class ChatMessage extends RESTAdapter {
jsonMode = true;
pathFor(store, type, findArgs) { pathFor(store, type, findArgs) {
if (findArgs.targetMessageId) { if (findArgs.targetMessageId) {
return `/chat/lookup/${findArgs.targetMessageId}.json?chat_channel_id=${findArgs.channelId}`; return `/chat/lookup/${findArgs.targetMessageId}.json?chat_channel_id=${findArgs.channelId}`;

View File

@ -3,6 +3,7 @@ import Promise from "rsvp";
import ChatChannel from "discourse/plugins/chat/discourse/models/chat-channel"; import ChatChannel from "discourse/plugins/chat/discourse/models/chat-channel";
import { tracked } from "@glimmer/tracking"; import { tracked } from "@glimmer/tracking";
import { TrackedObject } from "@ember-compat/tracked-built-ins"; import { TrackedObject } from "@ember-compat/tracked-built-ins";
import { popupAjaxError } from "discourse/lib/ajax-error";
const DIRECT_MESSAGE_CHANNELS_LIMIT = 20; const DIRECT_MESSAGE_CHANNELS_LIMIT = 20;
@ -105,10 +106,13 @@ export default class ChatChannelsManager extends Service {
} }
async #find(id) { async #find(id) {
return this.chatApi.getChannel(id).then((channel) => { return this.chatApi
this.#cache(channel); .getChannel(id)
return channel; .catch(popupAjaxError)
}); .then((channel) => {
this.#cache(channel);
return channel;
});
} }
#cache(channel) { #cache(channel) {

View File

@ -60,27 +60,35 @@ RSpec.describe "Visit channel", type: :system, js: true do
end end
context "when channel is not found" do context "when channel is not found" do
it "shows a not found page" do it "shows an error" do
visit("/chat/channel/999/-") visit("/chat/channel/999/-")
expect(page).to have_current_path("/404") expect(page).to have_content("Not Found") # this is not a translated key
end
end
context "when loading a non existing message of a channel" do
it "shows an error" do
visit("/chat/channel/#{category_channel_1.id}/-?messageId=-999")
expect(page).to have_content(I18n.t("not_found"))
end end
end end
context "when channel is not accessible" do context "when channel is not accessible" do
context "when category channel" do context "when category channel" do
it "shows a forbidden page" do it "shows an error" do
chat.visit_channel(private_category_channel_1) chat.visit_channel(private_category_channel_1)
expect(page).to have_content(I18n.t("js.errors.reasons.forbidden")) expect(page).to have_content(I18n.t("invalid_access"))
end end
end end
context "when direct message channel" do context "when direct message channel" do
it "shows a forbidden page" do it "shows an error" do
chat.visit_channel(inaccessible_dm_channel_1) chat.visit_channel(inaccessible_dm_channel_1)
expect(page).to have_content(I18n.t("js.errors.reasons.forbidden")) expect(page).to have_content(I18n.t("invalid_access"))
end end
end end
end end