CError

The CError class extends native Error with HTTP status codes and optional data payload. It provides a toResponse() method for converting errors into CResponse objects, making it ideal for consistent error handling across your application.

Contents
  1. Usage
  2. Constructor Parameters
  3. Properties
  4. Methods

Usage

Throwing errors in routes

import { C } from "@ozanarslan/corpus";

new C.Route("/users/:id", (c) => {
	const user = findUser(c.params.id);

	if (!user) {
		throw new C.Error("User not found", C.Status.NOT_FOUND);
	}

	return user;
});

With custom data payload

new C.Route("/validate", (c) => {
	const errors = validate(c.body);

	if (errors.length > 0) {
		throw new C.Error("Validation failed", C.Status.BAD_REQUEST, { errors });
	}

	return { valid: true };
});

Converting to response

new C.Route("/handle", async (c) => {
	try {
		return await riskyOperation();
	} catch (err) {
		if (err instanceof C.Error) {
			// this is already done in the Server.handleError
			return err.toResponse();
		}
		throw err;
	}
});

Checking error status

new C.Middleware({
	variant: "inbound",
	useOn: "*",
	handler: (c) => {
		try {
			return c.next();
		} catch (err) {
			if (err instanceof C.Error && err.isStatusOf(C.Status.UNAUTHORIZED)) {
				c.res.headers.set("X-Auth-Required", "true");
			}
			throw err;
		}
	},
});

Constructor Parameters

message

string

The error message. Available on error.message.

status

Status

HTTP status code for this error. Use C.Status enum for standard codes.

data (optional)

unknown

Additional data to include in the response. If a CResponse is passed, it will be modified with the status code and returned as-is from toResponse().

Properties

Property Type Description
message string Error message (inherited from Error)
status Status HTTP status code
data unknown Optional additional payload

Methods

toResponse

toResponse(): CResponse

Converts the error to a CResponse:

  • If data is a CResponse — returns it with status applied
  • Otherwise — returns JSON response with { error, message } shape
// With plain data
new C.Error("Not found", C.Status.NOT_FOUND).toResponse();
// → CResponse with body { error: true, message: "Not found" }

// With CResponse data
new C.Error(
	"Failed",
	C.Status.BAD_REQUEST,
	new C.Response("custom"),
).toResponse();
// → The passed CResponse with status 400

isStatusOf

isStatusOf(status: Status): boolean

Checks if the error matches a specific HTTP status code.

const err = new C.Error("Not found", C.Status.NOT_FOUND);
err.isStatusOf(C.Status.NOT_FOUND); // true
err.isStatusOf(C.Status.BAD_REQUEST); // false