How can you create custom error objects?
Quiz · JavaScript
Medium
6 min
errors
classes
TL;DR
Extend Error with a class, call super(message, options) (so cause works), set this.name, and add domain fields like status or code. Callers can then use instanceof or a stable code to branch, and you keep stack traces. Use custom errors to distinguish kinds of failure (validation, not found, network) at boundaries, not for every possible message.
The modern pattern
class AppError extends Error {
constructor(message, { code, cause, details } = {}) {
super(message, { cause }); // ES2022: preserves the underlying error
this.name = new.target.name; // 'AppError', or the subclass name
this.code = code; // stable, machine-readable identifier
this.details = details;
}
}
class HttpError extends AppError {
constructor(response, body) {
super(`HTTP ${response.status} for ${response.url}`, { code: `HTTP_${response.status}` });
this.status = response.status;
this.body = body;
}
get isRetryable() {
return this.status >= 500 || this.status === 429;
}
}
class ValidationError extends AppError {
constructor(fieldErrors) {
super('Validation failed', { code: 'VALIDATION', details: fieldErrors });
}
}
Usage:
async function request(url, init) {
let res;
try {
res = await fetch(url, init);
} catch (err) {
throw new AppError('Network unavailable', { code: 'NETWORK', cause: err });
}
if (!res.ok) throw new HttpError(res, await res.text());
return res.json();
}
try {
await request('/api/orders', { method: 'POST', body });
} catch (err) {
if (err instanceof ValidationError) showFieldErrors(err.details);
else if (err instanceof HttpError && err.status === 401) redirectToLogin();
else if (err instanceof HttpError && err.isRetryable) scheduleRetry();
else throw err;
}
Details that matter
name: without it, stack traces andString(err)showError. Settingthis.name = new.target.name(orthis.constructor.name) makes subclasses label themselves. Minifiers can mangle class names, so for reporting and branching prefer explicitcodestrings, or hard-codename.- Stack traces: native
class extends Errorcaptures them automatically. In V8 you can hide constructor frames withError.captureStackTrace?.(this, new.target). instanceofcaveats: it fails across realms (iframes, separate bundles shipping duplicate copies of your error module, worker boundaries). Checkingerr.codeorerr.nameis more robust.Error.isError()is a new built-in for realm-safe "is this an Error?" checks; verify support.- Old transpilation: compiling
extends Errorto ES5 broke the prototype chain (instanceofreturned false). Targeting ES2015+ fixes it; with legacy output you neededObject.setPrototypeOf(this, new.target.prototype). - Serialization:
JSON.stringify(err)gives{}for built-in properties (message,stackare non-enumerable), but includes your own fields. Add atoJSON()if errors cross the network or go to logs.structuredCloneandpostMessagekeepname/message(andcausein newer engines) for built-in error types only. - TypeScript: add
override readonly name = 'HttpError'or declare fields; narrowunknownincatchwithinstanceofor a type guard.
Design advice
- Keep a small hierarchy: a base
AppErrorplus a few meaningful categories. Explosions of one-off classes are hard to handle consistently. - Wrap, don't replace: always pass the low-level error as
cause. - Separate the user-facing message (translated UI copy) from the developer message. Map
codeto UI text at the edge. - Mark operational errors (expected, recoverable) differently from programmer bugs so monitoring can prioritise.
In an interview, mention…
class extends Error,super(message, { cause }),name, and acode.instanceoflimitations across realms and with old transpilation.- How custom errors map to handling: retry, redirect, field errors, generic fallback.
References
Spotted a mistake or a rights concern? Report an issue