Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

emojisets: Improve error handling when we fail to fetch an emojiset. #29761

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 44 additions & 16 deletions web/src/emojisets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,53 @@ emojisets.set("text", emojisets.get("google")!);
let current_emojiset: EmojiSet | undefined;

async function fetch_emojiset(name: string, url: string): Promise<void> {
return new Promise((resolve, _reject) => {
const get_emojiset = (): void => {
const sheet = new Image();
sheet.addEventListener("load", () => {
window.removeEventListener("online", get_emojiset);
resolve();
});
sheet.addEventListener("error", () => {
// If there's an error, try again when the browser is online
window.addEventListener("online", get_emojiset);
const MAX_RETRIES = 3;
const RETRY_DELAY = 10000; // 10 seconds

for (let attempt = 1; attempt <= MAX_RETRIES; attempt += 1) {
try {
const response = await fetch(url);

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

// If the fetch is successful, resolve the promise and return
return;
} catch (error) {
if (!navigator.onLine) {
// If the user is offline, retry once they are online.
await new Promise<void>((resolve) => {
// We don't want to throw an error here since this is clearly something wrong with user's network.
blueslip.warn(
`Failed to load emojiset ${name} from ${url}. Retrying when online.`,
);
window.addEventListener(
"online",
() => {
resolve();
},
{once: true},
);
});
} else {
blueslip.warn(
`Failed to load emojiset ${name} from ${url}. A retry will be attempted when the browser is online.`,
`Failed to load emojiset ${name} from ${url}. Attempt ${attempt} of ${MAX_RETRIES}.`,
);
});
sheet.src = url;
};

get_emojiset();
});
// If this was the last attempt, rethrow the error
if (attempt === MAX_RETRIES) {
blueslip.error(
`Failed to load emojiset ${name} from ${url} after ${MAX_RETRIES} attempts.`,
);
throw error;
}

// Wait before the next attempt
await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY));
}
}
}
}

export async function select(name: string): Promise<void> {
Expand Down