createProblemDetailsResponse

import { createProblemDetailsResponse } from "jsr:@ende/http/unstable-problem-details";

function · experimental

function createProblemDetailsResponse<T extends ProblemDetailsExtensions>(problemDetails: ProblemDetails<T>, options?: ProblemDetailsResponseOptions): Response

Creates a Response with an application/problem+json body from a ProblemDetails object.

  • Sets Content-Type to application/problem+json, overwriting any Content-Type provided in options.headers.
  • Defaults type to "about:blank" if not provided.
  • Defaults title to the standard HTTP status text (from @ende/http/status) when type is "about:blank" and title is not provided.
  • Defaults status to 500 if not provided.
  • Serializes directly to JSON — the flat intersection type already matches the RFC wire format, so no flattening step is needed.
  • Forwards options.statusText to the Response only when defined; when omitted, the constructor's empty-string default is used and the standard HTTP status text is intentionally not synthesized — the JSON title field already carries that information.
problemDetails: ProblemDetails<T>

The problem details to serialize into the response body.

options?: ProblemDetailsResponseOptions

Additional response options such as extra headers.

Returns

A Response with status, application/problem+json content type, and the serialized problem details as the body.

Example: Basic 404 response

import { createProblemDetailsResponse } from "@ende/http/unstable-problem-details";
import { assertEquals } from "@ende/assert";

const response = createProblemDetailsResponse({
  status: 404,
  detail: "No user found with ID 42",
});
assertEquals(response.status, 404);
assertEquals(
  await response.json(),
  {
    type: "about:blank",
    status: 404,
    title: "Not Found",
    detail: "No user found with ID 42",
  },
);

Example: Custom problem type with extensions

import { createProblemDetailsResponse } from "@ende/http/unstable-problem-details";
import { assertEquals } from "@ende/assert";

const response = createProblemDetailsResponse({
  type: "https://example.com/problems/insufficient-credit",
  status: 403,
  title: "Insufficient credit",
  detail: "Your account balance is too low",
  instance: "/account/12345/transactions/abc",
  balance: 30,
  accounts: ["/account/12345", "/account/67890"],
});
assertEquals(response.status, 403);
assertEquals(response.headers.get("Content-Type"), "application/problem+json");

Source