From b3ab222bd79e7fde9ad3108e13bafc6edeed415c Mon Sep 17 00:00:00 2001 From: Nitin Date: Thu, 30 Oct 2025 10:31:11 +0530 Subject: [PATCH 1/2] fix: improve error handling and documentation in locale regex and fetch response --- package.json | 4 ++-- packages/locales/src/constants.ts | 18 ++++++++++++++++-- packages/sdk/src/index.ts | 31 +++++++++++++++++++++++++------ scripts/docs/src/utils.ts | 6 +++++- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 13fe1c4c1..3585aa2a8 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "new": "changeset", "new:empty": "changeset --empty", "format": "prettier . --write", - "format:check": "prettier . --check" + "format:check": "prettier . --check", }, "devDependencies": { "@babel/generator": "^7.27.1", @@ -31,4 +31,4 @@ "node-machine-id": "^1.1.12" }, "packageManager": "pnpm@9.12.3" -} +} \ No newline at end of file diff --git a/packages/locales/src/constants.ts b/packages/locales/src/constants.ts index 1a458195f..1f7f4401c 100644 --- a/packages/locales/src/constants.ts +++ b/packages/locales/src/constants.ts @@ -28,5 +28,19 @@ * - Script codes preserve their original case * - Region codes are converted to uppercase */ -export const LOCALE_REGEX = - /^([a-z]{2,3})(?:[-_]([A-Za-z]{4}))?(?:[-_]([A-Z]{2}|[0-9]{3}))?$/; +// export const LOCALE_REGEX = +// /^([a-z]{2,3})(?:[-_]([A-Za-z]{4}))?(?:[-_]([A-Z]{2}|[0-9]{3}))?$/; + +/* The `export const LOCALE_REGEX` statement is defining a regular expression object named +`LOCALE_REGEX` that is used for parsing locale strings. The regular expression is constructed using +the `RegExp` constructor with the following components: */ +export const LOCALE_REGEX = new RegExp( + [ + '^', + '([a-z]{2,3})', // Language code + '(?:[-_]([A-Za-z]{4}))?', // Optional script code + '(?:[-_]([A-Z]{2}|[0-9]{3}))?', // Optional region code + '$' + ].join(''), + 'u' // Unicode flag for better Unicode support +); \ No newline at end of file diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 38ea763a0..6e6b72fe2 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -133,16 +133,35 @@ export class LingoDotDevEngine { signal, }); + // if (!res.ok) { + // if (res.status >= 500 && res.status < 600) { + // const errorText = await res.text(); + // throw new Error( + // `Server error (${res.status}): ${res.statusText}. ${errorText}. This may be due to temporary service issues.`, + // ); + // } else if (res.status === 400) { + // throw new Error(`Invalid request: ${res.statusText}`); + // } else { + // const errorText = await res.text(); + // throw new Error(errorText); + // } + // } + + /* The above TypeScript code is handling errors from a fetch request. It checks if the response + `res` is not ok (i.e., the status is not in the 200-299 range). If the status code is in the + 500-599 range, it throws an error indicating a server error along with the status code, status + text, and the error text received from the response. If the status code is 400, it throws an + error indicating an invalid request with the status text. For any other status codes, it throws + an error with the error text received from the response. */ if (!res.ok) { + const errorText = await res.text(); // Retrieve error text once if (res.status >= 500 && res.status < 600) { - const errorText = await res.text(); throw new Error( `Server error (${res.status}): ${res.statusText}. ${errorText}. This may be due to temporary service issues.`, ); } else if (res.status === 400) { throw new Error(`Invalid request: ${res.statusText}`); } else { - const errorText = await res.text(); throw new Error(errorText); } } @@ -592,8 +611,8 @@ export class ReplexicaEngine extends LingoDotDevEngine { if (!ReplexicaEngine.hasWarnedDeprecation) { console.warn( "ReplexicaEngine is deprecated and will be removed in a future release. " + - "Please use LingoDotDevEngine instead. " + - "See https://lingo.dev/cli for more information.", + "Please use LingoDotDevEngine instead. " + + "See https://lingo.dev/cli for more information.", ); ReplexicaEngine.hasWarnedDeprecation = true; } @@ -611,8 +630,8 @@ export class LingoEngine extends LingoDotDevEngine { if (!LingoEngine.hasWarnedDeprecation) { console.warn( "LingoEngine is deprecated and will be removed in a future release. " + - "Please use LingoDotDevEngine instead. " + - "See https://lingo.dev/cli for more information.", + "Please use LingoDotDevEngine instead. " + + "See https://lingo.dev/cli for more information.", ); LingoEngine.hasWarnedDeprecation = true; } diff --git a/scripts/docs/src/utils.ts b/scripts/docs/src/utils.ts index cdcb27552..2416a539d 100644 --- a/scripts/docs/src/utils.ts +++ b/scripts/docs/src/utils.ts @@ -6,7 +6,11 @@ import { Octokit } from "@octokit/rest"; import * as prettier from "prettier"; export function getRepoRoot(): string { - const __filename = fileURLToPath(import.meta.url); + const metadataUrl = import.meta.url; + if (!metadataUrl) { + throw new Error("import.meta.url is undefined"); + } + const __filename = fileURLToPath(metadataUrl); const __dirname = path.dirname(__filename); let currentDir = __dirname; From 571d944c9722828ef2c539a7e4e43f76733ce007 Mon Sep 17 00:00:00 2001 From: Nitin Date: Thu, 30 Oct 2025 10:51:18 +0530 Subject: [PATCH 2/2] fix: enhance error handling documentation for import.meta.url in getRepoRoot function --- scripts/docs/src/utils.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/scripts/docs/src/utils.ts b/scripts/docs/src/utils.ts index 2416a539d..3efc92cf0 100644 --- a/scripts/docs/src/utils.ts +++ b/scripts/docs/src/utils.ts @@ -7,6 +7,11 @@ import * as prettier from "prettier"; export function getRepoRoot(): string { const metadataUrl = import.meta.url; + /* This code snippet is checking if the `metadataUrl` variable is falsy (in this case, `undefined`). + If `metadataUrl` is `undefined`, it means that the `import.meta.url` feature is not supported or + not available in the current environment. In that case, the code throws an error with the message + "import.meta.url is undefined" to indicate that the script relies on `import.meta.url` and cannot + proceed without it. */ if (!metadataUrl) { throw new Error("import.meta.url is undefined"); }