Creating elegant custom exceptions is easy with ecmascript classes.

In most languages we subclass the base Exception class to create our own exceptions. But since javascript did not have classes until ES5, creating custom exceptions was cumbersome. So much that people usually just throw the standard Error object with the desired custom message string.

But now that ES6 has classes, we can just extend Error:

class BufferFullError extends Error {}

And there we have our own custom Error! Except it does not work.

    Expected the function to throw an error of type:
    Instead, it threw:

The reason is babel-6 (a common transpiler used to convert modern ES6 code to ES5 so that it runs in most browsers) does not support subclassing of built-in classes.

But there is a plugin we can use to achieve this: babel-plugin-transform-builtin-extend.

Install it via npm or yarn and add this to babel config:

"plugins": [
  ["babel-plugin-transform-builtin-extend", {
    "globals": ["Error", "Array"]

Protip: It is a good practice to add a custom error message:

export class BufferFullException extends Error {
  constructor(message) {
    this.message = message || 'Buffer is full.';


1 . More info on why babel does not support it:

2 . I learnt it because of a problem I was solving with exercism: