node-comfortv2.0.0

Errors

Every error node-comfort throws extends nc.errors.NodeComfortError, which extends Error. Each has a name (the class) and a stable code, so you can react to a specific failure without parsing messages.

try {
  await nc.http.get("https://api.example.com/users/42");
} catch (error) {
  if (error instanceof nc.errors.HttpError && error.status === 404) return null;
  throw error;
}

The classes

ClasscodeThrown byExtra fields
AssertionErrorERR_ASSERTIONassert, assertType
TimeoutErrorETIMEDOUTfunc.timeout, http, async.poll, emitter.waitFor...timeout
AbortErrorABORT_ERRanything cancelled with an AbortSignal
ValidationErrorERR_VALIDATIONschema, env.validateissues, flatten()
HttpErrorERR_HTTP, ERR_NETWORKhttpstatus, statusText, url, method, headers, data
JWTErrorERR_JWT_*crypto.verifyJWTexpiredAt
ProcessErrorERR_PROCESS, ETIMEDOUTsys.run, sys.execcommand, exitCode, signal, stdout, stderr, timedOut
SQLiteErrorERR_SQLITESQLitesql, sqliteCode

AbortError is named "AbortError", like the native one, so checks such as error.name === "AbortError" keep working.

Validation errors

A ValidationError lists every problem, not just the first. Each issue has a path, a code and a readable message:

const result = User.safeParse(body);
if (!result.success) {
  result.error.issues;    // [{ path: ["email"], code: "invalid_string", message: "Invalid email address" }]
  result.error.flatten(); // { email: ["Invalid email address"] }, handy for forms
}

Causes

When an error wraps another one, the original is kept as cause, and the logger prints the whole chain:

catch (error) {
  nc.error(error); // shows the SQLiteError and the native error that caused it
}

Importing the classes

const { HttpError, ValidationError } = require("@ix-xs/node-comfort/errors");
import { TimeoutError } from "@ix-xs/node-comfort/errors";
Full referenceEvery function in nc.errors, with its parameters and types.
9 entries →
node-comfort v2.0.0Edit this page on GitHub