Create custom error classes in JavaScript

June 24, 2022  ‐ 1 min read

Creating your own exception hierarchy for your JavaScript can be convenient in error monitoring and catching exceptions.

To set up your own error classes in JavaScript we need to extend the base Error type.

class ApiRequestError extends Error {
  constructor(message) {
    super(message); = "ApiRequestError";

Next, in our JavaScript code we can throw errors of our newly created error type.

throw new ApiRequestError("Failed to fetch resource.");