SubtitleEdit/subtitleedit · error · Exception

Baidu API Error {errorCode}: {errorMsg}

Error message

Baidu API Error {errorCode}: {errorMsg}

What it means

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).

Source

Thrown at src/libuilogic/AutoTranslate/BaiduTranslate.cs:111

            var json = Encoding.UTF8.GetString(bytes).Trim();

            if (!result.IsSuccessStatusCode)
            {
                Error = json;
                SeLogger.Error("Baidu Translate failed calling API: Status code=" + result.StatusCode + Environment.NewLine + json);
                result.EnsureSuccessStatusCode();
            }

            var parser = new SeJsonParser();

            // Check for error response
            var errorCode = parser.GetFirstObject(json, "error_code");
            if (!string.IsNullOrEmpty(errorCode))
            {
                var errorMsg = parser.GetFirstObject(json, "error_msg");
                Error = $"Baidu API Error {errorCode}: {errorMsg}";
                SeLogger.Error("Baidu Translate error: " + Error);
                throw new Exception(Error);
            }

            // Parse translation result - Baidu returns one array element per line of input
            var transResult = parser.GetArrayElementsByName(json, "trans_result");
            if (transResult.Count == 0)
            {
                return string.Empty;
            }

            var translations = new List<string>();
            foreach (var item in transResult)
            {
                var dst = parser.GetFirstObject(item, "dst");
                translations.Add(string.IsNullOrEmpty(dst) ? string.Empty : Json.DecodeJsonText(dst));
            }

            return string.Join(Environment.NewLine, translations).Trim();
        }

View on GitHub (pinned to 17a9f07487)

Solutions

  1. Look up the numeric error_code in Baidu's API docs (https://fanyi-api.baidu.com/doc/21) for the exact cause.
  2. 52003: re-verify App ID and Secret Key in settings.
  3. 54003/54004: slow down requests or top up your Baidu quota.
  4. 58002/unsupported: confirm the source/target language codes are valid for Baidu.
  5. Check the SeLogger output for the full error string.
Defensive patterns

Strategy: retry

Validate before calling

// Cannot fully prevent server-side errors, but validate language codes first
if (!BaiduSupportedLanguages.Contains(sourceLanguageCode) || !BaiduSupportedLanguages.Contains(targetLanguageCode))
{
    Console.WriteLine("Unsupported Baidu language code.");
    return;
}

Try / catch

try
{
    var result = await translator.Translate(text, src, tgt, ct);
}
catch (Exception ex) when (ex.Message.Contains("Baidu API Error"))
{
    // ex.Message has the error_code; 54003/54004 may warrant retry/backoff
    if (ex.Message.Contains("54003")) { /* retry with backoff */ }
    else { logger.Error(ex.Message); }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13). Data as JSON: /api/errors/842f8a7d319ca3dd. Report an issue: GitHub.