Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

core: handle 5xx http error codes & retry after (#10576)

Abdullah Atta740d826f26b0
diff --git a/packages/core/__tests__/http.test.ts b/packages/core/__tests__/http.test.ts
new file mode 100644
index 000000000..1011141d2
--- /dev/null
+++ b/packages/core/__tests__/http.test.ts
@@ -0,0 +1,149 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import { afterEach, expect, test, vi } from "vitest";
+import http, { RequestError } from "../src/utils/http.js";
+
+const URL = "https://api.example.com/users";
+
+function json(
+ status: number,
+ body: unknown,
+ headers: Record<string, string> = {}
+) {
+ return new Response(JSON.stringify(body), {
+ status,
+ headers: { "Content-Type": "application/json", ...headers }
+ });
+}
+
+afterEach(() => {
+ vi.unstubAllGlobals();
+ vi.useRealTimers();
+});
+
+test("a short Retry-After on a GET is waited out and retried once", async () => {
+ vi.useFakeTimers();
+ const fetch = vi
+ .fn()
+ .mockResolvedValueOnce(
+ json(
+ 503,
+ { error: "Service is temporarily unavailable." },
+ { "Retry-After": "2" }
+ )
+ )
+ .mockResolvedValueOnce(json(200, { id: "user" }));
+ vi.stubGlobal("fetch", fetch);
+
+ const result = http.get(URL);
+ await vi.advanceTimersByTimeAsync(1999);
+ expect(fetch).toHaveBeenCalledTimes(1);
+ await vi.advanceTimersByTimeAsync(1);
+
+ await expect(result).resolves.toEqual({ id: "user" });
+ expect(fetch).toHaveBeenCalledTimes(2);
+});
+
+test("a long Retry-After fails right away with the delay on the error", async () => {
+ const fetch = vi
+ .fn()
+ .mockResolvedValue(
+ json(
+ 503,
+ { error: "Service is temporarily unavailable." },
+ { "Retry-After": "30" }
+ )
+ );
+ vi.stubGlobal("fetch", fetch);
+
+ const error = await http.get(URL).catch((e) => e);
+
+ expect(error).toBeInstanceOf(RequestError);
+ expect(error.message).toBe("Service is temporarily unavailable.");
+ expect(error.status).toBe(503);
+ expect(error.retryAfter).toBe(30_000);
+ expect(fetch).toHaveBeenCalledTimes(1);
+});
+
+test("requests that change data are never retried", async () => {
+ const fetch = vi
+ .fn()
+ .mockResolvedValue(
+ json(
+ 503,
+ { error: "Service is temporarily unavailable." },
+ { "Retry-After": "1" }
+ )
+ );
+ vi.stubGlobal("fetch", fetch);
+
+ const error = await http.post(URL, { name: "x" }).catch((e) => e);
+
+ expect(error.retryAfter).toBe(1000);
+ expect(fetch).toHaveBeenCalledTimes(1);
+});
+
+test("an HTTP-date Retry-After is converted to a delay", async () => {
+ const date = new Date(Date.now() + 60_000).toUTCString();
+ vi.stubGlobal(
+ "fetch",
+ vi
+ .fn()
+ .mockResolvedValue(
+ new Response(null, { status: 429, headers: { "Retry-After": date } })
+ )
+ );
+
+ const error = await http.get(URL).catch((e) => e);
+
+ expect(error).toBeInstanceOf(RequestError);
+ expect(error.code).toBe("rate_limited");
+ expect(error.retryAfter).toBeGreaterThan(50_000);
+ expect(error.retryAfter).toBeLessThanOrEqual(60_000);
+});
+
+test("a 503 without a body has a readable message", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue(new Response(null, { status: 503 }))
+ );
+
+ const error = await http.get(URL).catch((e) => e);
+
+ expect(error).toBeInstanceOf(RequestError);
+ expect(error.message).toBe(
+ "Service is temporarily unavailable. Please try again later."
+ );
+ expect(error.status).toBe(503);
+ expect(error.retryAfter).toBeUndefined();
+});
+
+test("JSON errors keep their message and expose the status", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue(json(400, { error: "Wrong password." }))
+ );
+
+ const error = await http.post(URL, { password: "x" }).catch((e) => e);
+
+ expect(error.message).toBe("Wrong password.");
+ expect(error.status).toBe(400);
+ expect(error.retryAfter).toBeUndefined();
+});
diff --git a/packages/core/src/utils/http.ts b/packages/core/src/utils/http.ts
index c344e3729..5d41a0d23 100644
--- a/packages/core/src/utils/http.ts
+++ b/packages/core/src/utils/http.ts
@@ -81,13 +81,21 @@ export default {
patch
};
+const MAX_AUTO_RETRY_DELAY = 10_000;
+
async function request(url: string, method: "GET" | "DELETE", token?: string) {
- return handleResponse(
- await fetchWrapped(url, {
- method,
- headers: getHeaders(token)
- })
- );
+ const send = () => fetchWrapped(url, { method, headers: getHeaders(token) });
+ let response = await send();
+ const retryAfter = getRetryAfter(response);
+ if (
+ method === "GET" &&
+ retryAfter !== undefined &&
+ retryAfter <= MAX_AUTO_RETRY_DELAY
+ ) {
+ await new Promise((resolve) => setTimeout(resolve, retryAfter));
+ response = await send();
+ }
+ return handleResponse(response);
}
async function bodyRequest(
@@ -152,7 +160,11 @@ export function errorTransformer(errorJson: {
};
}
-async function fetchWrapped(input: string, init: RequestInit, timeoutMs = 30000) {
+async function fetchWrapped(
+ input: string,
+ init: RequestInit,
+ timeoutMs = 30000
+) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), timeoutMs);
try {
@@ -176,16 +188,33 @@ async function fetchWrapped(input: string, init: RequestInit, timeoutMs = 30000)
async function handleResponse(response: Response) {
try {
+ const status = response.status;
+ const retryAfter = getRetryAfter(response);
const contentType = response.headers.get("content-type");
if (contentType && contentType.includes("application/json")) {
const json = await response.json();
if (response.ok) {
return json;
}
- throw new RequestError(errorTransformer(json));
+ throw new RequestError({ ...errorTransformer(json), status, retryAfter });
} else {
- if (response.status === 429)
- throw new Error("You are being rate limited.");
+ if (status === 429)
+ throw new RequestError({
+ code: "rate_limited",
+ description: "You are being rate limited.",
+ data: undefined,
+ status,
+ retryAfter
+ });
+ if (status === 503)
+ throw new RequestError({
+ code: "service_unavailable",
+ description:
+ "Service is temporarily unavailable. Please try again later.",
+ data: undefined,
+ status,
+ retryAfter
+ });
if (response.ok) return await response.text();
else if (response.status === 401) {
@@ -210,13 +239,33 @@ async function handleResponse(response: Response) {
export class RequestError extends Error {
code: string;
data: unknown;
- constructor(error: { code: string; data: unknown; description: string }) {
+ status?: number;
+ retryAfter?: number;
+ constructor(error: {
+ code: string;
+ data: unknown;
+ description: string;
+ status?: number;
+ retryAfter?: number;
+ }) {
super(error.description);
this.code = error.code;
this.data = error.data;
+ this.status = error.status;
+ this.retryAfter = error.retryAfter;
}
}
+function getRetryAfter(response: Response): number | undefined {
+ if (response.status !== 429 && response.status !== 503) return;
+ const header = response.headers.get("retry-after");
+ if (!header) return;
+ const seconds = Number(header);
+ if (!Number.isNaN(seconds)) return Math.max(0, seconds * 1000);
+ const date = Date.parse(header);
+ if (!Number.isNaN(date)) return Math.max(0, date - Date.now());
+}
+
function getHeaders(token?: string | null) {
return token ? { Authorization: "Bearer " + token } : undefined;
}