ui_report: Reuse `channel.xhr_error_message` function in `error` method.

We should reuse the `channel.xhr_error_message` in `ui_report` to reduce
code duplication.

Also, I changed the type of `message` parameter to `string` instead of
`string | null` so that we do not need to alter the types of the functions
that depends on the return value of `xhr_error_message`.
This commit is contained in:
Lalit Kumar Singh 2023-08-30 15:38:32 +05:30 committed by Tim Abbott
parent 2e07c03968
commit cfe2ddb091
4 changed files with 6 additions and 19 deletions

View File

@ -201,10 +201,7 @@ export function patch(
return post(options);
}
export function xhr_error_message(
message: string | null,
xhr: JQuery.jqXHR<unknown>,
): string | null {
export function xhr_error_message(message: string, xhr: JQuery.jqXHR<unknown>): string {
if (xhr.status.toString().charAt(0) === "4" && xhr.responseJSON?.msg) {
// Only display the error response for 4XX, where we've crafted
// a nice response.

View File

@ -1032,7 +1032,7 @@ export function save_message_row_edit($row) {
}
hide_message_edit_spinner($row);
const message = channel.xhr_error_message(null, xhr);
const message = channel.xhr_error_message("", xhr);
const $container = compose_banner.get_compose_banner_container(
$row.find("textarea"),
);

View File

@ -1,6 +1,6 @@
import $ from "jquery";
import _ from "lodash";
import * as channel from "./channel";
import * as common from "./common";
import {$t} from "./i18n";
@ -38,18 +38,8 @@ export function error(
status_box: JQuery,
remove_after?: number,
): void {
if (xhr && xhr.status >= 400 && xhr.status < 500 && xhr.responseJSON?.msg) {
// Only display the error response for 4XX, where we've crafted
// a nice response.
const server_response_html = _.escape(xhr.responseJSON.msg);
if (response_html) {
response_html += ": " + server_response_html;
} else {
response_html = server_response_html;
}
}
message(response_html, status_box, "alert-error", remove_after);
const msg = xhr ? channel.xhr_error_message(response_html, xhr) : response_html;
message(msg, status_box, "alert-error", remove_after);
}
export function client_error(

View File

@ -348,7 +348,7 @@ test("xhr_error_message", () => {
msg = "some message";
assert.equal(channel.xhr_error_message(msg, xhr), "some message: file not found");
msg = null;
msg = "";
assert.equal(channel.xhr_error_message(msg, xhr), "file not found");
});