CHAN.RUN

ensure

ensure

ensure()

function ensure<T>(
   value, 
   errorOrMessage, 
   message?, 
options?): NonNullable<T>;

Defined in: src/ensure.ts

Assert non-null/undefined. Returns the narrowed value or throws.

Three forms:

// Full — typed error class + message
const user = ensure(db.find(id), NotFoundError, `No user: ${id}`);

// Class only — message defaults to error name
const user = ensure(db.find(id), NotFoundError);

// String only — throws EnsureError with your message
const user = ensure(db.find(id), "Could not find user");

Type Parameters

Type Parameter
T

Parameters

ParameterType
valueT
errorOrMessagestring | FaultErrorClass
message?string
options?{ cause?: unknown; }
options.cause?unknown

Returns

NonNullable<T>

Examples

import { ensure, defineError } from "@chan.run/ensure";

const UserNotFoundError = defineError("UserNotFoundError");

async function getUser(id: string) {
  const row = await db.users.findUnique({ where: { id } });
  return ensure(row, UserNotFoundError, `No user with id ${id}`);
}
const databaseUrl = ensure(process.env.DATABASE_URL, "DATABASE_URL is required");
const apiKey = ensure(process.env.API_KEY, "API_KEY is required");
app.get("/users/:id", (req, res) => {
  const id = ensure(req.params.id, ValidationError, "Missing user ID");
  // id is narrowed to string, never undefined
});
function UserProfile({ userId }: { userId: string }) {
  const { data } = useQuery(["user", userId], () => fetchUser(userId));
  const user = ensure(data, "User data not loaded");

  return <h1>{user.name}</h1>;
}
const params = new URLSearchParams(window.location.search);
const token = ensure(params.get("token"), "Missing token in URL");
try {
  await connectToDatabase();
} catch (err) {
  ensure(null, DbError, "Connection failed", { cause: err });
}