Skip to content

Errors ​

sensored uses a single error class — SensoredError — with a code field that identifies the specific error type. All error messages are library-defined and contain no caller-supplied values, making them safe to expose.

Error codes ​

CodeDefault HTTP StatusDescription
INVALID_CONFIG400Configuration or input has an invalid shape
UNKNOWN_RULE400Configuration contains an unknown rule or preset
EMPTY_POLICY400No rules are enabled
INPUT_LIMIT413Input exceeds the complete-string limit
POLICY_CONFLICT409Presets contain conflicting rule settings
STREAM_UNSUPPORTED400One or more active rules don't support streaming
BUFFER_LIMIT413Streaming buffer exceeded the configured limit
DETECTOR_CONTRACT500A detector violated its declared contract
SOURCE_FAILURE424The stream source produced an error
CANCELLED499The operation was cancelled

SensoredError ​

ts
class SensoredError extends Error {
  readonly code: ErrorCode;
  readonly path?: string;
  readonly info?: Readonly<Record<string, unknown>>;
}
  • code — One of the error codes listed above
  • path — Optional configuration path where the error occurred (e.g., "presets", "rules", "detectors")
  • info — Optional structured metadata (e.g., available preset versions)

Handling errors ​

ts
import { createRedactor, SensoredError } from "sensored";

try {
  const redactor = createRedactor({ presets: ["unknown"], rules: {} });
} catch (error) {
  if (error instanceof SensoredError) {
    console.log(error.code); // "UNKNOWN_RULE"
    console.log(error.message); // "Configuration contains an unknown rule."
    console.log(error.path); // "presets"
  }
}

RFC 9457 Problem Details ​

SensoredError.toProblemDetails() serializes to an RFC 9457 Problem Details object. You can provide a custom status map to override default HTTP statuses:

ts
import { SensoredError } from "sensored";

const error = new SensoredError("INPUT_LIMIT");
const problem = error.toProblemDetails();

// {
//   type: "urn:sensored:error:input_limit",
//   title: "INPUT_LIMIT",
//   status: 413,
//   detail: "Input exceeds the complete-string limit.",
//   code: "INPUT_LIMIT"
// }

Custom status map:

ts
const problem = error.toProblemDetails({
  INPUT_LIMIT: 400,
});
// problem.status === 400

Supplied status mappings must respect the only-500-in-5xx rule. Any

5xx status other than 500 will throw an error. :::

ProblemDetails interface ​

ts
interface ProblemDetails {
  readonly type: string;
  readonly title: string;
  readonly status: number;
  readonly detail: string;
  readonly code: ErrorCode;
  readonly path?: string;
  readonly [key: string]: unknown;
}

The type field is a URN in the format urn:sensored:error:{code}.

Released under the MIT License.