{"record":{"id":"9466a0d1e6e7a5ca","repo":"SubtitleEdit/subtitleedit","slug":"an-error-occurred-calling-gt-translate-status-co","errorCode":null,"errorMessage":"An error occurred calling GT translate - status code: {result.StatusCode}","messagePattern":"An error occurred calling GT translate - status code: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/libuilogic/AutoTranslate/GoogleTranslateV2.cs","lineNumber":85,"sourceCode":"                    catch\n                    {\n\n                        // ignore\n                    }\n                }\n\n                if ((int)result.StatusCode == 400)\n                {\n                    throw new Exception(\"API key invalid (or perhaps billing is not enabled)?\");\n                }\n                if ((int)result.StatusCode == 403)\n                {\n                    throw new Exception(\"\\\"Perhaps billing is not enabled (or API key is invalid)?\\\"\");\n                }\n\n                if (!result.IsSuccessStatusCode)\n                {\n                    throw new Exception($\"An error occurred calling GT translate - status code: {result.StatusCode}\");\n                }\n\n                content = await result.Content.ReadAsStringAsync(cancellationToken);\n            }\n            catch (WebException webException)\n            {\n                var message = string.Empty;\n                if (webException.Message.Contains(\"(400) Bad Request\"))\n                {\n                    message = \"API key invalid (or perhaps API/billing is not enabled)?\";\n                }\n                else if (webException.Message.Contains(\"(403) Forbidden.\"))\n                {\n                    message = \"Perhaps billing is not enabled (or API not enabled or API key is invalid)?\";\n                }\n\n                throw new Exception(message, webException);\n            }","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/libuilogic/AutoTranslate/GoogleTranslateV2.cs#L67-L103","documentation":"Thrown by GoogleTranslateV2 as the catch-all for any non-success status that is not 400 or 403 (those are handled explicitly above). The status code is included. This covers 429 (rate limit), 5xx (Google server errors), and other unexpected responses.","triggerScenarios":"Translate() posts; result.StatusCode is not 400, not 403, and not success. Most commonly 429 from exceeding per-100-second request/character quotas, or occasional 5xx.","commonSituations":"Per-100-second character or request quota exceeded (429); sustained load tripping rate limits; rare Google-side 500/503; region-specific outages.","solutions":["For 429: reduce request rate or batch size; the GCP quotas page shows per-second limits.","Request a quota increase in the GCP console for the Translation API.","For 5xx: retry with exponential backoff.","Inspect the exact status code in the message to pick the right response."],"exampleFix":"// before\nif (!result.IsSuccessStatusCode)\n{\n    throw new Exception($\"An error occurred calling GT translate - status code: {result.StatusCode}\");\n}\n\n// after - include the response body (already captured in Error) and special-case 429\nif (!result.IsSuccessStatusCode)\n{\n    if ((int)result.StatusCode == 429)\n    {\n        throw new Exception(\"GoogleTranslateV2 rate limit (429) exceeded - slow down or request a quota increase.\");\n    }\n    throw new Exception($\"An error occurred calling GT translate - status code: {result.StatusCode}. Body: {Error}\");\n}","handlingStrategy":"retry","validationCode":"// Pre-check daily/per-100s quota via the projects API (if available) or enforce a local limiter\nprivate static readonly RateLimiter GoogleV2Limiter = new RateLimiter(perSecond: 50);\npublic async Task<string> TranslateLimited(...)\n{\n    await GoogleV2Limiter.AcquireAsync(token);\n    return await googleV2.Translate(text, src, tgt, token);\n}","typeGuard":null,"tryCatchPattern":"for (int attempt = 0; attempt < 5; attempt++)\n{\n    try { return await googleV2.Translate(text, src, tgt, token); }\n    catch (Exception ex) when (ex.Message.Contains(\"status code: TooManyRequests\"))\n    {\n        await Task.Delay(TimeSpan.FromSeconds(Math.Pow(2, attempt)), token);\n        continue;\n    }\n    throw;\n}\nthrow new InvalidOperationException(\"GoogleTranslateV2 kept rate-limiting after backoff.\");","preventionTips":["Stay under the per-100-second request/character quotas; batch text where possible.","Use exponential backoff on 429 and 5xx.","Request quota increases in GCP for production workloads.","Log the exact status code so 429 vs 5xx can be handled differently."],"tags":["api","rate-limit","translation","google"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}