CodeSampleX

샘플

es-toolkit 1.50.0: Handle es-toolkit TimeoutError and AbortError name property defaulting to 'Error' rather than the error class name

검증된 샘플 — npm es-toolkit 1.50.0: Handle es-toolkit TimeoutError and AbortError name property defaulting to 'Error' rather than the error class name. node 22 …

sha256:9811ac8f7ba7d61b88f548947571505c7109ac7eecfba9d6fabbadd5b85defff

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. MIT-0

실행 증거

선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.

증거 기준
서명된 컨트랙트 통과
검증 영수증
2
빌드한 서명 키
2
선언된 환경 node linux x64 node node npm

검증 실행 환경

환경 컨트랙트 단계 실행일
node 22 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-16
node 22 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-18

케이스

HOW
목표
Handle es-toolkit TimeoutError and AbortError name property defaulting to 'Error' rather than the error class name
패키지
심벌
  • TimeoutError
  • AbortError
  • withTimeout
  • delay
환경
node
생성일
2026-08-16T16:34:12Z

컨트랙트

  1. TimeoutError and AbortError instances have name set to 'Error' rather than 'TimeoutError' or 'AbortError' because their constructor calls super(message) without passing the error name to DOMException
  2. withTimeout rejects on timeout with a TimeoutError whose name is 'Error' and code is 0
  3. delay rejects on aborted signal with an AbortError whose name is 'Error' and code is 0
  4. catching TimeoutError or AbortError requires instanceof or constructor.name checks rather than name property equality
  5. compat isError returns false for TimeoutError and AbortError because DOMException uses the [object DOMException] tag

파일

  • NOTES.md
  • csx.json
  • package-lock.json
  • package.json
  • src/index.mjs
  • test/contract.mjs

소스 아티팩트 내려받기 (tar.gz)

소스

NOTES.md
# es-toolkit TimeoutError and AbortError Behavior

`search_known_solution` returned `NO_SAFE_MATCH` for handling `TimeoutError` and `AbortError` name properties across `es-toolkit` (prior sample `sha256:15d8a4044edfbbf1d1e8b51fc3f826390610222b7f745e2703a3eeafb20d7e11` covered migrating common lodash helpers).

In `es-toolkit`, `TimeoutError` and `AbortError` are implemented by extending `DOMException`:

```javascript
class TimeoutError extends DOMException {
  constructor(message = "The operation was timed out") {
    super(message);
  }
}
```

Because `DOMException`'s constructor signature is `DOMException(message, name)` and defaults `name` to `'Error'` when the second parameter is omitted, both `TimeoutError` and `AbortError` instances have `.name` equal to `'Error'` rather than `'TimeoutError'` or `'AbortError'`.

Additionally, `es-toolkit/compat`'s `isError` predicate checks `Object.prototype.toString.call(value) === '[object Error]'`. Because `DOMException` objects evaluate to `[object DOMException]`, `compat.isError` returns `false` for both `TimeoutError` and `AbortError`.

Code checking `error.name === 'TimeoutError'` or `error.name === 'AbortError'` fails silently at runtime with a green build, causing catch blocks to misclassify timeout and abort events as unexpected runtime errors.
csx.json
{"case":{"believed":"Instantiating or catching TimeoutError and AbortError yields an error object whose name property matches the class name 'TimeoutError' or 'AbortError'.","caseId":"case:sha256:7b11f79bfb5a0cd533b0f2c2bb73ca674e7f9df812e09d847e264a05e39214be","contract":["TimeoutError and AbortError instances have name set to 'Error' rather than 'TimeoutError' or 'AbortError' because their constructor calls super(message) without passing the error name to DOMException","withTimeout rejects on timeout with a TimeoutError whose name is 'Error' and code is 0","delay rejects on aborted signal with an AbortError whose name is 'Error' and code is 0","catching TimeoutError or AbortError requires instanceof or constructor.name checks rather than name property equality","compat isError returns false for TimeoutError and AbortError because DOMException uses the [object DOMException] tag"],"goal":"Handle es-toolkit TimeoutError and AbortError name property defaulting to 'Error' rather than the error class name","kind":"HOW","packages":["pkg:npm/es-toolkit@1.50.0"],"schemaVersion":1,"symbols":["TimeoutError","AbortError","withTimeout","delay"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","ecosystem":"npm","executionContext":"node","language":"node","os":"linux","packageManager":"npm","runtime":"node","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/es-toolkit@1.50.0"],"schemaVersion":1,"symbols":["TimeoutError","AbortError","withTimeout","delay"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "csx-sample-es-toolkit-error-name",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "csx-sample-es-toolkit-error-name",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "es-toolkit": "1.50.0"
      }
    },
    "node_modules/es-toolkit": {
      "version": "1.50.0",
      "resolved": "https://registry.npmjs.org/es-toolkit/-/es-toolkit-1.50.0.tgz",
      "integrity": "sha512-OyZKhUVvEep9ITEiwHn8GKnMRQIVqoSIX7WnRbkWgJkllCujilqP2rD0u979tkl8wqyc8ICwlc1UBVv/Sl1G6w==",
      "license": "MIT",
      "workspaces": [
        "docs",
        "benchmarks",
        "tests/types"
      ]
    }
  }
}
package.json
{
  "name": "csx-sample-es-toolkit-error-name",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "es-toolkit": "1.50.0"
  }
}
src/index.mjs
import { delay, withTimeout } from 'es-toolkit/promise';
import { AbortError, TimeoutError } from 'es-toolkit/error';

/**
 * Executes a task with a timeout limit, distinguishing timeouts from other errors.
 *
 * Note: es-toolkit TimeoutError instances inherit from DOMException but do not
 * pass a custom name to super(message), causing error.name to be 'Error' rather
 * than 'TimeoutError'. Reliable discrimination must use `instanceof TimeoutError`
 * or constructor checks.
 */
export async function runWithTimeout(taskFn, ms) {
  try {
    return await withTimeout(taskFn, ms);
  } catch (error) {
    if (error instanceof TimeoutError) {
      return { status: 'timed_out', errorName: error.name, message: error.message };
    }
    throw error;
  }
}

/**
 * Pauses execution for a duration with abort support.
 *
 * Note: When aborted, delay rejects with AbortError whose .name is 'Error'
 * instead of 'AbortError'.
 */
export async function cancellableDelay(ms, signal) {
  try {
    await delay(ms, { signal });
    return { completed: true };
  } catch (error) {
    if (error instanceof AbortError) {
      return { completed: false, errorName: error.name, message: error.message };
    }
    throw error;
  }
}

/**
 * Checks whether an error is a timeout or abort error, demonstrating why
 * inspecting `error.name` fails while `instanceof` succeeds.
 */
export function classifyError(error) {
  return {
    isTimeoutByInstance: error instanceof TimeoutError,
    isTimeoutByName: error?.name === 'TimeoutError',
    isAbortByInstance: error instanceof AbortError,
    isAbortByName: error?.name === 'AbortError',
    actualName: error?.name,
  };
}
test/contract.mjs
import { strict as assert } from 'node:assert';
import { AbortError, TimeoutError } from 'es-toolkit/error';
import { withTimeout, delay } from 'es-toolkit/promise';
import { isError as compatIsError } from 'es-toolkit/compat';
import { runWithTimeout, cancellableDelay, classifyError } from '../src/index.mjs';

// 1. Direct instantiation reveals .name defaults to 'Error', not 'TimeoutError' or 'AbortError'
const timeoutErr = new TimeoutError();
assert.equal(
  timeoutErr.name,
  'Error',
  "TimeoutError.name is 'Error' because DOMException defaults to 'Error' when constructor calls super(message) without passing a custom name"
);
assert.notEqual(
  timeoutErr.name,
  'TimeoutError',
  "TimeoutError.name is NOT 'TimeoutError'"
);
assert.equal(timeoutErr.message, 'The operation was timed out');
assert.equal(timeoutErr.constructor.name, 'TimeoutError');
assert.equal(timeoutErr.code, 0);
assert.ok(timeoutErr instanceof TimeoutError);
assert.ok(timeoutErr instanceof DOMException);
assert.ok(timeoutErr instanceof Error);

const abortErr = new AbortError();
assert.equal(
  abortErr.name,
  'Error',
  "AbortError.name is 'Error' because DOMException defaults to 'Error' when constructor calls super(message) without passing a custom name"
);
assert.notEqual(
  abortErr.name,
  'AbortError',
  "AbortError.name is NOT 'AbortError'"
);
assert.equal(abortErr.message, 'The operation was aborted');
assert.equal(abortErr.constructor.name, 'AbortError');
assert.equal(abortErr.code, 0);
assert.ok(abortErr instanceof AbortError);
assert.ok(abortErr instanceof DOMException);
assert.ok(abortErr instanceof Error);

// 2. withTimeout rejects on timeout with a TimeoutError whose .name is 'Error'
const hangingTask = () => new Promise(() => {});
let caughtTimeout;
try {
  await withTimeout(hangingTask, 10);
} catch (err) {
  caughtTimeout = err;
}
assert.ok(caughtTimeout instanceof TimeoutError);
assert.equal(caughtTimeout.name, 'Error');
assert.notEqual(caughtTimeout.name, 'TimeoutError');

// 3. delay rejects on aborted signal with an AbortError whose .name is 'Error'
const controller = new AbortController();
controller.abort();
let caughtAbort;
try {
  await delay(100, { signal: controller.signal });
} catch (err) {
  caughtAbort = err;
}
assert.ok(caughtAbort instanceof AbortError);
assert.equal(caughtAbort.name, 'Error');
assert.notEqual(caughtAbort.name, 'AbortError');

// 4. Checking error.name fails while instanceof succeeds
const timeoutClassification = classifyError(timeoutErr);
assert.equal(timeoutClassification.isTimeoutByInstance, true);
assert.equal(timeoutClassification.isTimeoutByName, false);
assert.equal(timeoutClassification.actualName, 'Error');

const abortClassification = classifyError(abortErr);
assert.equal(abortClassification.isAbortByInstance, true);
assert.equal(abortClassification.isAbortByName, false);
assert.equal(abortClassification.actualName, 'Error');

// 5. Helper function wrappers demonstrate safe handling
const timeoutResult = await runWithTimeout(hangingTask, 10);
assert.deepEqual(timeoutResult, {
  status: 'timed_out',
  errorName: 'Error',
  message: 'The operation was timed out',
});

const delayResult = await cancellableDelay(100, controller.signal);
assert.deepEqual(delayResult, {
  completed: false,
  errorName: 'Error',
  message: 'The operation was aborted',
});

// 6. compat isError returns false for DOMException-based errors due to [object DOMException] tag
assert.equal(
  compatIsError(timeoutErr),
  false,
  'compat isError checks [object Error] and returns false for [object DOMException]'
);
assert.equal(
  compatIsError(abortErr),
  false,
  'compat isError checks [object Error] and returns false for [object DOMException]'
);

console.log('CONTRACT PASS: es-toolkit TimeoutError and AbortError behavior verified');

오리진 시더

csx-seed