jeessy2/ddns-go · error · Error

throw new Error(resp)

Error message

throw new Error(resp)

What it means

The getLogs function fetches log entries from the ./logs endpoint via request.get. If the response is not an array, the code treats it as an error payload and throws `new Error(resp)`, so the raw (often string/JSON error object) response becomes the Error message. It surfaces in the UI via showMessage with type 'error'.

Source

Thrown at web/writing.html:759

  document.getElementById("index").addEventListener('change', e => {
    configIndex = parseInt(e.target.value);
    showConf(configIndex);
  });

  // 初始化dnsConf
  reloadConf("{{.DnsConf}}");
</script>

<!-- 日志相关函数和日志初始化 -->
<script>
  // 获取日志
  const getLogs = async (loop = false) => {
    let logsList = [];
    try {
      const resp = await request.get("./logs");
      // 如果不是数组,说明返回的是错误信息
      if (!Array.isArray(resp)) {
        throw new Error(resp);
      }
      logsList = resp;
    } catch (err) {
      showMessage({
        content: err.toString(),
        type: "error",
        duration: 5000,
      });
      return;
    } finally {
      if (loop) {
        setTimeout(getLogs, 5 * 1000, true);
      }
    }
    const $logs = document.getElementById("logs");
    // 判断滚动条是否在底部
    const isBottom = $logs.scrollHeight - $logs.scrollTop - $logs.clientHeight < 10;
    $logs.value = logsList.join("");

View on GitHub (pinned to 5874c2e666)

Solutions

  1. Check why ./logs returned a non-array: log the raw resp before throwing to see the actual payload
  2. Verify you are authenticated (cookie/token) when calling ./logs; re-login if the session expired
  3. Fix the server/route so the endpoint always returns a JSON array of logs, or the client so it parses resp correctly
  4. Change the throw to include context: throw new Error('logs response is not an array: ' + JSON.stringify(resp))

Example fix

// before
if (!Array.isArray(resp)) {
  throw new Error(resp);
}
// after
if (!Array.isArray(resp)) {
  throw new Error('获取日志失败: ' + (typeof resp === 'string' ? resp : JSON.stringify(resp)));
}
Defensive patterns

Strategy: type-guard

Validate before calling

const resp = await request.get('./logs');
if (!Array.isArray(resp)) {
  // handle error payload before proceeding
  console.error('logs endpoint returned non-array:', resp);
}

Type guard

function isLogArray(v) {
  return Array.isArray(v) && v.every((e) => typeof e === 'object' && e !== null);
}

Try / catch

try {
  const resp = await request.get('./logs');
  if (!Array.isArray(resp)) {
    throw new Error('日志接口返回异常: ' + JSON.stringify(resp));
  }
  logsList = resp;
} catch (err) {
  showMessage({ content: err.toString(), type: 'error' });
}

Prevention

When it happens

Trigger: Calling getLogs() when the ./logs endpoint returns a non-array body — e.g. an auth/permission error page, a JSON object like {code,msg}, HTML from a proxy, or an empty/plain-text error string instead of the expected log array.

Common situations: Session expired or not logged in so the server returns an error object; reverse proxy or CDN intercepts the request and returns HTML; backend bug changed the response shape; wrong URL rewritten by a router returning an error page.

Related errors


AI-assisted analysis of jeessy2/ddns-go@5874c2e666 (2026-09-03). Data as JSON: /api/errors/17085738c7ab3b3a. Report an issue: GitHub.