Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

feat(web): word TeamHub's generic error codes in the interface language

yunheab51a829ffcd
diff --git a/apps/web/src/features/teamhub/teamhub-api.ts b/apps/web/src/features/teamhub/teamhub-api.ts
index 596288a3..5dd11c80 100644
--- a/apps/web/src/features/teamhub/teamhub-api.ts
+++ b/apps/web/src/features/teamhub/teamhub-api.ts
@@ -56,9 +56,7 @@ export function createTeamHubApi(options: {
},
onError({ error }) {
if (error instanceof Error && error.name === "AbortError") return error;
- return new TeamHubTransportError(
- "TeamHub request failed before receiving a response.",
- );
+ return new TeamHubTransportError();
},
};
api.use(errors);
diff --git a/apps/web/src/features/teamhub/teamhub-client.test.ts b/apps/web/src/features/teamhub/teamhub-client.test.ts
index be48aec1..d1b805e6 100644
--- a/apps/web/src/features/teamhub/teamhub-client.test.ts
+++ b/apps/web/src/features/teamhub/teamhub-client.test.ts
@@ -243,7 +243,8 @@ describe("TeamHubClient", () => {
await expect(teamHubData(client.api.GET("/v0/me"))).rejects.toEqual(
expect.objectContaining({
code: "UNAUTHENTICATED",
- message: "Signed in elsewhere.",
+ message: "Your sign-in has expired. Sign in again.",
+ serverMessage: "Signed in elsewhere.",
requestId: "request-1",
status: 401,
}),
diff --git a/apps/web/src/features/teamhub/teamhub-error-text.ts b/apps/web/src/features/teamhub/teamhub-error-text.ts
new file mode 100644
index 00000000..037b3b9b
--- /dev/null
+++ b/apps/web/src/features/teamhub/teamhub-error-text.ts
@@ -0,0 +1,47 @@
+import type { Translate } from "@/i18n/language";
+import type { Message } from "@/i18n/messages";
+
+import { teamHubErrorMessages as m } from "./teamhub-error.messages";
+
+// Only codes whose message says nothing beyond the code are worded here.
+// FORBIDDEN, CONFLICT, NOT_FOUND, PAYLOAD_TOO_LARGE, INVALID_REQUEST and the
+// unavailable codes keep TeamHub's message, which names what to do, such as
+// "This email was already sent." A case worth its own wording needs its own
+// code from TeamHub first.
+const worded: Readonly<Record<string, Message<string>>> = {
+ UNAUTHENTICATED: m.unauthenticated,
+ RATE_LIMITED: m.rateLimited,
+ INTERNAL_ERROR: m.internal,
+ // TeamHub always sends the same sentence for this code.
+ LOCAL_COMPUTER_OFFLINE: m.computerOffline,
+};
+
+/**
+ * The text a failed TeamHub request shows, in the interface language.
+ * A generic code is worded from the code; every other code keeps the
+ * message TeamHub sent. `HTTP_<status>` is the client's own code for a
+ * response without a TeamHub error body, from a layer in front of TeamHub.
+ */
+export function teamHubErrorText(
+ error: {
+ readonly code: string;
+ readonly message?: string;
+ readonly status: number;
+ },
+ t: Translate,
+): string {
+ const message = Object.hasOwn(worded, error.code)
+ ? worded[error.code]
+ : undefined;
+ if (message) return t(message);
+ if (error.code === `HTTP_${error.status}`) return statusText(error.status, t);
+ return error.message || statusText(error.status, t);
+}
+
+// Only the status class is meaningful without a TeamHub error body.
+function statusText(status: number, t: Translate) {
+ if (status === 401 || status === 403) return t(m.refused);
+ if (status === 429) return t(m.rateLimited);
+ if (status >= 500) return t(m.unavailable);
+ return t(m.notCompleted, { status: String(status) });
+}
diff --git a/apps/web/src/features/teamhub/teamhub-error.messages.ts b/apps/web/src/features/teamhub/teamhub-error.messages.ts
new file mode 100644
index 00000000..dafbf193
--- /dev/null
+++ b/apps/web/src/features/teamhub/teamhub-error.messages.ts
@@ -0,0 +1,19 @@
+import { defineMessages } from "@/i18n/messages";
+
+/**
+ * What a failed TeamHub request tells the person when TeamHub's code or the
+ * response status says all there is to say. Other codes keep TeamHub's own
+ * message.
+ */
+export const teamHubErrorMessages = defineMessages("teamHubErrors", {
+ unauthenticated: "Your sign-in has expired. Sign in again.",
+ rateLimited: "Too many requests. Wait a moment and try again.",
+ unavailable: "The server is temporarily unavailable. Try again shortly.",
+ internal: "Something went wrong on the server. Try again.",
+ computerOffline: "The computer is offline.",
+ refused: "The server refused this request. Reload the page and try again.",
+ notCompleted:
+ "The server could not complete this request ({status}). Try again.",
+ unreachable:
+ "Couldn't reach the server. Check your connection and try again.",
+});
diff --git a/apps/web/src/features/teamhub/teamhub-error.test.ts b/apps/web/src/features/teamhub/teamhub-error.test.ts
index e6372b95..d243b63b 100644
--- a/apps/web/src/features/teamhub/teamhub-error.test.ts
+++ b/apps/web/src/features/teamhub/teamhub-error.test.ts
@@ -1,19 +1,26 @@
-import { describe, expect, it } from "vitest";
+import { afterEach, describe, expect, it } from "vitest";
-import { readTeamHubError } from "./teamhub-error";
+import { resetLanguageForTest, setLanguage } from "@/i18n/language";
+import zhCN from "@/i18n/locales/zh-CN.json";
+
+import { readTeamHubError, TeamHubTransportError } from "./teamhub-error";
+
+afterEach(() => resetLanguageForTest());
+
+function teamHubResponse(code: string, message: string, status: number) {
+ return Response.json(
+ { error: { code, message, request_id: "req-1" } },
+ { status },
+ );
+}
describe("readTeamHubError", () => {
- it("keeps the TeamHub error message when the body is a TeamHub error", async () => {
+ it("keeps TeamHub's message for a code this client does not word", async () => {
const error = await readTeamHubError(
- Response.json(
- {
- error: {
- code: "ROOM_LEFT",
- message: "Rejoin this Room before sending messages.",
- request_id: "req-1",
- },
- },
- { status: 403 },
+ teamHubResponse(
+ "ROOM_LEFT",
+ "Rejoin this Room before sending messages.",
+ 403,
),
);
@@ -21,6 +28,56 @@ describe("readTeamHubError", () => {
expect(error.message).toBe("Rejoin this Room before sending messages.");
});
+ it("keeps TeamHub's message for an invalid request, which names the rule", async () => {
+ await setLanguage("zh-CN");
+ const error = await readTeamHubError(
+ teamHubResponse("INVALID_REQUEST", "name must not be empty", 400),
+ );
+
+ expect(error.message).toBe("name must not be empty");
+ });
+
+ it("words a generic code in the interface language and keeps TeamHub's text", async () => {
+ await setLanguage("zh-CN");
+ const error = await readTeamHubError(
+ teamHubResponse("INTERNAL_ERROR", "an unexpected error occurred", 500),
+ );
+
+ expect(error.code).toBe("INTERNAL_ERROR");
+ expect(error.message).toBe(zhCN.teamHubErrors.internal);
+ expect(error.serverMessage).toBe("an unexpected error occurred");
+ });
+
+ it("words a generic code in English", async () => {
+ const error = await readTeamHubError(
+ teamHubResponse(
+ "UNAUTHENTICATED",
+ "authentication is invalid or expired",
+ 401,
+ ),
+ );
+
+ expect(error.message).toBe("Your sign-in has expired. Sign in again.");
+ });
+
+ it.each([
+ ["CONFLICT", "This email was already sent.", 409],
+ ["FORBIDDEN", "Only the decision owner can decide this request.", 403],
+ ["NOT_FOUND", "Shared with me is unavailable.", 404],
+ ["PAYLOAD_TOO_LARGE", "Document content exceeds 10 MiB.", 413],
+ ["SERVICE_UNAVAILABLE", "Comment service unavailable", 503],
+ ])(
+ "keeps TeamHub's message for %s, which says what to do",
+ async (code, message, status) => {
+ await setLanguage("zh-CN");
+ const error = await readTeamHubError(
+ teamHubResponse(code, message, status),
+ );
+
+ expect(error.message).toBe(message);
+ },
+ );
+
it("explains a refusal from a layer in front of TeamHub without the raw status", async () => {
const error = await readTeamHubError(
new Response("<html>Forbidden</html>", { status: 403 }),
@@ -28,8 +85,29 @@ describe("readTeamHubError", () => {
expect(error.code).toBe("HTTP_403");
expect(error.status).toBe(403);
+ expect(error.serverMessage).toBeUndefined();
expect(error.message).toBe(
"The server refused this request. Reload the page and try again.",
);
});
+
+ it("words a response without a TeamHub body in the interface language", async () => {
+ await setLanguage("zh-CN");
+ const error = await readTeamHubError(
+ new Response("Bad gateway", { status: 418 }),
+ );
+
+ expect(error.message).toBe(
+ zhCN.teamHubErrors.notCompleted.replace("{status}", "418"),
+ );
+ });
+});
+
+describe("TeamHubTransportError", () => {
+ it("reads as a connection problem in the interface language", async () => {
+ await setLanguage("zh-CN");
+ expect(new TeamHubTransportError().message).toBe(
+ zhCN.teamHubErrors.unreachable,
+ );
+ });
});
diff --git a/apps/web/src/features/teamhub/teamhub-error.ts b/apps/web/src/features/teamhub/teamhub-error.ts
index 80d4f300..94e952bb 100644
--- a/apps/web/src/features/teamhub/teamhub-error.ts
+++ b/apps/web/src/features/teamhub/teamhub-error.ts
@@ -1,3 +1,7 @@
+import { translate } from "@/i18n/language";
+
+import { teamHubErrorMessages } from "./teamhub-error.messages";
+import { teamHubErrorText } from "./teamhub-error-text";
import type { TeamHubErrorResponse } from "./teamhub-types";
export class TeamHubError extends Error {
@@ -6,6 +10,8 @@ export class TeamHubError extends Error {
readonly requestId?: string;
readonly retryAfterSeconds?: number;
readonly retryable: boolean;
+ /** TeamHub's own English message, for diagnostics; `message` is worded. */
+ readonly serverMessage?: string;
readonly status: number;
constructor(input: {
@@ -15,11 +21,13 @@ export class TeamHubError extends Error {
requestId?: string;
retryAfterSeconds?: number;
retryable?: boolean;
+ serverMessage?: string;
status: number;
}) {
super(input.message);
this.name = "TeamHubError";
this.code = input.code;
+ this.serverMessage = input.serverMessage;
this.details = input.details;
this.requestId = input.requestId;
this.retryAfterSeconds = input.retryAfterSeconds;
@@ -28,8 +36,9 @@ export class TeamHubError extends Error {
}
}
+/** A request that never got a response; it reads as a connection problem. */
export class TeamHubTransportError extends Error {
- constructor(message: string) {
+ constructor(message: string = translate(teamHubErrorMessages.unreachable)) {
super(message);
this.name = "TeamHubTransportError";
}
@@ -44,34 +53,25 @@ export async function readTeamHubError(response: Response) {
body = undefined;
}
+ const code = body?.error?.code ?? `HTTP_${response.status}`;
+ const serverMessage = body?.error?.message;
return new TeamHubError({
- code: body?.error?.code ?? `HTTP_${response.status}`,
+ code,
details: body?.error?.details,
- message: body?.error?.message ?? fallbackErrorMessage(response.status),
+ message: teamHubErrorText(
+ { code, message: serverMessage, status: response.status },
+ translate,
+ ),
requestId: body?.error?.request_id,
retryAfterSeconds: parseRetryAfterSeconds(
response.headers.get("Retry-After"),
),
retryable: body?.error?.retryable,
+ serverMessage,
status: response.status,
});
}
-// A response without a TeamHub error body came from a layer in front of
-// TeamHub (gateway, proxy, WAF), so only the status class is meaningful.
-function fallbackErrorMessage(status: number) {
- if (status === 401 || status === 403) {
- return "The server refused this request. Reload the page and try again.";
- }
- if (status === 429) {
- return "Too many requests. Wait a moment and try again.";
- }
- if (status >= 500) {
- return "The server is temporarily unavailable. Try again shortly.";
- }
- return `The server could not complete this request (${status}). Try again.`;
-}
-
function parseRetryAfterSeconds(value: string | null) {
if (value === null || !/^\d+$/.test(value.trim())) return undefined;
const seconds = Number(value);
diff --git a/apps/web/src/i18n/locales/zh-CN.json b/apps/web/src/i18n/locales/zh-CN.json
index 9728ee09..ee4cb727 100644
--- a/apps/web/src/i18n/locales/zh-CN.json
+++ b/apps/web/src/i18n/locales/zh-CN.json
@@ -2839,5 +2839,15 @@
"researchAnalystDescription": "把问题和资料整理成有出处、可直接用于决策的简报。",
"codeReviewerName": "代码审查员",
"codeReviewerDescription": "从正确性、风险和清晰度审查改动,并给出可执行的发现。"
+ },
+ "teamHubErrors": {
+ "unauthenticated": "登录已过期,请重新登录。",
+ "rateLimited": "请求过于频繁,请稍后再试。",
+ "unavailable": "服务器暂时不可用,请稍后再试。",
+ "internal": "服务器出错了,请重试。",
+ "computerOffline": "电脑已离线。",
+ "refused": "服务器拒绝了此请求。请刷新页面后重试。",
+ "notCompleted": "服务器无法完成此请求({status}),请重试。",
+ "unreachable": "无法连接到服务器。请检查网络后重试。"
}
}