Rule Cascade
LearnUse it in your language

Handle errors

Handle load errors, malformed requests (BAD_REQUEST) and RULE-EVALUATION-ERROR in every runtime.

Three kinds of error matter. A load error means the rules must not be served: stop. A malformed request (BAD_REQUEST over the engine protocol) is the caller's fault: answer 400. A rule that cannot be evaluated never passes silently: it becomes a blocking RULE-EVALUATION-ERROR finding, the decision is deny, and you alert on it.

What to do. A malformed request throws RequestError. A rule that cannot be evaluated does not throw: it comes back as a blocking RULE-EVALUATION-ERROR finding, so the form stays blocked. When the rule server is down, createManifestClient answers with its last manifest and reports it as stale.

The code.

packages/client/test/react.test.ts
it('throws RequestError for a request that does not have the shape of an evaluation request', () => {
  const bad = { entity: 'Order', operation: 'create', data: [1] } as unknown as EvaluationRequest;
  expect(() => renderHook(() => useRuleEvaluation(manifest, bad))).toThrow(RequestError);
packages/client/test/manifest-cache.test.ts
it('answers with the cached copy when the server cannot be reached, and says so through onStale and stale()', async () => {
  const first = manifest('sha256:aaa');
  const seen: StaleManifest[] = [];
  const { fetch } = stub(ok(first, '"a"'), failing(), status(503), status(429), () => new Response('<html>gateway</html>', { status: 200 }), notModified);
  const client = createManifestClient({ baseUrl: 'https://r', fetch, onStale: (s) => void seen.push(s) });
  await client.get('a');
  for (const reason of ['fetch failed', 'manifest a: HTTP 503', 'manifest a: HTTP 429', /JSON/]) {
    expect(await client.get('a')).toEqual(first);
    expect(client.stale('a')?.error.message).toMatch(reason);
  }
  expect(seen).toHaveLength(4);

Run it.

Run it
npm test -w @rules-cascade/core

The output.

These are CI tests; they pass on every build. The same errors in Node.js, with their output, are in the TypeScript (Node.js) tab.

Try it YourselfRuns in your browser with the TypeScript engine. Nothing to install.
Course overview