Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Add shared helpers for rate limited responses

Régis Hanoldb909a73805e
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 5c22dccabab..3a749e58ea3 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -515,6 +515,9 @@ en:
generic_error: "Sorry, an error has occurred."
generic_error_with_reason: "An error occurred: %{error}"
multiple_errors: "Multiple errors occurred: %{errors}"
+ too_many_requests:
+ one: "You’ve performed this action too many times. Please wait %{count} second before trying again."
+ other: "You’ve performed this action too many times. Please wait %{count} seconds before trying again."
sign_up: "Sign Up"
log_in: "Log In"
age: "Age"
diff --git a/frontend/discourse/app/lib/ajax-error.js b/frontend/discourse/app/lib/ajax-error.js
index 935b6e37d5a..08278a167a1 100644
--- a/frontend/discourse/app/lib/ajax-error.js
+++ b/frontend/discourse/app/lib/ajax-error.js
@@ -2,6 +2,10 @@ import { trustHTML } from "@ember/template";
import { getOwnerWithFallback } from "discourse/lib/get-owner";
import { i18n } from "discourse-i18n";
+export const TOO_MANY_REQUESTS = 429;
+const DEFAULT_RATE_LIMIT_WAIT_SECONDS = 15;
+export const MAX_RATE_LIMIT_RETRY_SECONDS = 60;
+
export function extractErrorInfo(
error,
defaultMessage,
@@ -35,7 +39,10 @@ export function extractErrorInfo(
parsedJSON = error.responseJSON;
}
- if (!parsedJSON && error.responseText) {
+ const contentType = error.getResponseHeader?.("Content-Type");
+ const isJSON = !contentType || contentType.includes("json");
+
+ if (!parsedJSON && isJSON && error.responseText) {
try {
parsedJSON = JSON.parse(error.responseText);
} catch (ex) {
@@ -69,6 +76,12 @@ export function extractErrorInfo(
}
}
+ if (!parsedError && isRateLimitError(error)) {
+ parsedError = i18n("too_many_requests", {
+ count: rateLimitWaitSeconds(error),
+ });
+ }
+
if (!parsedError) {
if (error.status && error.status >= 400) {
parsedError = error.status + " " + error.statusText;
@@ -87,11 +100,35 @@ export function extractError(error, defaultMessage) {
return extractErrorInfo(error, defaultMessage).message;
}
+function responseFor(error) {
+ return error.jqXHR ?? error.source ?? error;
+}
+
export function isReadOnlyError(error) {
- const xhr = error.jqXHR ?? error;
+ const xhr = responseFor(error);
return xhr.status === 503 && xhr.responseJSON?.error_type === "read_only";
}
+export function isRateLimitError(error) {
+ return responseFor(error).status === TOO_MANY_REQUESTS;
+}
+
+export function rateLimitWaitSeconds(error) {
+ const xhr = responseFor(error);
+
+ const retryAfter = parseInt(xhr.getResponseHeader?.("Retry-After"), 10);
+ if (retryAfter > 0) {
+ return retryAfter;
+ }
+
+ const waitSeconds = xhr.responseJSON?.extras?.wait_seconds;
+ if (waitSeconds > 0) {
+ return waitSeconds;
+ }
+
+ return DEFAULT_RATE_LIMIT_WAIT_SECONDS;
+}
+
export function throwAjaxError(undoCallback, defaultMessage) {
return function (error) {
// If we provided an `undo` callback
diff --git a/frontend/discourse/tests/helpers/create-pretender.js b/frontend/discourse/tests/helpers/create-pretender.js
index d15281848d1..ef4e92ee46f 100644
--- a/frontend/discourse/tests/helpers/create-pretender.js
+++ b/frontend/discourse/tests/helpers/create-pretender.js
@@ -1,5 +1,6 @@
import EmberObject from "@ember/object";
import Pretender from "pretender";
+import { TOO_MANY_REQUESTS } from "discourse/lib/ajax-error";
import getURL from "discourse/lib/get-url";
import { cloneJSON } from "discourse/lib/object";
import User from "discourse/models/user";
@@ -51,6 +52,45 @@ export function OK(resp = {}, headers = {}) {
return [200, headers, resp];
}
+export { TOO_MANY_REQUESTS };
+
+export function middlewareRateLimit(
+ retryAfterSeconds = 10,
+ errorCode = "user_10_secs_limit"
+) {
+ const headers = { "Content-Type": "text/plain" };
+
+ if (retryAfterSeconds !== null) {
+ headers["Retry-After"] = String(retryAfterSeconds);
+ headers["Discourse-Rate-Limit-Error-Code"] = errorCode;
+ }
+
+ return [
+ TOO_MANY_REQUESTS,
+ headers,
+ "Slow down, you're making too many requests.\n" +
+ `Please retry again in ${retryAfterSeconds} seconds.\n` +
+ `Error code: ${errorCode}.\n`,
+ ];
+}
+
+export function controllerRateLimit(waitSeconds = 10) {
+ const headers = {
+ "Content-Type": "application/json",
+ "Retry-After": String(waitSeconds),
+ };
+
+ return [
+ TOO_MANY_REQUESTS,
+ headers,
+ {
+ errors: ["You've performed this action too many times."],
+ error_type: "rate_limit",
+ extras: { wait_seconds: waitSeconds },
+ },
+ ];
+}
+
const loggedIn = () => !!User.current();
const helpers = { response, success, parsePostData };
diff --git a/frontend/discourse/tests/unit/lib/ajax-error-test.js b/frontend/discourse/tests/unit/lib/ajax-error-test.js
new file mode 100644
index 00000000000..38c723027ff
--- /dev/null
+++ b/frontend/discourse/tests/unit/lib/ajax-error-test.js
@@ -0,0 +1,158 @@
+import { setupTest } from "ember-qunit";
+import { module, test } from "qunit";
+import sinon from "sinon";
+import { ajax } from "discourse/lib/ajax";
+import {
+ extractError,
+ isRateLimitError,
+ rateLimitWaitSeconds,
+} from "discourse/lib/ajax-error";
+import getURL from "discourse/lib/get-url";
+import pretender, {
+ middlewareRateLimit,
+ TOO_MANY_REQUESTS,
+} from "discourse/tests/helpers/create-pretender";
+import { i18n } from "discourse-i18n";
+
+const INTERNAL_SERVER_ERROR = 500;
+const RETRY_AFTER_SECONDS = 12;
+const WAIT_SECONDS = 300;
+const DEFAULT_WAIT_SECONDS = 15;
+
+const SERVER_MESSAGE =
+ "You’ve performed this action too many times. Please wait 5 minutes before trying again.";
+
+function controllerRateLimit() {
+ return [
+ TOO_MANY_REQUESTS,
+ { "Content-Type": "application/json", "Retry-After": String(WAIT_SECONDS) },
+ {
+ errors: [SERVER_MESSAGE],
+ error_type: "rate_limit",
+ extras: { wait_seconds: WAIT_SECONDS, time_left: "5 minutes" },
+ },
+ ];
+}
+
+function ajaxError(url) {
+ return ajax(url).then(
+ () => null,
+ (error) => error
+ );
+}
+
+function rawXHR(url) {
+ return new Promise((resolve) => {
+ const xhr = new XMLHttpRequest();
+ xhr.open("GET", getURL(url));
+ xhr.onloadend = () => resolve(xhr);
+ xhr.send();
+ });
+}
+
+module("Unit | Lib | ajax-error", function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ pretender.get("/middleware-limit", () =>
+ middlewareRateLimit(RETRY_AFTER_SECONDS)
+ );
+ pretender.get("/controller-limit", controllerRateLimit);
+ });
+
+ test("isRateLimitError detects both response shapes", async function (assert) {
+ pretender.get("/server-error", () => [
+ INTERNAL_SERVER_ERROR,
+ { "Content-Type": "application/json" },
+ { errors: ["boom"] },
+ ]);
+
+ assert.true(
+ isRateLimitError(await ajaxError("/middleware-limit")),
+ "detects the middleware rate limit from a jqXHR"
+ );
+ assert.true(
+ isRateLimitError(await rawXHR("/middleware-limit")),
+ "detects the middleware rate limit from a raw XHR"
+ );
+ assert.true(
+ isRateLimitError({ source: await rawXHR("/middleware-limit") }),
+ "detects the middleware rate limit from an XHR wrapped in source"
+ );
+ assert.true(
+ isRateLimitError(await ajaxError("/controller-limit")),
+ "detects the controller rate limit"
+ );
+ assert.false(
+ isRateLimitError(await ajaxError("/server-error")),
+ "ignores other errors"
+ );
+ });
+
+ test("rateLimitWaitSeconds reads the Retry-After header", async function (assert) {
+ assert.strictEqual(
+ rateLimitWaitSeconds(await ajaxError("/middleware-limit")),
+ RETRY_AFTER_SECONDS,
+ "reads the header from a jqXHR"
+ );
+ assert.strictEqual(
+ rateLimitWaitSeconds(await rawXHR("/middleware-limit")),
+ RETRY_AFTER_SECONDS,
+ "reads the header from a raw XHR"
+ );
+ assert.strictEqual(
+ rateLimitWaitSeconds({ source: await rawXHR("/middleware-limit") }),
+ RETRY_AFTER_SECONDS,
+ "reads the header from an XHR wrapped in source"
+ );
+ });
+
+ test("rateLimitWaitSeconds falls back to extras.wait_seconds", async function (assert) {
+ pretender.get("/controller-limit", () => [
+ TOO_MANY_REQUESTS,
+ { "Content-Type": "application/json" },
+ { errors: [SERVER_MESSAGE], extras: { wait_seconds: WAIT_SECONDS } },
+ ]);
+
+ assert.strictEqual(
+ rateLimitWaitSeconds(await ajaxError("/controller-limit")),
+ WAIT_SECONDS,
+ "uses the wait time advertised in the body"
+ );
+ });
+
+ test("rateLimitWaitSeconds defaults when no wait time is advertised", async function (assert) {
+ pretender.get("/bare-limit", () => middlewareRateLimit(null));
+
+ assert.strictEqual(
+ rateLimitWaitSeconds(await ajaxError("/bare-limit")),
+ DEFAULT_WAIT_SECONDS,
+ "falls back to the default wait time"
+ );
+ });
+
+ test("extractError translates a plain text rate limit", async function (assert) {
+ const error = await ajaxError("/middleware-limit");
+ const consoleError = sinon.stub(console, "error");
+
+ const message = extractError(error);
+
+ assert.strictEqual(
+ message,
+ i18n("too_many_requests", { count: RETRY_AFTER_SECONDS }),
+ "returns a translated message mentioning the wait time"
+ );
+ assert.false(
+ consoleError.called,
+ "does not report a JSON parse failure for a plain text body"
+ );
+ });
+
+ test("extractError keeps the server message for a JSON rate limit", async function (assert) {
+ assert.strictEqual(
+ extractError(await ajaxError("/controller-limit")),
+ i18n("generic_error_with_reason", { error: SERVER_MESSAGE }),
+ "prefers the localized message from the server"
+ );
+ });
+});