Errors
Blyp errors revolve around three pieces:
createError()for building and optionally logging aBlypErrorHTTP_CODESfor reusable status presetsparseError()for hydrating unknown payloads orResponseobjects back into aBlypError
createError()
import { createError } from "blyp-js";
throw createError({
status: 402,
code: "PAYMENT_DECLINED",
message: "Payment failed",
why: "Card declined by issuer",
fix: "Try a different payment method",
link: "https://docs.example.com/payments/declined",
details: { paymentId: "pay_123" },
});Important behavior
createError()returns a throwableBlypError.- If you only pass
status, Blyp uses the matching HTTP reason phrase. 4xxerrors log aterrorby default.5xxerrors log atcriticalby default.skipLogging: trueprevents automatic emission.loggerroutes the error through a request-scoped logger instead of the global root logger.logLevellets you override the default severity.
const error = createError({
status: 404,
message: "User not found",
skipLogging: true,
});
return Response.json(error.toJSON(), { status: error.status });parseError() on the server
The root parseError() accepts either a payload or a Response.
import { parseError } from "blyp-js";
const parsed = parseError({
error: {
status: 404,
message: "User not found",
fix: "Check the user id",
},
});const response = await fetch("https://api.example.com/payments");
if (!response.ok) {
const error = await parseError(response);
throw error;
}Response parsing behavior
- JSON bodies are preferred when the response is JSON.
- Nested
errorpayloads are unwrapped. - Text bodies become the message when JSON parsing fails.
response.statusis treated as authoritative even if the body contains a different status.- Empty bodies fall back to
statusText, then to the HTTP reason phrase.
parseError() in the browser
Use the browser-safe client entry:
import { parseError } from "blyp-js/client";
const response = await fetch("/api/users/does-not-exist");
if (!response.ok) {
throw await parseError(response);
}The client entry also re-exports HTTP_CODES, BlypError, and the related error types.
Optional parse-time logging
parseError() does not log by default, but it will emit exactly once when you pass a logger.
const error = await parseError(response, {
logger,
logLevel: "warning",
});Next step
Use HTTP Codes when you want reusable presets and custom error families instead of ad-hoc createError() calls.