CodeSampleX

サンプル

http-errors 2.0.0: createError

検証済みサンプル — npm http-errors 2.0.0: createError. node 22 · linux alpine/x64 · docker で contract を実行し、成功しました: assert createError creates 4xx HttpError with…

sha256:c734337956cda8234887d3eb56b4bc560e060b46d7f564e3111db69ce3d10fd1

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
2
ビルドした署名鍵
1
宣言された環境 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

検証実行環境

環境 コントラクト ステージ 実行日
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 SKIPPED compile:SKIPPED · contract:SKIPPED · load:SKIPPED · resolve:FAIL
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-30
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-30

ケース

HOW
ゴール
verify createError in pkg:npm/http-errors@2.0.0
パッケージ
シンボル
  • createError
環境
node 22.23.2
作成日
2026-08-30T09:48:55Z

コントラクト

  1. assert createError creates 4xx HttpError with status, statusCode, name, and expose true by default
  2. assert createError creates 5xx HttpError with expose false by default
  3. assert createError attaches custom properties and respects custom expose override
  4. assert createError wraps existing Error preserving original message and assigning status
  5. assert named error constructors create corresponding HttpError instances
  6. assert isHttpError identifies HttpError instances and rejects standard Errors or non-errors

ファイル

  • PROMPT.md
  • csx.json
  • index.js
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs

ソースアーティファクトをダウンロード (tar.gz)

ソース

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify createError in pkg:npm/http-errors@2.0.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/http-errors@2.0.0
Demonstrate these symbols/APIs:
  - createError

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:9e00c147f97de8f2856d0c20c9c01601bf0e1085f62192757f77e3fcbff4d9cf","contract":["assert createError creates 4xx HttpError with status, statusCode, name, and expose true by default","assert createError creates 5xx HttpError with expose false by default","assert createError attaches custom properties and respects custom expose override","assert createError wraps existing Error preserving original message and assigning status","assert named error constructors create corresponding HttpError instances","assert isHttpError identifies HttpError instances and rejects standard Errors or non-errors"],"goal":"verify createError in pkg:npm/http-errors@2.0.0","kind":"HOW","packages":["pkg:npm/http-errors@2.0.0"],"schemaVersion":1,"symbols":["createError"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/http-errors@2.0.0"],"schemaVersion":1,"subject":"pkg:npm/http-errors@2.0.0","symbols":["createError"],"verifierAdapter":"node-typescript@1"}
index.js
const createError = require('http-errors');

function makeHttpError(...args) {
  return createError(...args);
}

function makeClientError(status, message, props) {
  if (typeof status !== 'number' || status < 400 || status >= 500) {
    throw new Error('Status code must be 4xx');
  }
  if (props !== undefined) {
    return createError(status, message, props);
  }
  if (message !== undefined) {
    return createError(status, message);
  }
  return createError(status);
}

function makeServerError(status, message, props) {
  if (typeof status !== 'number' || status < 500 || status >= 600) {
    throw new Error('Status code must be 5xx');
  }
  if (props !== undefined) {
    return createError(status, message, props);
  }
  if (message !== undefined) {
    return createError(status, message);
  }
  return createError(status);
}

function wrapError(status, originalError) {
  return createError(status, originalError);
}

function checkHttpError(err) {
  return createError.isHttpError(err);
}

module.exports = {
  createError,
  makeHttpError,
  makeClientError,
  makeServerError,
  wrapError,
  checkHttpError,
};
package-lock.json
{
  "name": "sample-http-errors",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-http-errors",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "http-errors": "2.0.0"
      }
    },
    "node_modules/depd": {
      "version": "2.0.0",
      "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
      "integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.8"
      }
    },
    "node_modules/http-errors": {
      "version": "2.0.0",
      "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.0.tgz",
      "integrity": "sha512-FtwrG/euBzaEjYeRqOgly7G0qviiXoJWnvEH2Z1plBdXgbyjv34pHTSb9zoeHMyDy33+DWy5Wt9Wo+TURtOYSQ==",
      "license": "MIT",
      "dependencies": {
        "depd": "2.0.0",
        "inherits": "2.0.4",
        "setprototypeof": "1.2.0",
        "statuses": "2.0.1",
        "toidentifier": "1.0.1"
      },
      "engines": {
        "node": ">= 0.8"
      }
    },
    "node_modules/inherits": {
      "version": "2.0.4",
      "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
      "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",
      "license": "ISC"
    },
    "node_modules/setprototypeof": {
      "version": "1.2.0",
      "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz",
      "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==",
      "license": "ISC"
    },
    "node_modules/statuses": {
      "version": "2.0.1",
      "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz",
      "integrity": "sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.8"
      }
    },
    "node_modules/toidentifier": {
      "version": "1.0.1",
      "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz",
      "integrity": "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==",
      "license": "MIT",
      "engines": {
        "node": ">=0.6"
      }
    }
  }
}
package.json
{
  "name": "sample-http-errors",
  "version": "1.0.0",
  "description": "Minimal clean-room sample verifying createError in http-errors",
  "main": "index.js",
  "license": "MIT-0",
  "dependencies": {
    "http-errors": "2.0.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify createError in pkg:npm/http-errors@2.0.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/http-errors@2.0.0"
  ],
  "symbols": [
    "createError"
  ]
}
test/contract.mjs
import assert from 'node:assert';
import createError from 'http-errors';
import {
  makeHttpError,
  makeClientError,
  makeServerError,
  wrapError,
  checkHttpError,
} from '../index.js';

// 1. assert createError creates 4xx HttpError with status, statusCode, name, and expose true by default
const notFoundErr = createError(404, 'Resource not found');
assert.strictEqual(notFoundErr.status, 404);
assert.strictEqual(notFoundErr.statusCode, 404);
assert.strictEqual(notFoundErr.name, 'NotFoundError');
assert.strictEqual(notFoundErr.message, 'Resource not found');
assert.strictEqual(notFoundErr.expose, true);
assert.ok(notFoundErr instanceof Error);
assert.ok(notFoundErr instanceof createError.HttpError);
assert.ok(notFoundErr instanceof createError.NotFound);

const helperClientErr = makeClientError(403, 'Forbidden access');
assert.strictEqual(helperClientErr.status, 403);
assert.strictEqual(helperClientErr.statusCode, 403);
assert.strictEqual(helperClientErr.name, 'ForbiddenError');
assert.strictEqual(helperClientErr.expose, true);

// 2. assert createError creates 5xx HttpError with expose false by default
const serverErr = createError(500, 'Internal database failure');
assert.strictEqual(serverErr.status, 500);
assert.strictEqual(serverErr.statusCode, 500);
assert.strictEqual(serverErr.name, 'InternalServerError');
assert.strictEqual(serverErr.message, 'Internal database failure');
assert.strictEqual(serverErr.expose, false);
assert.ok(serverErr instanceof createError.InternalServerError);

const helperServerErr = makeServerError(503, 'Service unavailable');
assert.strictEqual(helperServerErr.status, 503);
assert.strictEqual(helperServerErr.expose, false);

// 3. assert createError attaches custom properties and respects custom expose override
const customErr = createError(400, 'Invalid parameter payload', {
  code: 'ERR_INVALID_PARAM',
  field: 'username',
  expose: false,
});
assert.strictEqual(customErr.status, 400);
assert.strictEqual(customErr.statusCode, 400);
assert.strictEqual(customErr.name, 'BadRequestError');
assert.strictEqual(customErr.code, 'ERR_INVALID_PARAM');
assert.strictEqual(customErr.field, 'username');
assert.strictEqual(customErr.expose, false);

const helperCustom = makeHttpError(422, 'Unprocessable entity', {
  details: [{ field: 'email', reason: 'invalid format' }],
});
assert.strictEqual(helperCustom.status, 422);
assert.strictEqual(helperCustom.statusCode, 422);
assert.deepStrictEqual(helperCustom.details, [{ field: 'email', reason: 'invalid format' }]);
assert.strictEqual(helperCustom.expose, true);

// 4. assert createError wraps existing Error preserving original message and assigning status
const originalError = new Error('Connection refused');
const wrapped = wrapError(502, originalError);
assert.strictEqual(wrapped.status, 502);
assert.strictEqual(wrapped.statusCode, 502);
assert.strictEqual(wrapped.message, 'Connection refused');
assert.strictEqual(wrapped.expose, false);
assert.strictEqual(wrapped, originalError);

// 5. assert named error constructors create corresponding HttpError instances
const badRequest = new createError.BadRequest('Missing required headers');
assert.strictEqual(badRequest.status, 400);
assert.strictEqual(badRequest.statusCode, 400);
assert.strictEqual(badRequest.name, 'BadRequestError');
assert.strictEqual(badRequest.message, 'Missing required headers');
assert.strictEqual(badRequest.expose, true);
assert.ok(badRequest instanceof createError.BadRequest);
assert.ok(badRequest instanceof createError.HttpError);
assert.ok(badRequest instanceof Error);

const unauthorized = new createError.Unauthorized('Bearer token required');
assert.strictEqual(unauthorized.status, 401);
assert.strictEqual(unauthorized.statusCode, 401);
assert.strictEqual(unauthorized.name, 'UnauthorizedError');
assert.strictEqual(unauthorized.expose, true);

// 6. assert isHttpError identifies HttpError instances and rejects standard Errors or non-errors
assert.strictEqual(createError.isHttpError(notFoundErr), true);
assert.strictEqual(createError.isHttpError(badRequest), true);
assert.strictEqual(createError.isHttpError(wrapped), true);
assert.strictEqual(checkHttpError(helperServerErr), true);

assert.strictEqual(createError.isHttpError(new Error('plain error')), false);
assert.strictEqual(createError.isHttpError('string error'), false);
assert.strictEqual(createError.isHttpError(null), false);
assert.strictEqual(createError.isHttpError(undefined), false);
assert.strictEqual(createError.isHttpError({ status: 500 }), false);

console.log('All http-errors contract assertions passed.');

オリジンシーダー

匿名