Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- `@openstatus/health`: `onReport` catches rejected promises from other JavaScript
realms without an unhandled rejection.
- Health reports and responder fallbacks remain available when a rejected value
cannot convert to a string. The error uses generic text without private fields.
- Health responses ignore a top-level `toJSON` from `extend` so JSON
Expand Down
15 changes: 15 additions & 0 deletions packages/health/src/check.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from "node:assert/strict";
import test from "node:test";
import { setTimeout as delay } from "node:timers/promises";
import { runInNewContext } from "node:vm";
import { createHealthCheck } from "./check.ts";
import { DuplicateProbeError } from "./errors.ts";
import type { Probe } from "./types.ts";
Expand Down Expand Up @@ -150,6 +151,20 @@ test("createHealthCheck() swallows onReport errors", async () => {
assert.equal((await rejecting.report()).status, "ok");
});

test("createHealthCheck() swallows onReport rejections from another realm", async () => {
const { probe } = counting("a");
const ForeignPromise: PromiseConstructor = runInNewContext("Promise");
assert.notEqual(ForeignPromise, Promise);
const check = createHealthCheck({
probes: [probe],
onReport: () => ForeignPromise.reject(new Error("logger down")),
});
const report = await check.report();
assert.equal(report.status, "ok");
await delay(0);
assert.equal(await check.report(), report);
});

test("createHealthCheck() accepts the formatError presets", async () => {
const { probe } = counting("a", true);
const generic = await createHealthCheck({ probes: [probe] }).report();
Expand Down
3 changes: 1 addition & 2 deletions packages/health/src/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ export function createHealthCheck(options: HealthCheckOptions): HealthCheck {
function notify(onReport: OnReport | undefined, report: HealthReport): void {
if (onReport == null) return;
try {
const result = onReport(report);
if (result instanceof Promise) result.catch(() => {});
Promise.resolve(onReport(report)).catch(() => {});
} catch {
return;
}
Expand Down
Loading