SubtitleEdit/subtitleedit · error · Exception

An error occurred during translate: {jsonResult}

Error message

An error occurred during translate:

 {jsonResult}

What it means

Thrown by MicrosoftTranslator.Translate() as the catch-all for any non-success, non-401 status from Azure Translator. Includes the full response body (jsonResult), which usually contains Azure's structured error JSON with a 'message' field explaining the cause.

Source

Thrown at src/libuilogic/AutoTranslate/MicrosoftTranslator.cs:102

            jsonBuilder.Append("{ \"Text\":\"" + Json.EncodeJsonText(text) + "\"}");
            jsonBuilder.Append("]");
            var json = jsonBuilder.ToString();
            var content = new StringContent(json, Encoding.UTF8);
            content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
            var result = await httpClient.PostAsync(url, content, cancellationToken);
            var parser = new JsonParser();
            var jsonResult = await result.Content.ReadAsStringAsync(cancellationToken);

            if (!result.IsSuccessStatusCode)
            {
                Error = jsonResult;

                if (result.StatusCode == HttpStatusCode.Unauthorized)
                {
                    throw new Exception("API key is not valid!" + Environment.NewLine + Environment.NewLine + jsonResult);
                }

                throw new Exception("An error occurred during translate:" + Environment.NewLine + Environment.NewLine + jsonResult);
            }

            var x = (List<object>)parser.Parse(jsonResult);
            foreach (var xElement in x)
            {
                var dict = (Dictionary<string, object>)xElement;
                var y = (List<object>)dict["translations"];
                foreach (var o in y)
                {
                    var textDictionary = (Dictionary<string, object>)o;
                    var res = (string)textDictionary["text"];
                    res = res.Replace("<br />", Environment.NewLine);
                    res = res.Replace("<br/>", Environment.NewLine);
                    res = res.Replace("<br>", Environment.NewLine);
                    results.Add(res);
                }
            }

View on GitHub (pinned to 17a9f07487)

Solutions

  1. Parse the jsonResult for Azure's error code/message (it has .error.code like 'UnsupportedLanguagePair').
  2. For 429: reduce concurrency and add backoff.
  3. For 400: verify the language codes are valid Azure Translator codes and the text fits the limit.
  4. Check the Azure Service Health dashboard for incidents (503).

Example fix

// before
throw new Exception("An error occurred during translate:" + Environment.NewLine + Environment.NewLine + jsonResult);

// after - surface the HTTP code and Azure's structured error code alongside the body
throw new Exception($"Azure Translator error: HTTP {(int)result.StatusCode} ({result.StatusCode})." + Environment.NewLine + Environment.NewLine + jsonResult);
Defensive patterns

Strategy: retry

Validate before calling

// Validate language codes against Azure's supported set and respect the 10000-char limit
if (text.Length > 10000)
    throw new ArgumentOutOfRangeException(nameof(text), "Azure Translator accepts at most 10000 characters per request.");
var supported = await GetAzureLanguagesAsync(token);
if (!supported.Contains(targetLanguageCode))
    throw new ArgumentException($"Target language '{targetLanguageCode}' is not supported by Azure Translator.");

Try / catch

try
{
    return await translator.Translate(text, src, tgt, token);
}
catch (Exception ex) when (ex.Message.Contains("An error occurred during translate"))
{
    // Parse Azure's error JSON (in the message body) for .error.code to branch
    if (ex.Message.Contains("429")) { await Task.Delay(backoff, token); return await translator.Translate(text, src, tgt, token); }
    throw;
}

Prevention

When it happens

Trigger: Translate() posts; result is not success and not 401. Common codes: 400 (bad request - unsupported language pair, text too long), 403 (forbidden - quota/resource disabled), 429 (rate limit), 503 (service unavailable).

Common situations: Unsupported source/target language code for Azure Translator; single-request character limit exceeded (Azure allows 10000 chars per request); 429 from exceeding the transactions-per-second quota; 503 during Azure incidents.

Related errors


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