MCPcopy Create free account
hub / github.com/denoland/std / normalizeParsedProblemDetails

Function normalizeParsedProblemDetails

http/unstable_problem_details.ts:249–289  ·  view source on GitHub ↗
(
  raw: unknown,
)

Source from the content-addressed store, hash-verified

247}
248
249function normalizeParsedProblemDetails(
250 raw: unknown,
251): Record<string, unknown> {
252 if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
253 throw new TypeError(
254 `Cannot parse Problem Details: expected a JSON object, but got ${
255 raw === null ? "null" : Array.isArray(raw) ? "an array" : typeof raw
256 }`,
257 );
258 }
259
260 const result: Record<string, unknown> = {};
261 const source = raw as Record<string, unknown>;
262
263 // RFC 9457 §3.1: ignore standard members whose value type does not match.
264 for (const key in source) {
265 if (!Object.hasOwn(source, key)) continue;
266 const value = source[key];
267 switch (key) {
268 case "type":
269 if (typeof value === "string") result.type = value;
270 break;
271 case "status":
272 if (Number.isInteger(value)) result.status = value;
273 break;
274 case "title":
275 if (typeof value === "string") result.title = value;
276 break;
277 case "detail":
278 if (typeof value === "string") result.detail = value;
279 break;
280 case "instance":
281 if (typeof value === "string") result.instance = value;
282 break;
283 default:
284 result[key] = value;
285 }
286 }
287
288 return result;
289}
290
291/**
292 * Parses a plain JSON value into a {@linkcode ProblemDetails}.

Callers 2

parseProblemDetailsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected