You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript中正确捕获Akamai返回的多行HTML格式服务不可用错误的方法

Fixing the Akamai HTML Error Detection Issue

Got it, let's tackle this problem. The core issue here is that your current includes check might be failing because the raw HTML response has line breaks, extra whitespace, or nested tags that make the exact string match tricky. Here are a few robust solutions to reliably catch this Akamai error:

1. Parse the HTML to Extract Plain Text (Most Reliable)

Instead of checking the raw HTML string directly, use the browser's built-in DOMParser to convert the HTML into a DOM object, then extract the plain text content. This strips out all HTML tags and normalizes whitespace, making your check much more resilient to minor changes in Akamai's error page structure.

// Assuming your 'error' variable holds the full HTML response string
const parser = new DOMParser();
const errorDoc = parser.parseFromString(error, 'text/html');
const plainErrorText = errorDoc.body.textContent.trim();

if (plainErrorText.includes('Service Unavailable - Zero size object')) {
  // Show your user-friendly error prompt
  alert('Oops! The service is temporarily unavailable. Please try again later.');
}

This method works because it focuses on the actual human-readable content of the error page, not the messy HTML markup around it.

2. Use a Regular Expression to Ignore Whitespace/Line Breaks

If you don't want to parse the DOM, a regular expression can handle multi-line strings more gracefully than includes(). The regex will match your target phrase regardless of line breaks or extra spaces in the raw HTML.

// Create a regex pattern for the error message (case-insensitive optional)
const akamaiErrorPattern = /Service Unavailable - Zero size object/;

// Test if the error string matches the pattern
if (akamaiErrorPattern.test(error)) {
  // Execute your error handling logic here
}

The test() method checks the entire string, including across line breaks, so it won't fail just because the error message is split across multiple lines in the HTML.

3. Combine with HTTP Status Code Checks (Extra Rigor)

Akamai's "Service Unavailable" response almost always comes with a 503 HTTP status code. Adding a status code check first can help you narrow down the error type before verifying the specific message, reducing false positives.

Here's an example using the Fetch API:

fetch('your-api-endpoint')
  .then(response => {
    if (!response.ok) {
      // Check for 503 status first
      if (response.status === 503) {
        return response.text().then(html => {
          // Now verify the specific error message
          if (/Service Unavailable - Zero size object/.test(html)) {
            alert('Service temporarily unavailable. Please try again later.');
          }
        });
      }
      // Handle other non-OK status codes here
    }
    // Process successful response
  })
  .catch(networkError => {
    // Handle network failures, CORS issues, etc.
  });

This two-step check ensures you're only acting on the exact Akamai error you care about, not other 503 errors from different services.


内容的提问来源于stack exchange,提问作者Happy Coder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 07:57:42