{"record":{"id":"842f8a7d319ca3dd","repo":"SubtitleEdit/subtitleedit","slug":"baidu-api-error-errorcode-errormsg","errorCode":null,"errorMessage":"Baidu API Error {errorCode}: {errorMsg}","messagePattern":"Baidu API Error (.+?): (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/libuilogic/AutoTranslate/BaiduTranslate.cs","lineNumber":111,"sourceCode":"            var json = Encoding.UTF8.GetString(bytes).Trim();\n\n            if (!result.IsSuccessStatusCode)\n            {\n                Error = json;\n                SeLogger.Error(\"Baidu Translate failed calling API: Status code=\" + result.StatusCode + Environment.NewLine + json);\n                result.EnsureSuccessStatusCode();\n            }\n\n            var parser = new SeJsonParser();\n\n            // Check for error response\n            var errorCode = parser.GetFirstObject(json, \"error_code\");\n            if (!string.IsNullOrEmpty(errorCode))\n            {\n                var errorMsg = parser.GetFirstObject(json, \"error_msg\");\n                Error = $\"Baidu API Error {errorCode}: {errorMsg}\";\n                SeLogger.Error(\"Baidu Translate error: \" + Error);\n                throw new Exception(Error);\n            }\n\n            // Parse translation result - Baidu returns one array element per line of input\n            var transResult = parser.GetArrayElementsByName(json, \"trans_result\");\n            if (transResult.Count == 0)\n            {\n                return string.Empty;\n            }\n\n            var translations = new List<string>();\n            foreach (var item in transResult)\n            {\n                var dst = parser.GetFirstObject(item, \"dst\");\n                translations.Add(string.IsNullOrEmpty(dst) ? string.Empty : Json.DecodeJsonText(dst));\n            }\n\n            return string.Join(Environment.NewLine, translations).Trim();\n        }","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/libuilogic/AutoTranslate/BaiduTranslate.cs#L93-L129","documentation":"Thrown by BaiduTranslate.Translate when the Baidu API response JSON contains a non-empty 'error_code' field. The error code and 'error_msg' are formatted into the message, logged via SeLogger, and a bare Exception is thrown. This is Baidu's server-side error channel (e.g. invalid sign, rate limit, unsupported language, insufficient quota).","triggerScenarios":"Baidu returns an error_code for reasons such as: bad signature (wrong appid/secretKey/salt), unsupported language pair, rate limiting (54003), insufficient balance (54004), invalid API key (52003), or a request that exceeds the per-request size limit. The specific code is in the message.","commonSituations":"Wrong App ID or Secret Key (52003), IP not whitelisted, exceeded QPS/QPM limits (54003), out of quota (54004), source/target language code not recognized by Baidu, or a clock/salt/sign mismatch.","solutions":["Look up the numeric error_code in Baidu's API docs (https://fanyi-api.baidu.com/doc/21) for the exact cause.","52003: re-verify App ID and Secret Key in settings.","54003/54004: slow down requests or top up your Baidu quota.","58002/unsupported: confirm the source/target language codes are valid for Baidu.","Check the SeLogger output for the full error string."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// Cannot fully prevent server-side errors, but validate language codes first\nif (!BaiduSupportedLanguages.Contains(sourceLanguageCode) || !BaiduSupportedLanguages.Contains(targetLanguageCode))\n{\n    Console.WriteLine(\"Unsupported Baidu language code.\");\n    return;\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    var result = await translator.Translate(text, src, tgt, ct);\n}\ncatch (Exception ex) when (ex.Message.Contains(\"Baidu API Error\"))\n{\n    // ex.Message has the error_code; 54003/54004 may warrant retry/backoff\n    if (ex.Message.Contains(\"54003\")) { /* retry with backoff */ }\n    else { logger.Error(ex.Message); }\n}","preventionTips":["Map subtitle language codes to Baidu-recognized codes before calling.","Throttle requests below Baidu's QPS/QPM limits to avoid 54003.","Monitor quota to catch 54004 early.","Log error_code via SeLogger for diagnosis."],"tags":["auto-translate","baidu","api-error","network","rate-limit"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}