languagetool-org/languagetool · error · RuntimeException

Got error: - HTTP response code

Error message

Got error: ${error} - HTTP response code ${conn.getResponseCode()}

What it means

RemoteLanguageTool.getMaxTextLength reads the server's /v2/maxtextlength endpoint; if the HTTP response is not the expected success code, it reads the error stream and throws a RuntimeException containing the server's error body and the HTTP status code. This means the request reached the server but the server rejected it.

Solutions

  1. Read the embedded error body and HTTP status in the message to identify the server-side problem
  2. Verify serverBaseUrl points at a running LanguageTool server (try /v2/languages in a browser or curl)
  3. Check server logs for the corresponding request failure
  4. If behind a proxy, fix proxy/rate-limiting/auth configuration

Example fix

// before
RemoteLanguageTool lt = new RemoteLanguageTool("http://localhost:8081"); // wrong port, another app
// after
RemoteLanguageTool lt = new RemoteLanguageTool("http://localhost:8081"); // with LT running:
// curl -s http://localhost:8081/v2/maxtextlength  -> 200 with number
Defensive patterns

Strategy: retry

Validate before calling

HttpURLConnection probe = (HttpURLConnection) new URL(serverBaseUrl + "/v2/languages").openConnection();
probe.setConnectTimeout(3000);
if (probe.getResponseCode() != 200) throw new IllegalStateException("server not healthy");

Type guard

static boolean isLanguageToolServerUp(String baseUrl) {
  try {
    HttpURLConnection c = (HttpURLConnection) new URL(baseUrl + "/v2/languages").openConnection();
    c.setConnectTimeout(3000);
    return c.getResponseCode() == 200;
  } catch (IOException e) { return false; }
}

Try / catch

try {
  int max = lt.getMaxTextLength();
} catch (RuntimeException e) {
  if (e.getMessage().contains("Got error:")) {
    // parse embedded HTTP code and server body, retry with backoff or fail fast
  } else throw e;
}

Prevention

When it happens

Trigger: Calling getMaxTextLength() against a server that returns a non-2xx response: wrong API endpoint path, server-side authentication failure, an HTTP 4xx/5xx from a proxy, or a non-LanguageTool service listening on the URL.

Common situations: Reverse proxy returning 502/503; wrong port pointing at another app; premium API endpoint hit without valid credentials; server overloaded returning errors.

Understand the failure class

Background: "API error: {status}" and "HTTP 401/403/404/429/5xx" errors: non-2xx HTTP responses explained — this error's family across 27 libraries.

Related errors


AI-assisted analysis of languagetool-org/languagetool@2e990059ce (2026-09-06). Data as JSON: /api/errors/aa323d18f43139c3. Report an issue: GitHub.

Appendix: source

Thrown at languagetool-http-client/src/main/java/org/languagetool/remote/RemoteLanguageTool.java:226

      checkUrl = new URL(serverBaseUrl + V2_MAXTEXTLENGTH);
    } catch (MalformedURLException e) {
      throw new RuntimeException(e);
    }
    HttpURLConnection conn = getConnection(postData, checkUrl);
    try {
      if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
        try (InputStream inputStream = conn.getInputStream()) {
          StringBuilder sb = new StringBuilder();
          try (InputStreamReader isr = new InputStreamReader(inputStream, "utf-8");
               BufferedReader br = new BufferedReader(isr)) {
            String line = br.readLine();
            return Integer.parseInt(line);
          }
        }
      } else {
        try (InputStream inputStream = conn.getErrorStream()) {
          String error = readStream(inputStream, "utf-8");
          throw new RuntimeException("Got error: " + error + " - HTTP response code " + conn.getResponseCode());
        }
      }
    } catch (ConnectException e) {
      throw new RuntimeException("Could not connect to server at " + serverBaseUrl, e);
    } catch (Exception e) {
      throw new RuntimeException(e);
    } finally {
      conn.disconnect();
    }
  }

  HttpURLConnection getConnection(byte[] postData, URL url) {
    try {
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setDoOutput(true);
      conn.setInstanceFollowRedirects(false);
      conn.setRequestMethod("POST");
      conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

View on GitHub (pinned to 2e990059ce)