@nice-code/common-errors
v0.86.0
Published
Readme
@nice-code/common-errors
Docs: nicecode.io — guides, integrations, and the full API surface. Working with an AI assistant? Point it at nicecode.io/llms-common-errors.txt (just this package) or nicecode.io/llms.txt (the whole stack) — the complete, current docs flattened into plain text.
Shared error domains for Standard Schema validation errors, with Hono middleware integration.
Install
bun add @nice-code/common-errorsPeer deps: valibot (or any Standard Schema library), hono (for /hono subpath).
Validation error domain
err_validation is a @nice-code/error domain for Standard Schema validation failures. Import it to match or inspect validation errors by domain.
import { err_validation, EValidator } from "@nice-code/common-errors";
// Check if an error is a validation error
if (err_validation.isExact(caught)) {
const hydrated = err_validation.hydrate(caught);
const { issues } = hydrated.getContext(EValidator.standard_schema);
// issues: readonly StandardSchemaV1.Issue[]
}The domain exposes one error id: EValidator.standard_schema with context { issues }.
Hono middleware
Import from the /hono subpath:
import { niceSValidator, niceCatchSValidation } from "@nice-code/common-errors/hono";niceSValidator(target, schema)
Drop-in replacement for @hono/standard-validator's sValidator that throws a NiceError instead of returning a 400 response when validation fails.
import { niceSValidator } from "@nice-code/common-errors/hono";
import * as v from "valibot";
const CreateUserSchema = v.object({
name: v.string(),
email: v.pipe(v.string(), v.email()),
});
app.post("/users", niceSValidator("json", CreateUserSchema), async (c) => {
const body = c.req.valid("json"); // fully typed
// ...
});When validation fails, a NiceError from the err_validation domain is thrown. Use Hono's onError handler to convert it to a JSON response:
import { castNiceError } from "@nice-code/error";
app.onError((err, c) => {
const niceError = castNiceError(err);
return c.json(niceError.toJsonObject(), niceError.httpStatusCode as any);
});niceCatchSValidation()
Middleware that intercepts raw @hono/standard-validator validation responses (the default { success: false, error: [...] } shape) and converts them into NiceError JSON responses. Use this when you can't replace sValidator with niceSValidator directly.
app.use(niceCatchSValidation());
// Existing sValidator usage is automatically intercepted
app.post("/data", sValidator("json", MySchema), handler);Full Hono example
import { Hono } from "hono";
import { niceSValidator } from "@nice-code/common-errors/hono";
import { castNiceError, EErrorPackType } from "@nice-code/error";
import * as v from "valibot";
const app = new Hono();
app.onError((err, c) => {
const niceError = castNiceError(err);
return c.json(niceError.toJsonObject(), niceError.httpStatusCode as any);
});
const BodySchema = v.object({ message: v.string() });
app.post("/echo", niceSValidator("json", BodySchema), async (c) => {
const { message } = c.req.valid("json");
return c.json({ echo: message });
});