Error handling in node js

Пост содержит перевод статьи «Error Handling in Node.js», которую подготовили сотрудники компании Joyent. Статья была опубликована 28 марта 2014 года на сайте...

Время прочтения
22 мин

Просмотры 68K

Пост содержит перевод статьи «Error Handling in Node.js», которую подготовили сотрудники компании Joyent. Статья была опубликована 28 марта 2014 года на сайте компании. Dave Pacheco поясняет, что статья призвана устранить неурядицу среди разработчиков, касаемо лучших практик работы с ошибками в Node.js, а так же ответить на вопросы, которые часто возникают у начинающих разработчиков.

Обработка ошибок в Node.js

По мере освоения Node.js можно достаточно долго писать программы, не уделяя при этом должного внимания корректной обработке ошибок. Однако, разработка серьёзных проектов на Node.js требует осознанного подхода к этой проблеме.

У начинающих разработчиков часто возникают следующие вопросы:

  • Можно ли использовать throw, что бы вернуть ошибку из функции или следует вызывать callback-функцию передав объект ошибки в качестве аргумента? В каких случаях необходимо генерировать событие 'error' у объекта класса EventEmitter?
  • Нужно ли производить проверку аргументов переданных функции? Что, если в функцию переданы некорректные аргументы? Нужно ли в таком случае генерировать исключение или вызывать callback-функцию, передавая ей ошибку?
  • Возможно ли программно различать ошибки по типу, что бы приложение могло соответствующим образом обрабатывать ошибки согласно их типу (например, «Bad Request» или «Service Unavailable»)?
  • Как функция может наиболее информативно «сообщить» программе о возникновении ошибки, чтобы та могла корректно её обработать?
  • Нужно ли обрабатывать ошибки вызванные «багами» в программе?


Данная статья состоит из семи частей:

  1. Введение. О том, что читатель должен знать перед ознакомлением со статьей.
  2. Программные ошибки и ошибки программиста. Ознакомление с типами ошибок.
  3. Шаблоны написания функций. Основополагающие принципы написания функций, реализующих корректную работу с ошибками.
  4. Правила написания функций. Перечень указаний которым следует придерживаться при написании функций.
  5. Пример. Пример написания функции.
  6. Резюме. Краткое представление основных положений рассмотренных в статье.
  7. Приложение. Общепринятые имена полей объектов ошибок.

1. Введение

Предполагается, что читатель:

  • знаком с термином «исключение» в JavaScript, Java, Python, C++, или другом подобном языке и понимает принцип работы конструкции try/catch;
  • знаком с разработкой на Node.js и освоил принципы асинхронного программирования.

Читатель должен понимать, почему в представленном ниже коде не работает перехват исключений, несмотря на наличие конструкции try/catch.1

function myFunc(callback)
{
  /*
   * Пример некорректного перехвата исключений
   */
  try {
    doSomeAsyncOperation(function (err) {
      if (err) {
        throw (err);
      }
    });
  } catch (ex) {
    callback(ex);
  }
}

Читателю следует знать, что в Node.js существует 3 основных способа, которыми функция может вернуть ошибку:

  1. Бросание ошибки throw (генерирование исключения).
  2. Вызов callback-функции с объектом ошибки в качестве первого аргумента.
  3. Генерирование события 'error' у объекта класса EventEmitter.

Предполагается, что читатель не знаком с доменами в Node.js.

Читатель должен понимать разницу между ошибкой и исключением в JavaScript. Ошибка — это любой объект класса Error. Ошибка может быть создана конструктором класса и возвращена из функции либо брошена с помощью инструкции ThrowStatement. Когда объект ошибки брошен, возникает исключение. Далее приведён пример бросания ошибки (генерирование исключения):2

throw new Error('произошла ошибка');

Пример, где ошибка передаётся в callback-функцию:

callback(new Error('произошла ошибка'));

Второй вариант чаще встречается в Node.js, из-за асинхронности большинства выполняемых операций. Как правило, первый вариант используется лишь при десериализации данных (например, JSON.parse), при этом брошенное исключение перехватывается с помощью конструкции try/catch. Это отличает Node.js от Java или C++ и других языков, где приходится чаще работать с исключениями.

2. Программные ошибки и ошибки программиста

Ошибки можно условно разделить на два типа:3

  • Программные ошибки представляют собой конфликты, возникающие в ходе нормального функционирования программы. Они не являются «багами». Обычно, они не связаны напрямую с программой: системные ошибки (например, переполнение памяти), ошибки конфигураций (например, неверно указан адрес удалённого сервера), ошибки интернет-соединения или ошибки возникшие на удалённом сервере.
    Примеры программных ошибок:

    • пользователь ввёл некорректные данные,
    • истекло время ожидания ответа на запрос (request timeout),
    • сервер ответил на запрос ошибкой с кодом 500,
    • разрыв соединения,
    • израсходована выделенная память.

  • Ошибки программиста — это дефекты кода, приводящие к некорректной работе программы. Ошибки данного типа не могут быть правильно обработаны, так как сам факт их наличия говорит о некорректности написанного кода. Ошибки этого типа возможно устранить изменив код программы. К ошибкам программиста можно отнести:
    • попытку обратиться к какому-либо полю у значения undefined,
    • вызов асинхронной функции без callback-функции,
    • вызов функции с некорректными аргументами.

Разработчики используют термин «ошибка» для обоих типов ошибок, несмотря на их принципиальные различия. «Файл не найден» — программная ошибка, её возникновение может означать, что программе требуется создать искомый файл. Таким образом, возникновение этой ошибки не является некорректным поведением программы. Ошибки программиста, напротив, не предполагались разработчиком. Возможно, разработчик ошибся в имени переменной или неправильно описал проверку данных, введённых пользователем. Данный тип ошибок не поддается обработке.

Возможны случаи, когда по одной и той же причине возникают как программная ошибка, так и ошибка программиста. Предположим, HTTP-сервер производит попытку считать какое-либо поле у значения undefined, что является ошибкой программиста. В результате, сервер выходит из строя. Клиент, при этом, в качестве ответа на свой запрос получает ошибку ECONNRESET, обычно описываемую Node.js как: «socket hang-up». Для клиента, это программная ошибка и корректно написанная программа-клиент соответствующим образом обработает ошибку и продолжит работу.

Отсутствие обработчика программной ошибки является ошибкой программиста. Предположим, что программа-клиент, устанавливая соединение с сервером, сталкивается с ECONNREFUSED ошибкой, в результате, объект соединения генерирует событие 'error', но для данного события не зарегистрирована ни одна функция-обработчик, по этой причине программа выходит из строя. В данном случае, ошибка соединения является программной ошибкой, однако, отсутствие обработчика для события ‘error’ объекта соединения — ошибка программиста.

Важно понимать различия между ошибками программиста и программными ошибками. Поэтому, прежде чем продолжать чтение статьи, убедитесь, что вы разобрались в этих понятиях.

Обработка программных ошибок

Обработка программных ошибок, так же как и вопросы безопасности или производительности приложения, не относится к тому типу задач, которые могут быть решены внедрением какого-либо модуля — невозможно в одном месте исходного кода решить все проблемы связанные с обработкой ошибок. Для решения задачи обработки ошибок требуется децентрализованный подход. Для всех участков программы, где возможно возникновение ошибки (обращение к файловой системе, соединение с удалённым сервером, создание дочернего процесса и т.д.) необходимо предписать соответствующие сценарии обработки для каждого возможного типа ошибки. Значит, необходимо не только выделить проблемные участки, но и понять каких типов ошибки могут в них возникнуть.

В некоторых случаях приходится передавать объект ошибки из функции, в которой она возникла, через callback-функцию на уровень выше, а из него еще выше, таким образом ошибка «всплывает» до тех пор, пока не достигнет логического уровня приложения, который ответственен за обработку данного типа ошибок. На ответственном уровне программа может принять решение: запустить ли проблемную операцию повторно, сообщить ли об ошибке пользователю или записать информацию об ошибке в лог-файл и пр. Не следует всегда полагаться на эту схему и передавать ошибки более высоким уровням иерархии, так как callback-функции на высоких уровнях ничего не знают о том, в каком контексте возникла переданная им ошибка. В результате, может возникнуть ситуация, когда на выбранном логическом уровне будет сложно описать логику обработки, соответствующую возникшей ошибке.

Выделим возможные сценарии обработки ошибок:

  • Устранение ошибки. Иногда, возникшую ошибку можно устранить. Предположим, возникла ошибка ENOENT, при попытке записать информацию в лог-файл. Это может означать, что программа запущена впервые и лог-файл еще не создан. В таком случае, обработчик может устранить ошибку, создав искомый файл. Приведём более интересный пример: программе необходимо постоянно поддерживать соединение с определённым севером (например, с базой данных), но в ходе работы возник разрыв соединения. В этом случае обработчик ошибки может произвести переподключение к базе данных.
  • Информирование пользователя и прекращение обработки запроса. Если нельзя решить возникшую проблему, проще всего прервать работу текущей операции, и сообщить пользователю об ошибке. Данный сценарий применим в случаях, когда известно, что причина, по которой возникла ошибка, не исчезнет с течением времени. К примеру, если ошибка возникла при попытке десериализации JSON-данных, переданных клиентом, то нет смысла повторять попытку с этими же данными.
  • Повторение операции. В случае ошибок связанных с работой по сети может помочь повторный запуск операции. Предположим, программа в ответ на запрос к удалённому сервису получила в ответе ошибку 503 (Service Unavailable error), в таком случае, возможно, стоит повторить запрос спустя несколько секунд. Важно определить конечное число повторов, а так же, с какой периодичностью должны выполняться попытки. Но не следует всегда полагаться на данный сценарий. Предположим, пользователь выполнил запрос к некоторому сервису, которому для обработки запроса потребовалось обратиться к вашей программе, а ваша программа, в свою очередь, осуществляет запрос к еще одному сервису, который ответил ошибкой 503. В этом случае, лучшим решением будет не выполнять повторных попыток, а незамедлительно дать возможность обработать ошибку исходному сервису, с которым работает пользователь. Если каждый сервис, участвующий в цепочке запросов, будет производить повторные попытки, то пользователь будет ожидать ответ на свой запрос дольше чем, если бы их выполнял только исходный сервис.
  • Прекращение работы программы. Если произошла непредвиденная ситуация, появление которой невозможно при нормальном функционировании программы, следует записать информацию об ошибке в соответствующий лог-файл и прекратить работу. Данный сценарий может быть использован, если ваша программа израсходовала доступную память (однако, если ваша программа получила ошибку ENOMEM от дочернего процесса, то ошибку можно обработать и не прекращать работу программы). Так же, данный сценарий можно применить если у вашей программы нет прав доступа к необходимым для работы файлам.
  • Запись ошибки в лог-файл и продолжение работы. В некоторых случаях нет необходимости прекращать работу программы даже если возникшая ошибка неустранима. В пример можно привести ситуацию, когда ваша программа периодически обращается к группе удалённых сервисов через систему DNS, и один из сервисов «выпал» из DNS. В данной ситуации программа может продолжить работу с оставшимися сервисами. Но, тем не менее, необходимо записать об ошибке в лог-файл. (Для любого правила всегда есть исключения, если ошибка возникает тысячу раз в секунду, и вы не можете ничего с ней поделать, то не нужно каждый раз выполнять запись в лог, однако, стоит периодически производить логирвоание.)

Обработка ошибок программиста

Не существует правильного способа обрабатывать ошибки программиста. По определению, если возникла такая ошибка, то код программы некорректен. Устранить проблему можно лишь исправив код.

Есть программисты считающие, что в некоторых случаях можно восстанавливать программу после произошедшей ошибки таким образом, что текущая операция прерывается, но программа, тем не менее, продолжает работать и обрабатывать другие запросы. Так поступать не рекомендуется. Принимая во внимание то, что ошибка программиста вводит программу в нестабильное состояние, можете ли вы быть уверены в том, что возникшая ошибка не нарушит работу других запросов? Если запросы работают с одними и теми же сущностями (например, сервер, сокет, соединения с базой данных и т.д.), остаётся лишь надеется, что последующие запросы будут правильно обработаны.

Рассмотрим REST-сервис (реализованный, например, с помощью модуля restify). Предположим, что один из обработчиков запросов бросил исключение RefferenceError из-за того, что программист сделал опечатку в имени переменной. Если немедленно не прекратить работу сервиса, может возникнуть ряд проблем, которые бывает сложно отследить:

  • Если какая-то сущность в результате опечатки оказалась равна null или undefined, то последующие запросы, обратившись к ней, так же, бросят исключения и не будут обработаны.
  • Если функция, которая бросила исключение, работала с базой данных, может произойти утечка соединия. Каждый раз, когда подобная ошибка будет повторяться, число соединений, используя которые сервис может работать с базой данных, будет уменьшаться.
  • Более сложная ситуация может произойти, если в качестве базы данных используется postgres, и соединение осталось незакрытым в ходе выполнения транзакции. В этом случае, «повисшая» транзакция не даст очищать старые версии записей, которые для неё видны. Транзакция может оставаться открытой неделями. Размер, который таблица занимает в памяти, будет расти без ограничений, что приведёт к тому, что обработка последующих запросов будет замедляться.4 Конечно, данный пример достаточно специфичен и касается лишь postgres, однако, он отлично иллюстрирует, что опасно продолжать работу программы, которая пребывает в нестабильном состоянии.
  • Соединение к удалённому сервису может остаться с незакрытой сессией, вследствие чего, следующий запрос может быть обработан от лица не того пользователя.
  • Может остаться незакрытым сокет. По умолчанию Node.js закроет неактивный сокет через две минуты, но это поведение может быть переопределено, и если ошибка будет повторяться, то в итоге число возможных сокетов будет исчерпано. Если вы оставите конфигурации по умолчанию, отследить и исправить проблему будет тяжело, так как ошибка о неактивном сокете возникает с задержкой в две минуты.
  • Может возникнуть утечка памяти, которая приведёт к её переполнению и выходу программы из строя. Или еще хуже — утечка может усложнить процесс сборки мусора, из-за чего начнет страдать производительность программы. Обнаружить причину проблемы в таком случае будет особенно затруднительно.

Учитывая вышеперечисленное, в таких ситуациях лучшим решением будет прервать работу программы. Вы можете перезапускать свою программу, после того как она была прервана — такой подход позволит автоматически восстанавливать стабильную работу вашего сервиса после возникающих ошибок.
Единственный, но существенный, недостаток этого подхода заключается в том, что будут отключены все пользователи работавшие с сервисом в момент перезапуска. Имейте ввиду следующее:

  • Сбои вызванные ошибкой программиста вводят приложение в нестабильное состояние. Нужно стремиться к тому, чтобы таких ошибок не возникало, их устранение имеет наивысший приоритет.
  • После перезапуска запросы могут как выполняться корректно, так и снова привести к ошибке. Может случиться так, что запросы обрабатываются некорректно, но отследить проблему сложно.
  • В хорошо спроектированной системе, независимо от того вызвана ли ошибка проблемой с интернет-соединением или ошибка произошла в Node.js, программа-клиент должна уметь обрабатывать ошибки сервера (переподключаться, выполнять повторные запросы).

Если перезапуск программы происходит очень часто, то следует отлаживать код и устранять ошибки. Лучшим способом для отладки будет сохранение и анализ снимка ядра. Данный подход работает как в GNU/Linux-системах, так и в illumos-системах, и позволяет просмотреть не только последовательность функций, которые привели к ошибке, но и переданные им аргументы, а так же состояние других объектов, видимых через замыкания.

3. Шаблоны написания функций

Во-первых стоит отметить, что очень важно подробно документировать свои функции. Необходимо описывать, что возвращает функция, какие аргументы принимает и какие ошибки могут возникнуть в процессе выполнения функции. Если не определить типы возможных ошибок и не сформулировать, что они означают, то вы не сможете правильно написать обработчик.

Throw, callback или EventEmitter?

Существует три основных способа вернуть ошибку из функции:

  1. throw возвращает ошибку синхронно. Это значит, что исключение возникнет в том же контексте, в котором функция была вызвана. Если используется try/catch, то исключение будет поймано. В противном случае — программа выйдет из строя (если, конечно, исключение не отловит домен или обработчик события 'uncaughtException' глобального объекта process, такой вариант будет рассмотрен далее).
  2. Вызов callback-функции с объектом ошибки в качестве первого аргумента является наиболее часто используемым способом вернуть ошибку из асинхронной функции. Общепринятым шаблоном вызова callback-функции является вызов вида callback(err, results), где только один из аргументов может принимать значения отличные от null.
  3. В более сложных случаях функция может генерировать событие 'error' объекта класса EventEmitter, тогда ошибка будет обработана, если зарегистрирован обработчик для события 'error'. Данный вариант используется если:
    • производится комплексная операция, которая возвращает несколько результатов или ошибок. Примером может быть извлечение записей из базы данных. Функция возвращает объект класса EventEmitter и вызывает событие 'row' — при извлечении каждой записи, "end" — когда все записи извлечены и 'error' — если возникает ошибка.
    • объект представляет собой сложный автомат, производящий множество асинхронных операций. В пример можно привести сокет, вызывающий события 'connect', 'end', 'timeout', 'drain' и 'close'. При возникновении ошибки, объект будет генерировать событие 'error'. Используя данный подход важно понимать, в каких ситуациях может возникать ошибка, могут ли при этом возникать и другие события и в каком порядке они возникают.

Использование callback-функций и генерирование событий относятся к асинхронным способам возврата ошибок. Если производится асинхронная операция, то реализуется один из этих способов, но никогда не используются сразу оба.

Итак, когда же использовать throw, а когда использовать callback-функции или события? Это зависит от двух факторов:

  • типа ошибки (ошибка программиста или программная ошибка),
  • типа функции в которой возникла ошибка (асинхронная или синхронная).

Программные ошибки характерны в большей мере для асинхронных функций. Асинхронные функции принимают в качестве аргумента callback-функцию, при возникновении ошибки она вызвается с объектом ошибки в качестве аргумента. Такой подход отлично себя зарекомендовал и широко применяется. В качестве примера можно ознакомиться с Node.js модулем fs. Событийный подход так же используется, но уже в более сложных случаях.

Программные ошибки в синхронных функциях могут возникать, как правило, если функция работает с данными, введёнными пользователем (например JSON.parse). В таких функциях при возникновении ошибки бросается исключение, реже – объект ошибки возвращается оператором return.

Если в функции хотя бы одна из возможных ошибок асинхронна, то все возможные ошибки должны возвращаться из функции используя асинхронный подход. Даже если ошибка возникла в том же контексте, в котором была вызвана функция, объект ошибки следует вернуть асинхронно.

Есть важное правило: для возврата ошибок в одной и той же функции может быть реализован либо синхронный, либо асинхронный подход, но никогда и тот и другой вместе. Тогда, чтобы принимать у функции ошибку, нужно будет использовать либо callback-функцию (или функцию-обработчик события 'error'), либо конструкцию try/catch, но никогда и то и другое. В документации к функции следует указывать, какой из способов к ней применим.

Проверка входных аргументов как правило позволяет, предупредить многие ошибки, которые совершают программисты. Часто случается, что при вызове асинхронной функции, ей забывают передать callback-функцию, в результате, чтобы понять где возникает ошибка, разработчику приходится, как минимум, просмотреть стек вызванных функций. Поэтому, если функция асинхронна, то в первую очередь, важно проверять передана ли callback-функция. Если не передана, то необходимо генерировать исключение. Кроме того, в начале функции следует проверять типы переданных ей аргументов, и так же генерировать исключение, если если они некорректны.

Напомним, что ошибки программиста не являются частью нормального процесса работы программы. Они не должны отлавливаться и обрабатываться. Поэтому данные рекомендации о немедленном бросании исключений при ошибках программиста не противоречат сформулированному выше правилу о том, что одна и та же функция не должна реализовывать как синхронный так и асинхронный подход для возврата ошибок.

Рассмотренные рекомендации представлены в таблице:

Пример функции Тип функции Ошибка Тип ошибки Как возвращать Как обрабатывать
fs.stat асинхронная файл не найден программная callback функция-обработчик
JSON.parse синхронная ошибка ввода программная throw try/catch
fs.stat асинхронная отсутствует обязательный аргумент ошибка программиста throw не обрабатывается
(прекращение работы)

В первой записи представлен наиболее часто встречаемый пример — асинхронная функция. Во второй строке – пример для синхронной функции, такой вариант встречается реже. В третей строке — ошибка программиста, желательно, чтобы подобные случаи имели место лишь в процессе разработки программы.

Ошибка ввода: ошибка программиста или программная ошибка?

Как различать ошибки программиста от программных ошибок? Вам решать, какие данные переданные функции являются корректными, а какие – нет. Если в функцию переданы аргументы не отвечающие поставленным вами требованиям, то это ошибка программиста. Если же аргументы корректны, но функция в данный момент не может с ними работать, то это программная ошибка.

Вам предстоит решать с какой строгостью производить проверку аргументов. Представим некую функцию connect, котороя принимает IP-адрес и callback-функцию в качестве аргументов. Предположим, что был произведён вызов этой функции с аргументом отличающимся по формату от IP-адреса, например: «bob». Рассмотрим что может произойти в таком случае:

  • Если вы строго производите проверку, соответствует ли формат введённой строки формату IPv4 адреса, то ваша функция бросит исключение на этапе проверки аргументов. Такой сценарий является наиболее приемлимым.
  • Если же вы проверяете лишь тип аргументов, то возникнет асинхронная ошибка о том, что невозможно подключиться к IP-адресу «bob».

Оба варианта удовлетворяют рассмотренным рекомендациям и вам решать насколько строго производить проверку. Функция Date.parse, например, принимает аргументы различных форматов, но на то есть причины. Всё же, для большинства функций рекомендуется строго проверять переданные аргументы. Чем более расплывчаты критерии проверки аргументов, тем более затруднительным становится процесс отладки кода. Как правило, чем строже проверка – тем лучше. И даже если в будущих версиях программы вы вдруг смягчите критерии проверки внутри какой-то функции, то вы не рискуете сломать ваш код.

Если переданное значение не удовлетворяет требованиям (например, undefined или строка имеет неверный формат), то функция должна сообщать о том, что переданное значение некорректно и прекращать работу программы. Прекращая работу программы, сообщив о некорректных аргументах, вы упрощаете процесс отладки кода себе и другим программистам.

Домены и process.on(‘uncaughtException’)

Программные ошибки всегда могут быть отловлены по определённому механизму: через try/catch, в callback-функции или обработчиком события 'error'. Домены и событие глобального объекта process 'uncaughtException' часто используются для перестраховки от непредвиденных ошибок, которые мог допустить программист. Учитывая рассмотренные выше положения, данный подход настоятельно не рекомендуется.

4. Правила написания функций

При написании функций придерживайтесь следующих правил:

  1. Пишите подробную документацию
    Это самое важное правило. Документация к функции должна содержать информацию:

    • о том с какими аргументами работает функция;
    • о том каких типов должны быть аргументы;
    • о любых дополнительных ограничениях, которые накладываются на вид аргументов (пример: IP-адрес должен иметь корректный формат).

    Если какое-то из установленных правил не выполняется, то функция должа немедленно бросать исключение.
    Так же следует документировать:

    • какие программные ошибки могут возникнуть в ходе выполнения функции (включая имена ошибок),
    • как обрабатывать возможные ошибки (отлавливать через try/catch или использовать асинхронные подходы),
    • описание результата выполнения функции.

  2. Используйте объекты класса Error (или подклассов) для всех ошибок.
    Все ваши ошибки должны быть объектами класса Error или классов, которые являются его наследниками. Используйте поля name и message, поле stack так же должно корректно работать.
  3. Расширяйте объект ошибки полями, которые описывают подробности ошибки.
    Если в функцию был передан некорректный аргумент, задайте в объекте ошибки поля propertyName и propertyValue. Для ошибок подключения к удалённому серверу расширяйте объект ошибки полем remoteIp, чтобы указать к какому адресу не удалось подключиться. При возникновении системной ошибки включайте в объект ошибки поле syscall, поясняющее, какой системный вызов не был обработан, так же включите поле errno , содержащее информацию о типе системной ошибки. В приложении к статье описаны рекомендуемые имена полей.

    Ошибка обязательно должна содержать корректные поля:

    • name: используется обработчиками для дифференциации ошибок по типу.
    • message: текст описывающий возникшую проблему. Текст должен быть коротким, но достаточно ёмким, что бы можно было понять суть проблемы.
    • stack: никогда не изменяйте объект стэка вызовов. V8 производит построение этого объекта только тогда, когда к нему производится обращение и процесс построения достаточно ресурсоёмкий, обращение к этому полю существенно снижает производительность программы.

    Ошибка должна содержать достаточно информации, чтобы обработчик мог на её основе сформировать своё сообщение об ошибке, не используя при этом поле message исходной ошибки. Возможно обработчику потребуется формировать ошибку из нескольких, чтобы выводить их в форме таблицы конечному пользователю.

  4. Если ошибка возвращается с низкого уровня вложенности функций, то следует оборачивать её.
    В начале статьи упоминалось, что возможна ситуация, когда приходится возвращать ошибку из функции, в которой она возникла через callback-функцию на уровень выше, а затем еще выше, до тех пор пока она не достигнет логического уровня приложения, который ответственен за обработку данного типа ошибок. В таких случаях рекомендуется производить обёртку ошибки по мере её «всплытия». Обёрткой функции называется расширение исходного объекта ошибки информацией о логическом уровне через который она была передана. Модуль verror позволяет реализовать такой механизм.
    Рассмотрим некую функцию fetchConfig, извлекающую настройки из удалённой базы данных. Вызов fetchConfig выполняется при старте работы сервиса. Алгоритм работы функции описан ниже.

        1. Извлечение настроек
          1.1 Соединение с базой данных
            1.1.1 Получение адреса через систему DNS
            1.1.2 Создание TCP соединения с сервером базы данных
            1.1.3 Аутентификация на сервере базы данных
          1.2. Выполнение запроса к базе данных
          1.3. Обработка результата запроса
          1.4. Настройка сервиса
        2. Запуск работы сервиса
        

    Предположим, что в пункте 1.1.2 возникла ошибка. Если передавать ошибку в контекст из которого была вызвана функция fetchConfig не оборачивая её, то сообщение об ошибке будет иметь вид:

    myserver: Error: connect ECONNREFUSED
    

    Пользы от такого сообщения мало.
    Далее представлено сообщение о той же ошибке, но с применением обёртки:

    myserver: failed to start up: failed to load configuration: failed to connect to
    database server: failed to connect to 127.0.0.1 port 1234: connect ECONNREFUSED
    

    Если не выполнять обёртку на некоторых уровнях, то можно получить более лаконичное сообщение:

    myserver: failed to load configuration: connection refused from database at
    127.0.0.1 port 1234.
    

    Однако, как правило, избыток информации — лучше чем дефицит.

    Есть несколько нюансов о которых нужно знать, если вы решили оборачивать свои ошибки:

    • Старайтесь не изменять поля начального обьекта ошибки, обработчику может потребоваться информация об исходной ошибке.
    • Поле name ошибки при обёртке можно изменять, чтобы оно больше соответствовало контексту. Однако, нет необходимости это делать, если у объекта ошибки есть иные поля, по которым обработчик может распознать её тип.
    • Поле message при обёртке тоже может быть изменено, но не следует при этом менять message исходного объекта. Не производите никаких действий с полем stack, как уже упоминалось выше, V8 формирует объект stack, только при обращении к нему и это достаточно ресурсоёмкий процесс, который может привести к существенному снижению производительности вашей программы.

    В Joyent мы используем модуль verror для обёртки ошибок, так как он имеет минималистичный синтаксис. На момент написания статьи в модуле не реализованы некоторые из рассмотренных рекомендаций, однако он будет дорабатываться.

5. Пример

Рассмотрим в качестве примера функцию, которая создаёт TCP соединение по указанному IPv4 адресу.

/*
 * Функция создаёт TCP соединение по указанному IPv4 адресу.  Аргументы:
 *
 *    ip4addr        строка адреса формата IPv4;
 *
 *    tcpPort        натуральное число, TCP порт;
 *
 *    timeout        натуральное число, время в миллисекундах, в течение которого
 *                   необходимо ждать ответа от удалённого сервера;
 *
 *    callback       функция вызываемая после завершения операции,
 *                   если операция завершилась успешно, происходит 
 *                   вызов вида callback(null, socket), где socket это
 *                   объект класса net.Socket, если возникла ошибка,
 *                   выполняется вызов вида callback(err).
 *
 * В функции могут возникнуть ошибки следующих типов:
 *
 *    SystemError    Для "connection refused", "host unreachable" и других
 *                   ошибок, возвращаемых системным вызовом connect(2). Для
 *                   данного типа ошибок поле errno объекта err будет содержать
 *                   соответствующее ошибке символьное представление.
 *
 *    TimeoutError   Данный тип ошибок возникает при истечении 
 *                   времени ожидания timeout.
 *
 * Все возвращаемые объекты ошибок имеют поля "remoteIp" и "remotePort".
 * После возникновении ошибки, сокеты, которые были открыты функцией, будут закрыты.
 */
function connect(ip4addr, tcpPort, timeout, callback)
{
  assert.equal(typeof (ip4addr), 'string',
      "аргумент 'ip4addr' должен быть строкового типа");
  assert.ok(net.isIPv4(ip4addr),
      "аргумент 'ip4addr' должен содержать IPv4 адрес");
  assert.equal(typeof (tcpPort), 'number',
      "аргумент 'tcpPort' должен быть числового типа");
  assert.ok(!isNaN(tcpPort) && tcpPort > 0 && tcpPort < 65536,
      "аргумент 'tcpPort' должен быть натуральным числом в диапазоне от 1 до 65535");
  assert.equal(typeof (timeout), 'number',
      "аргумент 'timeout' должен быть числового типа");
  assert.ok(!isNaN(timeout) && timeout > 0,
      "аргумент 'timeout' должен быть натуральным числом");
  assert.equal(typeof (callback), 'function');

  /* код функции */
}

Этот пример достаточно примитивен, но он иллюстрирует многие из рассмотренных рекомендаций:

  • Аргументы, их типы, и предъявляемые к ним требования подробно документированы.
  • Функция проверяет переданные ей аргументы и бросает исключение, если аргументы не удовлетворяют критериям.
  • Документированы типы возможных ошибок, а так же поля, которые они содержат.
  • Указан способ, которым функция возвращает ошибки.
  • Возвращаемые ошибки имеют поля «remoteIp» и «remotePort», что позволит обработчику на основе этой информации формировать сообщение ошибки.
  • Документировано состояние соединений после возникновения ошибки: «после возникновении ошибки, сокеты которые были открыты функцией, будут закрыты».

Может показаться, что в представленном примере проделано много лишней работы, однако, десять минут потраченные на опиание документации могут сэкономить несколько часов вам или другим разработчикам.

6. Резюме

  • Различайте ошибки программиста и программные ошибки.
  • Програмные ошибки могут и должны обрабатываться, тогда как ошибки программиста не могут быть корректно обработаны. Не следует продолжать работу программы в случае возникновения ошибок программиста, так как дальнейшее поведение программы непредсказуемо.
  • Для возврата ошибок в функции может быть реализован синхронный подход (например, throw) или асинхронный подход (callback-функция или событие), но нельзя реализовывать оба подхода в одной функции. Тогда, при использовании функции, чтобы обрабатывать возникающие в ней ошибки, нужно будет применять либо callback-функции, либо конструкцию try/catch, но никогда и то и другое.
  • При написании функций подробно документируйте аргументы, их типы, предъявляемые к ним требования, а так же типы возможных ошибок и то, как функция возвращает ошибки (синхронно, используя throw, или асинхронно, используя callback-функцию или событийный подход).
  • Возвращаемая ошибка должна быть объектом класса Error или класса-наследника. Расширяйте объект ошибки новыми полями, чтобы включить в объект необходимую информацию об ошибке. По возможности используйте общепринятые имена полей, представленные в приложении.

7. Приложение: общепринятые имена полей ошибок

Настоятельно рекомендуется для расширения объектов ошибок использовать приведённые в таблице имена полей. Представленные имена используются в стандартных модулях Node.js, следует пользоваться ими в обработчиках ошибок, а так же при формировании сообщений об ошибках.

Имя поля объекта ошибки Значение поля
localHostname локальное DNS-имя (например, то, по которому принимаются соединения)
localIp локальный IP-адрес (например, тот, по которому принимаются соединения)
localPort локальный TCP порт (например, тот, по которому принимаются соединения)
remoteHostname DNS-имя удалённого сервера (например, сервера, с которым устанавливается соединение)
remoteIp IP-адрес удалённого сервера (например, сервера, с которым устанавливается соединение)
remotePort порт удалённого сервера (например, сервера, с которым устанавливается соединение)
path путь к файлу, директории иди сокет межпроцессного взаимодействия (IPC-сокет) (например, путь к файлу, который необходимо считать)
srcpath путь используемый в качестве источника (например, для копирования фала)
dstpath путь назначения (например, для копирования фала)
hostname DNS имя (например, то, которое используется для попытки получить IP-адрес)
ip IP-адрес (например, тот, для которого производится попытка получить DNS-имя)
propertyName имя свойста объекта или имя аргумента (например, в ошибке, возникшей при проверке аргументов переданных в функцию)
propertyValue значение поля объекта (например, в ошибке, возникшей при проверке аргументов переданных в функцию)
syscall имя невыполненного системного вызова
errno символьное представление errno (например, "ENOENT")


1 Начинающие разработчики часто допускают подобную ошибку. В данном примере try/catch и вызов функции бросающей исключение выполнятся в разных контекстах из-за асинхронности функции doSomeAsyncOperation, поэтому исключение не будет поймано.
2 В JavaScript throw может работать со значениями и других типов, но рекомендуется использовать именно объекты класса Error. Если в ThrowStatement использовать другие значения, то будет невозможно получить стэк вызовов, который привел к ошибке, что усложнит отладку кода.
3 Данные понятия возникли задолго до появления Node.js. В Java аналогом можно считать проверяемые и непроверяемые исключения. В C для работы с ошибками программиста предусмотрены утверждения.
4 Приведённый пример может показаться слишком предметным, это потому, что он не вымышлен, мы действительно сталкивались с этой проблемой, это было неприятно.

Don’t you hate it when you see an uncaughtException error pop up and crash your Node.js app?

Yeah… I feel you. Can anything be worse? Oh yeah, sorry, unhandledRejection I didn’t see you there. What a nightmare you are. 😬

I maintain all Node.js open-source repos at Sematext. A few of them can help you out with error handling, but more about that further down.

Here at Sematext, we take error handling seriously! I want to share a bit of that today.

I want to guide you through what I’ve learned so far about error handling in Node.js while working on open-source projects. Hopefully, it’ll help you improve your code, make it more robust, and ultimately help you step up your bug hunting, and help improve your general developer experience.

I don’t want you to have to stay up late and burn the midnight oil troubleshooting bugs. Ah! About that, here’s an epic song I really like!

What Is Error Handling in Node.js

I’ve heard a ton of my fellow developers say error handling in Node.js is way too hard. Well, I can’t lie. It’s not easy. But, I have to be fair and say it’s not that hard either once you set up centralized error handling.

What is an error anyhow? It’s a way to see bugs in your code. Following this logic, error handling is a way to find these bugs and solve them as quickly as humanly possible.

From this explanation, it’s obvious the hard part is setting up a good base for your error handling. It’s all about keeping you sane at the end of the day. Handling errors properly means developing a robust codebase and reducing development time by finding bugs and errors easily.

Why Do You Need Error Handling

Why? For your own sanity. You want to make bug fixing less painful. It helps you write cleaner code. It centralizes all errors and lets you enable alerting and notifications so you know when and how your code breaks.

Types of Errors: Operational vs. Programmer Errors

Would you believe me when I said not all errors are caused by humans? Don’t get me wrong, most still are, but not all of them! Errors can be Operational and Programmer errors.

Operational Errors

Operational errors represent runtime problems. These errors are expected in the Node.js runtime and should be dealt with in a proper way. This does not mean the application itself has bugs. It means they need to be handled properly. Here’s a list of common operational errors:

  • failed to connect to server
  • failed to resolve hostname
  • invalid user input
  • request timeout
  • server returned a 500 response
  • socket hang-up
  • system is out of memory

Programmer Errors

Programmer errors are what we call bugs. They represent issues in the code itself. Here’s a common one for Node.js, when you try reading a property of an undefined object. It’s a classic case of programmer error. Here are a few more:

  • called an asynchronous function without a callback
  • did not resolve a promise
  • did not catch a rejected promise
  • passed a string where an object was expected
  • passed an object where a string was expected
  • passed incorrect parameters in a function

Now you understand what types of errors you’ll be facing, and how they are different. Operational errors are part of the runtime and application while programmer errors are bugs you introduce in your codebase.

Now you’re thinking, why do we divide them into two categories? It’s simple really.

Do you want to restart your app if there’s a user not found error? Absolutely not. Other users are still enjoying your app. This is an example of an operational error.

What about failing to catch a rejected promise? Does it make sense to keep the app running even when a bug threatens your app? No! Restart it.

What Is an Error Object?

The error object is a built-in object in the Node.js runtime. It gives you a set of info about an error when it happens. The Node.js docs have a more in-depth explanation.

A basic error looks like this:

const error = new Error("An error message")
console.log(error.stack)

It has an error.stack field that gives you a stack trace showing where the error came from. It also lists all functions that were called before the error occurred. The error.stack field is optimal to use while debugging as it prints the error.message as well.

How Do You Handle Errors in Node.js: Best Practices You Should Follow

From my experience, there are a few best practices that will make it easier to handle errors in Node.js.

You can handle errors in callbacks. There are some serious drawbacks to using callbacks because it creates a nested “callback hell”. It’s notoriously hard to debug and fix errors if you need to look for them in nested functions.

A better way is to use async/await and try-catch statements, or .catch() errors in promises.

Let me show you what I mean.

1. Use Custom Errors to Handle Operational Errors

With the async/await pattern you can write code that looks synchronous, but actually is asynchronous.

const anAsyncTask = async () => {
 try {
 const user = await getUser()
 const cart = await getCart(user)

 return cart
 } catch (error) {
 console.error(error)
 } finally {
 await cleanUp()
 }
}

This pattern will clean up your code and avoid the dreaded callback hell.

You can use the built-in Error object in Node.js as I mentioned above, as it gives you detailed info about stack traces.

However, I also want to show you how to create custom Error objects with more meaningful properties like HTTP status codes and more detailed descriptions.

Here’s a file called baseError.js where you set the base for every custom error you’ll use.

// baseError.js

class BaseError extends Error {
 constructor (name, statusCode, isOperational, description) {
 super(description)

 Object.setPrototypeOf(this, new.target.prototype)
 this.name = name
 this.statusCode = statusCode
 this.isOperational = isOperational
 Error.captureStackTrace(this)
 }
}

module.exports = BaseError

Also create an httpStatusCodes.js file to keep a map of all status codes you want to handle.

// httpStatusCodes.js

const httpStatusCodes = {
 OK: 200,
 BAD_REQUEST: 400,
 NOT_FOUND: 404,
 INTERNAL_SERVER: 500
}

module.exports = httpStatusCodes

Then, you can create an api404Error.js file, and extend the BaseError with a custom error for handling 404s.

// api404Error.js

const httpStatusCodes = require('./httpStatusCodes')
const BaseError = require('./baseError')

class Api404Error extends BaseError {
 constructor (
 name,
 statusCode = httpStatusCodes.NOT_FOUND,
 description = 'Not found.',
 isOperational = true
 ) {
 super(name, statusCode, isOperational, description)
 }
}

module.exports = Api404Error

How do you use it? Throw it in your code when you want to handle 404 errors.

const Api404Error = require('./api404Error')

...
const user = await User.getUserById(req.params.id)
if (user === null) {
 throw new Api404Error(`User with id: ${req.params.id} not found.`)
}
...

You can duplicate this code for any custom error, 500, 400, and any other you want to handle.

2. Use a Middleware

Once you have a set of custom errors, you can configure centralized error handling. You want to have a middleware that catches all errors. There you can decide what to do with them and where to send them if they need to notify you via an alert notification.

In your API routes you’ll end up using the next() function to forward errors to the error handler middleware.

Let me show you.

...
app.post('/user', async (req, res, next) => {
 try {
const newUser = User.create(req.body)
 } catch (error) {
 next(error)
 }
})
...

The next() function is a special function in Express.js middlewares that sends values down the middleware chain. At the bottom of your routes files you should have a .use() method that uses the error handler middleware function.

const { logError, returnError } = require('./errorHandler')

app.use(logError)
app.use(returnError)

The error handler middleware should have a few key parts. You should check if the error is operational, and decide which errors to send as alert notifications so you can debug them in more detail. Here’s what I suggest you add to your error handler.

function logError (err) {
 console.error(err)
}

function logErrorMiddleware (err, req, res, next) {
 logError(err)
 next(err)
}

function returnError (err, req, res, next) {
 res.status(err.statusCode || 500).send(err.message)
}

function isOperationalError(error) {
 if (error instanceof BaseError) {
 return error.isOperational
 }
 return false
}

module.exports = {
 logError,
 logErrorMiddleware,
 returnError,
 isOperationalError
}

3. Restart Your App Gracefully to Handle Programmer Errors

Everything I’ve explained so far has been related to operational errors. I’ve shown how to gracefully handle expected errors and how to send them down the middleware chain to a custom error handling middleware.

Let’s jump into programmer errors now. These errors can often cause issues in your apps like memory leaks and high CPU usage. The best thing to do is to crash the app and restart it gracefully by using the Node.js cluster mode or a tool like PM2. I wrote another article where I describe in detail how to detect Node.js memory leaks using various solutions.

4. Catch All Uncaught Exceptions

When unexpected errors like these happen, you want to handle it immediately by sending a notification and restarting the app to avoid unexpected behavior.

const { logError, isOperationalError } = require('./errorHandler')

...
process.on('uncaughtException', error => {
 logError(error)

 if (!isOperationalError(error)) {
 process.exit(1)
 }
})
...

5. Catch All Unhandled Promise Rejections

Promise rejections in Node.js only cause warnings. You want them to throw errors, so you can handle them properly.

It’s good practice to use fallback and subscribe to:

process.on('unhandledRejection', callback)

This lets you throw an error properly.

Here’s what the error handling flow should look like.

...
const user = User.getUserById(req.params.id)
 .then(user => user)
 // missing a .catch() block
...

// if the Promise is rejected this will catch it
process.on('unhandledRejection', error => {
 throw error
})

process.on('uncaughtException', error => {
 logError(error)

 if (!isOperationalError(error)) {
 process.exit(1)
 }
})

6. Use a Centralized Location for Logs and Error Alerting

I recently wrote a detailed tutorial about Node.js logging best practices you should check out.

The gist of it is to use structured logging to print errors in a formatted way and send them for safekeeping to a central location, like Sematext Logs, our log management tool.

It’ll help with your sanity and persist the logs over time, so you can go back and troubleshoot issues whenever things break.

To do this, you should use loggers like winston and morgan. Additionally, you can add winston-logsene to send the logs to Sematext right away.

First, create a setup for winston and winston-logsene. Create a loggers directory and a logger.js file. Paste this into the file.

// logger.js

const winston = require('winston')
const Logsene = require('winston-logsene')

const options = {
 console: {
 level: 'debug',
 handleExceptions: true,
 json: false,
 colorize: true
 },
 logsene: {
 token: process.env.LOGS_TOKEN,
 level: 'debug',
 type: 'app_logs',
 url: 'https://logsene-receiver.sematext.com/_bulk'
 }
}

const logger = winston.createLogger({
 levels: winston.config.npm.levels,
 transports: [
 new winston.transports.Console(options.console),
 new Logsene(options.logsene)
 ],
 exitOnError: false
})

module.exports = logger

The good thing with this is that you get JSON formatted logs you can analyze to get more useful information about your app. You’ll also get all logs forwarded to Sematext. This will alert you whenever errors occur. That’s pretty awesome!

Furthermore, you should add an httpLogger.js file in the loggers directory and add morgan and morgan-json to print out access logs. Paste this into the httpLogger.js:

const morgan = require('morgan')
const json = require('morgan-json')
const format = json({
 method: ':method',
 url: ':url',
 status: ':status',
 contentLength: ':res[content-length]',
 responseTime: ':response-time'
})

const logger = require('./logger')
const httpLogger = morgan(format, {
 stream: {
 write: (message) => {
 const {
 method,
 url,
 status,
 contentLength,
 responseTime
 } = JSON.parse(message)

 logger.info('HTTP Access Log', {
 timestamp: new Date().toString(),
 method,
 url,
 status: Number(status),
 contentLength,
 responseTime: Number(responseTime)
 })
 }
 }
})

module.exports = httpLogger

In your app.js file you can now require both the logger.js and httpLogger.js, and use the logger instead of console.log().

// app.js

const logger = require('./loggers/logger')
const httpLogger = require('./loggers/httpLogger')
...

app.use(httpLogger)
...

In your errorHandler.js you can now replace all console.error() statements with logger.error() to persist the logs in Sematext.

// errorHandler.js

const logger = require('../loggers/logger')
const BaseError = require('./baseError')

function logError (err) {
 logger.error(err)
}

function logErrorMiddleware (err, req, res, next) {
 logError(err)
 next(err)
}

function returnError (err, req, res, next) {
 res.status(err.statusCode || 500).send(err.message)
}

function isOperationalError(error) {
 if (error instanceof BaseError) {
 return error.isOperational
 }
 return false
}

module.exports = {
 logError,
 logErrorMiddleware,
 returnError,
 isOperationalError
}

That’s it. You now know how to properly handle errors!

However, I do want to cover how to deliver errors. Should you be throwing them, passing errors in callback functions or promise rejections, or emit an “error” event via an EventEmitter.

How to Deliver Errors: Function Patterns

Let’s go over the four main ways to deliver an error in Node.js:

  • throw the error (making it an exception).
  • pass the error to a callback, a function provided specifically for handling errors and the results of asynchronous operations
  • pass the error to a reject Promise function
  • emit an “error” event on an EventEmitter

We’ve talked about how to handle errors, but when you’re writing a new function, how do you deliver errors to the code that called your function?

Throwing Errors

When you throw an error it unwinds the entire function call stack ignoring any functions you have on the stack. It gets delivered synchronously, in the same context where the function was called.

If you use a try-catch block you can handle the error gracefully. Otherwise, the app usually crashes, unless you have a fallback for catching Uncaught Exceptions as I explained above.

Here’s an example of throwing an error and handling it in a try-catch block:

const getUserWithAsyncAwait = async (id) => {
 try {
 const user = await getUser(id)
 if (!user) {
 throw new 404ApiError('No user found.')
 }

 return user
 } catch (error) {
 // handle the error
 logError(error)
 }
}

const user = await getUserWithAsyncAwait(1)

...

Using Callback

Callbacks are the most basic way of delivering an error asynchronously. The user passes you a function – the callback, and you invoke it sometime later when the asynchronous operation completes. The usual pattern is that the callback is invoked as callback(err, result), where only one of err and result is non-null, depending on whether the operation succeeded or failed.

Callbacks have been around for ages. It’s the oldest way of writing asynchronous JavaScript code. It’s also the oldest way of delivering errors asynchronously.

You pass a callback function as a parameter to the calling function, which you later invoke when the asynchronous function completes executing.

The usual pattern looks like this:

callback(err, result)

The first parameter in the callback is always the error.

Inside the callback function, you’ll then first check if the error exists and only if it’s a non-null value you continue executing the callback function.

function getUserWithCallback(id, callback) {
 getUser(id, function(user) {
 if (!user) {
 return callback(new 404ApiError('No user found.'))
 }

 callback(null, user)
 })
}

getUserWithCallback(1, function(err, user) {
 if (err) {
 // handle the error
 logError(error)
 }

 const user = user
 ...
})

Using Promises

Promises have replaced callbacks as the new and improved way of writing asynchronous code.

This pattern has become the new norm since Node.js version 8 that included async/await out of the box. Asynchronous code can be written to look like synchronous code. Catch errors can be done by using try-catch.

function getUserWithPromise(id) {
 return new Promise((resolve, reject) => {
 getUser(id, function(user) {
 if (!user) {
 return reject(new 404ApiError('No user found.'))
 }

 resolve(user)
 })
 })
}

getUserWithPromise(1)
 .then(user => {
 const user = user
 ...
 })
 .catch(err => {
 logError(error)
 })

Using EventEmitter

Ready for some more complicated use cases?

In some cases, you can’t rely on promise rejection or callbacks. What if you’re reading files from a stream. Or, fetching rows from a database and reading them as they arrive. A use case I see on a daily basis is streaming log lines and handling them as they’re coming in.

You can’t rely on one error because you need to listen for error events on the EventEmitter object.

In this case, instead of returning a Promise, your function would return an EventEmitter and emit row events for each result, an end event when all results have been reported, and an error event if any error is encountered.

Here’s a code sample from Logagent, an open-source log shipper I maintain. The socket value is an EventEmitter object.

net.createServer(socket => {
...

 socket
 .on('data', data => {
 ...

 })
 .on('end', result => {
 …

 })
 .on('error', console.error) // handle multiple errors
}

Throw, Callback, Promises, or EventEmitter: Which Pattern Is the Best?

Now, we’ve finally come to the verdict, when should you throw errors, and when do you use promise rejections or EventEmitters?

For operational errors, you should use Promise rejections or a try-catch block with async/await. You want to handle these errors asynchronously. It works well and is widely used.

If you have a more complicated case like I explained above, you should use an event emitter instead.

You want to explicitly throw errors if unwinding the whole call stack is needed. This can mean when handling programmer errors and you want the app to restart.

How to Write Functions for Efficient Error Handling

Whatever you do, choose one way to deliver operational errors. You can throw errors and deliver them synchronously, or asynchronously by using Promise rejections, passing them in callbacks, or emitting errors on an EventEmitter.

After setting up centralized error handling, the next logical step is to use a central location for your logs that also gives you error alerting.

Sematext Logs provides log management and error alerting to help analyze logs and debug and fix errors and exceptions. Definitely check it out and try it yourself. But you can also take a look at the lists where we compare the best log management tools, log analysis software, and cloud logging services available today.

noed.js error exception handling

Closing Thoughts

In this tutorial, I wanted to give you a way to handle the dreaded unhandledException and unhandledRejection errors in Node.js apps. I hope it was useful to you and that you’ll use what you learned today in your own apps.

Alongside this, I also explained a few best practices about error handling, like how to set up centralized error handling with middlewares and use a central location for your error logs. Don’t forget that logging frameworks like winston and morgan are crucial for this to work.

Lastly, I explained a few different ways of delivering errors, either with throw or with Promise .reject(), callback functions or the .on(‘error’) event on an EventEmitter.

I’ve tried to share all the knowledge I’ve gained over the last few years while maintaining open-source repos to keep you from making the same mistakes I’ve made in the past. Best of luck!

If you ever need alerting, error handling, and log management for your production apps, check us out.

Errors are part of a developer’s life. We can neither run nor hide from them. While building production-ready software, we need to manage errors effectively to:

  1. improve the end-user experience; i.e., providing correct information and not the generic message “Unable to fulfill the request”
  2. develop a robust codebase
  3. recede development time by finding bugs efficiently
  4. avoid abruptly stopping a program

Because you are here, I assume you are probably a web developer with a JavaScript background. Let’s take the typical use case of reading a file in Node.js without handling an error:

var fs = require('fs')

# read a file
const data = fs.readFileSync('/Users/Kedar/node.txt')

console.log("an important piece of code that should be run at the end")

Note that Node.js should execute some critical piece of code after the file-reading task. When we run it, we receive the output as shown below:

Output:

$node main.js
fs.js:641
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^

Error: ENOENT: no such file or directory, open '/Users/Kedar/node.txt'
    at Error (native)
    at Object.fs.openSync (fs.js:641:18)
    at Object.fs.readFileSync (fs.js:509:33)
    at Object.<anonymous> (/home/cg/root/7717036/main.js:3:17)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)

Here, the program ends abruptly without executing the necessary code. We will discuss the revised code with error handling later in the try...catch blocks section. This example demonstrates only one of many issues faced without error handling. Let’s take a look at what we’ll cover to better understand how we can handle errors:

  • Error
    • Programmer errors
    • Operational errors
  • Error handling techniques
    • try…catch blocks
    • The callback function
    • Promises
    • Async/await
    • Event emitters
  • Handling errors
    • Retry the operation
    • Report the failure to the client
    • Report failures directly top of stack
    • Crash immediately

Before we learn about error handling, let’s understand an Error in Node.js.

Error

Error is an extension of the Error object in Javascript. The error can be constructed and thrown or passed to some function. Let’s check out some examples:

throw new Error('bad request'); // throwing new error
callback_function(new Error('connectivity issue')); // passing error as an argument

While creating an error, we need to pass a human-readable string as an argument to understand what went wrong when our program is working incorrectly. In other words, we are creating an object by passing the string to the Error constructor.

You also need to know that errors and exceptions are different in JavaScript, particularly Node.js. The errors are the instances of an Error class, and when you throw an error, it becomes an exception.

Humans do not cause all errors. There are two types of errors, programmer and operational. We use the phrase “error” to describe both, but they are quite different in reality because of their root causes. Let’s have a look at each one.

Programmer errors

Programmer errors depict issues with the program written — bugs. In other words, these are the errors caused by the programmer’s mistakes while writing a program. We cannot handle these errors properly, and the only way to correct them is to fix the codebase. Here are some of the common programmer errors:

  • Array index out of bounds — trying to access the seventh element of the array when only six are available
  • Syntax errors — failing to close the curly braces while defining a JavaScript function
  • Reference errors — accessing a function or variables that are not defined
  • Deprecation errors and warnings — calling an asynchronous function without a callback
  • Type error — x object is not iterable
  • Failing to handle operational errors

Operational errors

Every program faces operational errors (even if the program is correct). These are issues during runtime due to external factors that can interrupt the program’s normal flow. Unlike programmer errors, we can understand and handle them. These are some examples:

  • Unable to connect server/database
  • Request timeout
  • Invalid input from the user
  • Socket hang-up
  • 500 response from a server
  • File not found

You might wonder why this segregation is necessary because both cause the same effect, interrupting the program? Well, you might have to act based on the type of error. For example, restarting the app may not be a suitable action for file not found error (operational error) but restarting might be helpful when your program is failing to catch the rejected promise (programmer error).

Now that you know about the errors, let’s handle them. We can avoid the abrupt termination of our program by managing these errors, which is an essential part of production-ready code.

Error handling techniques

To handle the errors effectively, we need to understand the error delivery techniques.

There are four fundamental strategies to report errors in Node.js:

  1. try…catch blocks
  2. Callbacks
  3. Promises
  4. Event emitters

Let’s understand using them one by one.

try…catch blocks

In the try…catch method, the try block surrounds the code where the error can occur. In other words, we wrap the code for which we want to check errors; the catch block handles exceptions in this block.

Here’s the try…catch code to handle errors:

var fs = require('fs')

try {
const data = fs.readFileSync('/Users/Kedar/node.txt')
} catch (err) {
  console.log(err)
}

console.log("an important piece of code that should be run at the end")

We receive the output as shown below:

$node main.js
{ Error: ENOENT: no such file or directory, open '/Users/Kedar/node.txt'
    at Error (native)
    at Object.fs.openSync (fs.js:641:18)
    at Object.fs.readFileSync (fs.js:509:33)
    at Object.<anonymous> (/home/cg/root/7717036/main.js:4:17)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/Users/Kedar/node.txt' }
an important piece of code that should be run at the end

The error is processed and displayed. In the end, the rest of the code executes as planned.

Callbacks

A callback function (commonly used for asynchronous code) is an argument to the function in which we implement error handling.

The purpose of a callback function is to check the errors before the result of the primary function is used. The callback is usually the final argument to the primary function, and it executes when an error or outcome of the operation emerges.

Here’s the syntax for a callback function:

function (err, result) {}

The first argument is for an error, and the second is for the result. In case of an error, the first attribute will contain the error, and the second attribute will be undefined and vice versa. Let’s check out an example where we try to read a file by applying this technique:

const fs = require('fs');

fs.readFile('/home/Kedar/node.txt', (err, result) => {
  if (err) {
    console.error(err);
    return;
  }

  console.log(result);
});

The result looks like this:

$node main.js
{ Error: ENOENT: no such file or directory, open '/home/Kedar/node.txt'
    at Error (native)
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/home/Kedar/node.txt' }

We received the error because the file is not available. We can also implement the callbacks with the user-defined functions. The example below illustrates a user-defined function to double the given number using the callbacks:

const udf_double = (num, callback) => {
  if (typeof callback !== 'function') {
    throw new TypeError(`Expected the function. Got: ${typeof callback}`);
  }

  // simulate the async operation
  setTimeout(() => {
    if (typeof num !== 'number') {
      callback(new TypeError(`Expected number, got: ${typeof num}`));
      return;
    }

    const result = num * 2;
    // callback invoked after the operation completes.
    callback(null, result);
  }, 100);
}

// function call
udf_double('2', (err, result) => {
  if (err) {
    console.error(err)
    return
  }
  console.log(result);
});

The program above will throw an error since we pass the string instead of an integer. The result is as follows:

$node main.js
TypeError: Expected number, got: string
    at Timeout.setTimeout (/home/cg/root/7717036/main.js:9:16)
    at ontimeout (timers.js:386:14)
    at tryOnTimeout (timers.js:250:5)
    at Timer.listOnTimeout (timers.js:214:5)

Promises

Promise in Node.js is a contemporary way to handle errors, and it is usually preferred compared to callbacks. Since promises are alternatives to callbacks, let’s convert the example discussed above (udf_double) to utilize promises:

const udf_double = num => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (typeof num !== 'number') {
        reject(new TypeError(`Expected number, got: ${typeof num}`));
      }

      const result = num * 2;
      resolve(result);
    }, 100);
  });
}

In the function, we will return a promise, which is a wrapper to our primary logic. We pass two arguments while defining the Promise object:

  1. resolve — used to resolve promises and provide results
  2. reject — used to report/throw errors

Now, let’s execute the function by passing the input:

udf_double('8')
  .then((result) => console.log(result))
  .catch((err) => console.error(err));

We get an error, as shown below:

$node main.js
TypeError: Expected number, got: string
    at Timeout.setTimeout (/home/cg/root/7717036/main.js:5:16)
    at ontimeout (timers.js:386:14)
    at tryOnTimeout (timers.js:250:5)
    at Timer.listOnTimeout (timers.js:214:5)

Well, this looks much simpler than callbacks. We can also use a utility such as util.promisify() to convert callback-based code into a Promise. Let’s transform the fs.readFile example from the callback section to use promisify:

const fs = require('fs');
const util = require('util');

const readFile = util.promisify(fs.readFile);

readFile('/home/Kedar/node.txt')
  .then((result) => console.log(result))
  .catch((err) => console.error(err));

Here we are promisifying the readFile function. We get the result as below:

[Error: ENOENT: no such file or directory, open '/home/Kedar/node.txt'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/home/Kedar/node.txt'
}

Async/await

Async/await is just syntactic sugar that is meant to augment promises. It provides a synchronous structure to asynchronous code. For simple queries, Promises can be easy to use.

Still, if you run into scenarios with complex queries, it’s easier to understand the code that looks as though it’s synchronous.

Note that the return value of an async function is a Promise. The await waits for the promise to be resolved or rejected. Let’s implement the readFile example using async/await:

const fs = require('fs');
const util = require('util');

const readFile = util.promisify(fs.readFile);

const read = async () => {
  try {
    const result = await readFile('/home/Kedar/node.txt');
    console.log(result);
  } catch (err) {
    console.error(err);
  }
};

read()

We are creating the async read function in which we are reading the file using await. The output is as below:

[Error: ENOENT: no such file or directory, open '/home/Kedar/node.txt'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/home/Kedar/node.txt'
}

Event emitters

We can use the EventEmitter class from the events module to report errors in complex scenarios — lengthy async operations that can produce numerous failures. We can continuously emit the errors caused and listen to them using an emitter.

Let’s check out the example where we try to mimic a receiving data scenario and check if it is correct. We need to check if the first six indexes are integers, excluding the zeroth index. If any index among the six is not an integer, we emit an error, making further decisions based on this error:

const { EventEmitter } = require('events'); //importing module

const getLetter = (index) =>{
    let cypher = "*12345K%^*^&*" //will be a fetch function in a real scenario which will fetch a new cypher every time
    let cipher_split = cipher.split('')
    return cipher_split[index]
}

const emitterFn = () => {
  const emitter = new EventEmitter(); //initializing new emitter
  let counter = 0;
  const interval = setInterval(() => {
    counter++;
    
    if (counter === 7) {
      clearInterval(interval);
      emitter.emit('end');
    }
    
    let letter = getLetter(counter)
    
    if (isNaN(letter)) { //Check if the received value is a number
      (counter<7) && emitter.emit(
        'error',
        new Error(`The index ${counter} needs to be a digit`)
      );
      return;
    }
    (counter<7) && emitter.emit('success', counter);

  }, 1000);

  return emitter;
}

const listner = emitterFn();

listner.on('end', () => {
  console.info('All six indexes have been checked');
});

listner.on('success', (counter) => {
  console.log(`${counter} index is an integer`);
});

listner.on('error', (err) => {
  console.error(err.message);
});

Firstly, we import the events module to use EventEmitter. Then we define the getLetter() function to fetch the new cipher and send value on a particular index whenever requested by emitterFn(). The emitterFn() will initiate the EventEmitter object. We fetch the value on all six indexes one by one and emit an error if it is not an integer.

A variable stores the value received from emitterFn(), and we listen to them using listener.on(). After checking all the indexes, the program will end. The output looks as shown below:

1 index is an integer
2 index is an integer
3 index is an integer
4 index is an integer
5 index is an integer
The index 6 needs to be a digit
All six indexes have been checked

Handling errors

Now that you know the techniques to report errors, let’s handle them.

Retry the operation

Sometimes, errors can be caused by the external system for valid requests. For example, while fetching some coordinates using an API, you get the error 503, service not available, which is caused due to overload or a network failure.

At this point, the service might be back in a few seconds, and reporting an error might not be the ideal thing to do, so you retry the operation. Also, this may not be a good idea if you are deep down the stack because all layers keep retrying the process, and the wait time extends heavily. In such cases, it’s better to abort and let clients retry from their side.

Report the failure to the client

While receiving the wrong input from the client, retrying doesn’t make sense because we might get the same result upon reprocessing the incorrect information. In such cases, the most straightforward way is to finish the rest of the processing and report it to the client.


More great articles from LogRocket:

  • Don’t miss a moment with The Replay, a curated newsletter from LogRocket
  • Learn how LogRocket’s Galileo cuts through the noise to proactively resolve issues in your app
  • Use React’s useEffect to optimize your application’s performance
  • Switch between multiple versions of Node
  • Discover how to animate your React app with AnimXYZ
  • Explore Tauri, a new framework for building binaries
  • Compare NestJS vs. Express.js

Report failures directly top of the stack

Sometimes it’s appropriate to report the errors directly because you might know the cause. For example, the ENOENT error discussed in the try…catch blocks section is generated when you are trying to open a file that is not present, and you can use any of the methods discussed above to report it. In this way, you can report creating the file to solve the error.

Crash immediately

In case of unrecoverable errors, crashing the program is the best option. For example, an error is caused due to accessing a file without permission; there is nothing you can do instead of crashing the system and letting sysadmin provide the access. Crashing is also the most practical way to deal with programmer errors to recover the system.

Conclusion

To conclude, appropriate error handling is mandatory if you strive to write good code and deliver reliable software. In this post, we learned about errors and the importance of handling them in Node.js. We discussed preliminary ways to report the errors: try…catch blocks, callbacks and promises, async/await syntax, and event emitters. We also learned to handle these errors once they are reported.

Lastly, I hope it was helpful to you, and now you can handle errors in your Node.js application.

200’s only Monitor failed and slow network requests in production

Deploying a Node-based web app or website is the easy part. Making sure your Node instance continues to serve resources to your app is where things get tougher. If you’re interested in ensuring requests to the backend or third party services are successful, try LogRocket. LogRocket Network Request Monitoringhttps://logrocket.com/signup/

LogRocket is like a DVR for web and mobile apps, recording literally everything that happens while a user interacts with your app. Instead of guessing why problems happen, you can aggregate and report on problematic network requests to quickly understand the root cause.

LogRocket instruments your app to record baseline performance timings such as page load time, time to first byte, slow network requests, and also logs Redux, NgRx, and Vuex actions/state. Start monitoring for free.

It is not hard to see that some people are struggling to handle errors, and some are even totally missing it. Handling errors properly means not only reducing the development time by finding bugs and errors easily but also developing a robust codebase for large-scale applications.

In particular, Node.js developers sometimes find themselves working with not-so-clean code while handling various kinds of errors, incorrectly applying the same logic everywhere to deal with them. They just keep asking themselves “Is Node.js bad at handling errors?” or If not, how to handle them?” My answer to them is “No, Node.js is not bad at all. That depends on us developers.”

Here is one of my favorite solutions for that.

Types of Errors in Node.js

First of all, it is necessary to have a clear understanding of errors in Node.js. In general, Node.js errors are divided into two distinct categories: operational errors and programmer errors.

  • Operational errors represent runtime problems whose results are expected and should be dealt with in a proper way. Operational errors don’t mean the application itself has bugs, but developers need to handle them thoughtfully. Examples of operational errors include “out of memory,” “an invalid input for an API endpoint,” and so on.
  • Programmer errors represent unexpected bugs in poorly written code. They mean the code itself has some issues to solve and was coded wrong. A good example is to try to read a property of “undefined.” To fix the issue, the code has to be changed. That is a bug a developer made, not an operational error.

With that in mind, you should have no problem distinguishing between these two categories of errors: Operational errors are a natural part of an application, and programmer errors are bugs caused by developers. A logical question that follows is: “Why is it useful to divide them into two categories and deal with them?”

Without a clear understanding of errors, you might feel like restarting an application whenever an error occurs. Does it make sense to restart an application due to “File Not Found” errors when thousands of users are enjoying the application? Absolutely not.

But what about programmer errors? Does it make sense to keep an application running when an unknown bug appears that could result in an unexpected snowball effect in the application? Again, definitely not!

It’s Time to Handle Errors Properly

Assuming you have some experience with async JavaScript and Node.js, you might have experienced drawbacks when using callbacks for dealing with errors. They force you to check errors all the way down to nested ones, causing notorious “callback hell” issues that make it hard to follow the code flow.

Using promises or async/await is a good replacement for callbacks. The typical code flow of async/await looks like the following:

const doAsyncJobs = async () => {
 try {
   const result1 = await job1();
   const result2 = await job2(result1);
   const result3 = await job3(result2);
   return await job4(result3);
 } catch (error) {
   console.error(error);
 } finally {
   await anywayDoThisJob();
 }
}

Using Node.js built-in Error object is a good practice because it includes intuitive and clear information about errors like StackTrace, which most developers depend on to keep track of the root of an error. And additional meaningful properties like HTTP status code and a description by extending the Error class will make it more informative.

class BaseError extends Error {
 public readonly name: string;
 public readonly httpCode: HttpStatusCode;
 public readonly isOperational: boolean;
 
 constructor(name: string, httpCode: HttpStatusCode, description: string, isOperational: boolean) {
   super(description);
   Object.setPrototypeOf(this, new.target.prototype);
 
   this.name = name;
   this.httpCode = httpCode;
   this.isOperational = isOperational;
 
   Error.captureStackTrace(this);
 }
}

//free to extend the BaseError
class APIError extends BaseError {
 constructor(name, httpCode = HttpStatusCode.INTERNAL_SERVER, isOperational = true, description = 'internal server error') {
   super(name, httpCode, isOperational, description);
 }
}

I only implemented some HTTP status codes for the sake of simplicity, but you are free to add more later.

export enum HttpStatusCode {
 OK = 200,
 BAD_REQUEST = 400,
 NOT_FOUND = 404,
 INTERNAL_SERVER = 500,
}

There is no need to extend BaseError or APIError, but it is okay to extend it for common errors according to your needs and personal preferences.

class HTTP400Error extends BaseError {
 constructor(description = 'bad request') {
   super('NOT FOUND', HttpStatusCode.BAD_REQUEST, true, description);
 }
}

So how do you use it? Just throw this in:

...
const user = await User.getUserById(1);
if (user === null)
 throw new APIError(
   'NOT FOUND',
   HttpStatusCode.NOT_FOUND,
   true,
   'detailed explanation'
 );

Centralized Node.js Error-handling

Now, we are ready to build the main component of our Node.js error-handling system: the centralized error-handling component.

It is usually a good idea to build a centralized error-handling component in order to avoid possible code duplications when handling errors. The error-handling component is in charge of making the caught errors understandable by, for example, sending notifications to system admins (if necessary), transferring events to a monitoring service like Sentry.io, and logging them.

Here is a basic workflow for dealing with errors:

In some parts of the code, errors are caught to transfer to an error-handling middleware.

...
try {
 userService.addNewUser(req.body).then((newUser: User) => {
   res.status(200).json(newUser);
 }).catch((error: Error) => {
   next(error)
 });
} catch (error) {
 next(error);
}
...

The error-handling middleware is a good place to distinguish between error types and send them to the centralized error-handling component. Knowing the basics about handling errors in Express.js middleware would certainly help.

app.use(async (err: Error, req: Request, res: Response, next: NextFunction) => {
 if (!errorHandler.isTrustedError(err)) {
   next(err);
 }
 await errorHandler.handleError(err);
});

By now, one can imagine what the centralized component should look like because we have already used some of its functions. Bear in mind that it is totally up to you how to implement it, but it might look like the following:

class ErrorHandler {
 public async handleError(err: Error): Promise<void> {
   await logger.error(
     'Error message from the centralized error-handling component',
     err,
   );
   await sendMailToAdminIfCritical();
   await sendEventsToSentry();
 }
 
 public isTrustedError(error: Error) {
   if (error instanceof BaseError) {
     return error.isOperational;
   }
   return false;
 }
}
export const errorHandler = new ErrorHandler();

Sometimes, the output of the default “console.log” makes it difficult to keep track of errors. Rather, it could be much better to print errors in a formatted way so that developers can quickly understand the issues and make sure they are fixed.

Overall, this will save developers time making it easy to keep track of errors and handle them by increasing their visibility. It is a good decision to employ a customizable logger like winston or morgan.

Here is a customized winston logger:

const customLevels = {
 levels: {
   trace: 5,
   debug: 4,
   info: 3,
   warn: 2,
   error: 1,
   fatal: 0,
 },
 colors: {
   trace: 'white',
   debug: 'green',
   info: 'green',
   warn: 'yellow',
   error: 'red',
   fatal: 'red',
 },
};
 
const formatter = winston.format.combine(
 winston.format.colorize(),
 winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
 winston.format.splat(),
 winston.format.printf((info) => {
   const { timestamp, level, message, ...meta } = info;
 
   return `${timestamp} [${level}]: ${message} ${
     Object.keys(meta).length ? JSON.stringify(meta, null, 2) : ''
   }`;
 }),
);
 
class Logger {
 private logger: winston.Logger;
 
 constructor() {
   const prodTransport = new winston.transports.File({
     filename: 'logs/error.log',
     level: 'error',
   });
   const transport = new winston.transports.Console({
     format: formatter,
   });
   this.logger = winston.createLogger({
     level: isDevEnvironment() ? 'trace' : 'error',
     levels: customLevels.levels,
     transports: [isDevEnvironment() ? transport : prodTransport],
   });
   winston.addColors(customLevels.colors);
 }
 
 trace(msg: any, meta?: any) {
   this.logger.log('trace', msg, meta);
 }
 
 debug(msg: any, meta?: any) {
   this.logger.debug(msg, meta);
 }
 
 info(msg: any, meta?: any) {
   this.logger.info(msg, meta);
 }
 
 warn(msg: any, meta?: any) {
   this.logger.warn(msg, meta);
 }
 
 error(msg: any, meta?: any) {
   this.logger.error(msg, meta);
 }
 
 fatal(msg: any, meta?: any) {
   this.logger.log('fatal', msg, meta);
 }
}
 
export const logger = new Logger();

What it basically provides is logging at multiple different levels in a formatted way, with clear colors, and logging into different output media according to the runtime environment. The good thing with this is you can watch and query logs by using winston’s built-in APIs. Furthermore, you can use a log analysis tool to analyze the formatted log files to get more useful information about the application. It’s awesome, isn’t it?

Up to this point, we mostly discussed dealing with operational errors. How about programmer errors? The best way to deal with these errors is to crash immediately and restart gracefully with an automatic restarter like PM2—the reason being that programmer errors are unexpected, as they are actual bugs that might cause the application to end up in a wrong state and behave in an unexpected way.

process.on('uncaughtException', (error: Error) => {
 errorHandler.handleError(error);
 if (!errorHandler.isTrustedError(error)) {
   process.exit(1);
 }
});

Last but not least, I am going to mention dealing with unhandled promise rejections and exceptions.

You might find yourself spending a lot of time dealing with promises when working on Node.js/Express applications. It is not hard to see warning messages about unhandled promise rejections when you forget to handle rejections.

The warning messages don’t do much except logging, but it is a good practice to use a decent fallback and subscribe to process.on(‘unhandledRejection’, callback).

The typical error-handling flow might look like the following:

// somewhere in the code
...
User.getUserById(1).then((firstUser) => {
  if (firstUser.isSleeping === false) throw new Error('He is not sleeping!');
});
...
 
// get the unhandled rejection and throw it to another fallback handler we already have.
process.on('unhandledRejection', (reason: Error, promise: Promise<any>) => {
 throw reason;
});
 
process.on('uncaughtException', (error: Error) => {
 errorHandler.handleError(error);
 if (!errorHandler.isTrustedError(error)) {
   process.exit(1);
 }
});

Wrapping Up

When all is said and done, you should realize that error-handling is not an optional extra but rather an essential part of an application, both in the development stage and in production.

The strategy of handling errors in a single component in Node.js will ensure developers save valuable time and write clean and maintainable code by avoiding code duplication and missing error context.

I hope you enjoyed reading this article and found the discussed error-handling workflow and implementation helpful for building a robust codebase in Node.js.


Further Reading on the Toptal Engineering Blog:

  • Using Express.js Routes for Promise-based Error Handling

Understanding the basics

  • Why is error-handling important?

    Proper error-handling makes apps robust resulting in superior user experience and improved productivity.

  • What are the strategies or techniques used for error-handling?

    Using promises or async/await, handling errors in a centralized component, handling uncaught exceptions.

  • Why do we need error-handling?

    Error-handling is a must-have part of all apps. It prevents apps from being error-prone and saves valuable development time.

  • How do you handle unchecked exceptions?

    By subscribing to process.on(‘unhandledRejection’), process.on(‘uncaughtException’)

  • Is Node.js bad at handling errors?

    No, it is not. That depends on developers and the way they choose to handle them.

Located in Thunder Bay, ON, Canada

Member since March 20, 2020

About the author

Jay is a full-stack developer with extensive experience in computer science. He specializes in JavaScript but is also proficient in Django, RoR, GraphQL, and SQL.


Toptalauthors are vetted experts in their fields and write on topics in which they have demonstrated experience. All of our content is peer reviewed and validated by Toptal experts in the same field.

Expertise

Hire Jay

This article was originally written by Ayooluwa Isaiah on the Honeybadger Developer Blog.

If you’ve been writing anything more than «Hello world» programs, you are probably familiar with the concept of errors in programming. They are mistakes in your code, often referred to as «bugs», that cause a program to fail or behave unexpectedly. Unlike some languages, such as Go and Rust, where you are forced to interact with potential errors every step of the way, it’s possible to get by without a coherent error handling strategy in JavaScript and Node.js.

It doesn’t have to be this way, though, because Node.js error handling can be quite straightforward once you are familiar with the patterns used to create, deliver, and handle potential errors. This article aims to introduce you to these patterns so that you can make your programs more robust by ensuring that you’ll discover potential errors and handle them appropriately before deploying your application to production!

What are errors in Node.js

An error in Node.js is any instance of the Error object. Common examples include built-in error classes, such as ReferenceError, RangeError, TypeError, URIError, EvalError, and SyntaxError. User-defined errors can also be created by extending the base Error object, a built-in error class, or another custom error. When creating errors in this manner, you should pass a message string that describes the error. This message can be accessed through the message property on the object. The Error object also contains a name and a stack property that indicate the name of the error and the point in the code at which it is created, respectively.

const userError = new TypeError("Something happened!");
console.log(userError.name); // TypeError
console.log(userError.message); // Something happened!
console.log(userError.stack);
/*TypeError: Something happened!
    at Object.<anonymous> (/home/ayo/dev/demo/main.js:2:19)
    <truncated for brevity>
    at node:internal/main/run_main_module:17:47 */

Enter fullscreen mode

Exit fullscreen mode

Once you have an Error object, you can pass it to a function or return it from a function. You can also throw it, which causes the Error object to become an exception. Once you throw an error, it bubbles up the stack until it is caught somewhere. If you fail to catch it, it becomes an uncaught exception, which may cause your application to crash!

How to deliver errors

The appropriate way to deliver errors from a JavaScript function varies depending on whether the function performs a synchronous or asynchronous operation. In this section, I’ll detail four common patterns for delivering errors from a function in a Node.js application.

1. Exceptions

The most common way for functions to deliver errors is by throwing them. When you throw an error, it becomes an exception and needs to be caught somewhere up the stack using a try/catch block. If the error is allowed to bubble up the stack without being caught, it becomes an uncaughtException, which causes the application to exit prematurely. For example, the built-in JSON.parse() method throws an error if its string argument is not a valid JSON object.

function parseJSON(data) {
  return JSON.parse(data);
}

try {
  const result = parseJSON('A string');
} catch (err) {
  console.log(err.message); // Unexpected token A in JSON at position 0
}

Enter fullscreen mode

Exit fullscreen mode

To utilize this pattern in your functions, all you need to do is add the throw keyword before an instance of an error. This pattern of error reporting and handling is idiomatic for functions that perform synchronous operations.

function square(num) {
  if (typeof num !== 'number') {
    throw new TypeError(`Expected number but got: ${typeof num}`);
  }

  return num * num;
}

try {
  square('8');
} catch (err) {
  console.log(err.message); // Expected number but got: string
}

Enter fullscreen mode

Exit fullscreen mode

2. Error-first callbacks

Due to its asynchronous nature, Node.js makes heavy use of callback functions for much of its error handling. A callback function is passed as an argument to another function and executed when the function has finished its work. If you’ve written JavaScript code for any length of time, you probably know that the callback pattern is heavily used throughout JavaScript code.

Node.js uses an error-first callback convention in most of its asynchronous methods to ensure that errors are checked properly before the results of an operation are used. This callback function is usually the last argument to the function that initiates an asynchronous operation, and it is called once when an error occurs or a result is available from the operation. Its signature is shown below:

function (err, result) {}

Enter fullscreen mode

Exit fullscreen mode

The first argument is reserved for the error object. If an error occurs in the course of the asynchronous operation, it will be available via the err argument and result will be undefined. However, if no error occurs, err will be null or undefined, and result will contain the expected result of the operation. This pattern can be demonstrated by reading the contents of a file using the built-in fs.readFile() method:

const fs = require('fs');

fs.readFile('/path/to/file.txt', (err, result) => {
  if (err) {
    console.error(err);
    return;
  }

  // Log the file contents if no error
  console.log(result);
});

Enter fullscreen mode

Exit fullscreen mode

As you can see, the readFile() method expects a callback function as its last argument, which adheres to the error-first function signature discussed earlier. In this scenario, the result argument contains the contents of the file read if no error occurs. Otherwise, it is undefined, and the err argument is populated with an error object containing information about the problem (e.g., file not found or insufficient permissions).

Generally, methods that utilize this callback pattern for error delivery cannot know how important the error they produce is to your application. It could be severe or trivial. Instead of deciding for itself, the error is sent up for you to handle. It is important to control the flow of the contents of the callback function by always checking for an error before attempting to access the result of the operation. Ignoring errors is unsafe, and you should not trust the contents of result before checking for errors.

If you want to use this error-first callback pattern in your own async functions, all you need to do is accept a function as the last argument and call it in the manner shown below:

function square(num, callback) {
  if (typeof callback !== 'function') {
    throw new TypeError(`Callback must be a function. Got: ${typeof callback}`);
  }

  // simulate async operation
  setTimeout(() => {
    if (typeof num !== 'number') {
      // if an error occurs, it is passed as the first argument to the callback
      callback(new TypeError(`Expected number but got: ${typeof num}`));
      return;
    }

    const result = num * num;
    // callback is invoked after the operation completes with the result
    callback(null, result);
  }, 100);
}

Enter fullscreen mode

Exit fullscreen mode

Any caller of this square function would need to pass a callback function to access its result or error. Note that a runtime exception will occur if the callback argument is not a function.

square('8', (err, result) => {
  if (err) {
    console.error(err)
    return
  }

  console.log(result);
});

Enter fullscreen mode

Exit fullscreen mode

You don’t have to handle the error in the callback function directly. You can propagate it up the stack by passing it to a different callback, but make sure not to throw an exception from within the function because it won’t be caught, even if you surround the code in a try/catch block. An asynchronous exception is not catchable because the surrounding try/catch block exits before the callback is executed. Therefore, the exception will propagate to the top of the stack, causing your application to crash unless a handler has been registered for process.on('uncaughtException'), which will be discussed later.

try {
  square('8', (err, result) => {
    if (err) {
      throw err; // not recommended
    }

    console.log(result);
  });
} catch (err) {
  // This won't work
  console.error("Caught error: ", err);
}

Enter fullscreen mode

Exit fullscreen mode

Throwing an error inside the callback can crash the Node.js process

3. Promise rejections

Promises are the modern way to perform asynchronous operations in Node.js and are now generally preferred to callbacks because this approach has a better flow that matches the way we analyze programs, especially with the async/await pattern. Any Node.js API that utilizes error-first callbacks for asynchronous error handling can be converted to promises using the built-in util.promisify() method. For example, here’s how the fs.readFile() method can be made to utilize promises:

const fs = require('fs');
const util = require('util');

const readFile = util.promisify(fs.readFile);

Enter fullscreen mode

Exit fullscreen mode

The readFile variable is a promisified version of fs.readFile() in which promise rejections are used to report errors. These errors can be caught by chaining a catch method, as shown below:

readFile('/path/to/file.txt')
  .then((result) => console.log(result))
  .catch((err) => console.error(err));

Enter fullscreen mode

Exit fullscreen mode

You can also use promisified APIs in an async function, such as the one shown below. This is the predominant way to use promises in modern JavaScript because the code reads like synchronous code, and the familiar try/catch mechanism can be used to handle errors. It is important to use await before the asynchronous method so that the promise is settled (fulfilled or rejected) before the function resumes its execution. If the promise rejects, the await expression throws the rejected value, which is subsequently caught in a surrounding catch block.

(async function callReadFile() {
  try {
    const result = await readFile('/path/to/file.txt');
    console.log(result);
  } catch (err) {
    console.error(err);
  }
})();

Enter fullscreen mode

Exit fullscreen mode

You can utilize promises in your asynchronous functions by returning a promise from the function and placing the function code in the promise callback. If there’s an error, reject with an Error object. Otherwise, resolve the promise with the result so that it’s accessible in the chained .then method or directly as the value of the async function when using async/await.

function square(num) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (typeof num !== 'number') {
        reject(new TypeError(`Expected number but got: ${typeof num}`));
      }

      const result = num * num;
      resolve(result);
    }, 100);
  });
}

square('8')
  .then((result) => console.log(result))
  .catch((err) => console.error(err));

Enter fullscreen mode

Exit fullscreen mode

4. Event emitters

Another pattern that can be used when dealing with long-running asynchronous operations that may produce multiple errors or results is to return an EventEmitter from the function and emit an event for both the success and failure cases. An example of this code is shown below:

const { EventEmitter } = require('events');

function emitCount() {
  const emitter = new EventEmitter();

  let count = 0;
  // Async operation
  const interval = setInterval(() => {
    count++;
    if (count % 4 == 0) {
      emitter.emit(
        'error',
        new Error(`Something went wrong on count: ${count}`)
      );
      return;
    }
    emitter.emit('success', count);

    if (count === 10) {
      clearInterval(interval);
      emitter.emit('end');
    }
  }, 1000);

  return emitter;
}

Enter fullscreen mode

Exit fullscreen mode

The emitCount() function returns a new event emitter that reports both success and failure events in the asynchronous operation. The function increments the count variable and emits a success event every second and an error event if count is divisible by 4. When count reaches 10, an end event is emitted. This pattern allows the streaming of results as they arrive instead of waiting until the entire operation is completed.

Here’s how you can listen and react to each of the events emitted from the emitCount() function:

const counter = emitCount();

counter.on('success', (count) => {
  console.log(`Count is: ${count}`);
});

counter.on('error', (err) => {
  console.error(err.message);
});

counter.on('end', () => {
  console.info('Counter has ended');
});

Enter fullscreen mode

Exit fullscreen mode

EventEmitter demonstration

As you can see from the image above, the callback function for each event listener is executed independently as soon as the event is emitted. The error event is a special case in Node.js because, if there is no listener for it, the Node.js process will crash. You can comment out the error event listener above and run the program to see what happens.

The error event will cause the application to crash if there is no listener for it

Extending the error object

Using the built-in error classes or a generic instance of the Error object is usually not precise enough to communicate all the different error types. Therefore, it is necessary to create custom error classes to better reflect the types of errors that could occur in your application. For example, you could have a ValidationError class for errors that occur while validating user input, DatabaseError class for database operations, TimeoutError for operations that elapse their assigned timeouts, and so on.

Custom error classes that extend the Error object will retain the basic error properties, such as message, name, and stack, but they can also have properties of their own. For example, a ValidationError can be enhanced by adding meaningful properties, such as the portion of the input that caused the error. Essentially, you should include enough information for the error handler to properly handle the error or construct its own error messages.

Here’s how to extend the built-in Error object in Node.js:

class ApplicationError extends Error {
  constructor(message) {
    super(message);
    // name is set to the name of the class
    this.name = this.constructor.name;
  }
}

class ValidationError extends ApplicationError {
  constructor(message, cause) {
    super(message);
    this.cause = cause
  }
}

Enter fullscreen mode

Exit fullscreen mode

The ApplicationError class above is a generic error for the application, while the ValidationError class represents any error that occurs when validating user input. It inherits from the ApplicationError class and augments it with a cause property to specify the input that triggered the error. You can use custom errors in your code just like you would with a normal error. For example, you can throw it:

function validateInput(input) {
  if (!input) {
    throw new ValidationError('Only truthy inputs allowed', input);
  }

  return input;
}

try {
  validateInput(userJson);
} catch (err) {
  if (err instanceof ValidationError) {
    console.error(`Validation error: ${err.message}, caused by: ${err.cause}`);
    return;
  }

  console.error(`Other error: ${err.message}`);
}

Enter fullscreen mode

Exit fullscreen mode

Using custom errors

The instanceof keyword should be used to check for the specific error type, as shown above. Don’t use the name of the error to check for the type, as in err.name === 'ValidationError', because it won’t work if the error is derived from a subclass of ValidationError.

Types of errors

It is beneficial to distinguish between the different types of errors that can occur in a Node.js application. Generally, errors can be siloed into two main categories: programmer mistakes and operational problems. Bad or incorrect arguments to a function is an example of the first kind of problem, while transient failures when dealing with external APIs are firmly in the second category.

1. Operational errors

Operational errors are mostly expected errors that can occur in the course of application execution. They are not necessarily bugs but are external circumstances that can disrupt the flow of program execution. In such cases, the full impact of the error can be understood and handled appropriately. Some examples of operational errors in Node.js include the following:

  • An API request fails for some reason (e.g., the server is down or the rate limit exceeded).
  • A database connection is lost, perhaps due to a faulty network connection.
  • The OS cannot fulfill your request to open a file or write to it.
  • The user sends invalid input to the server, such as an invalid phone number or email address.

These situations do not arise due to mistakes in the application code, but they must be handled correctly. Otherwise, they could cause more serious problems.

2. Programmer errors

Programmer errors are mistakes in the logic or syntax of the program that can only be corrected by changing the source code. These types of errors cannot be handled because, by definition, they are bugs in the program. Some examples of programmer errors include:

  • Syntax errors, such as failing to close a curly brace.
  • Type errors when you try to do something illegal, such as performing operations on operands of mismatched types.
  • Bad parameters when calling a function.
  • Reference errors when you misspell a variable, function, or property name.
  • Trying to access a location beyond the end of an array.
  • Failing to handle an operational error.

Operational error handling

Operational errors are mostly predictable, so they must be anticipated and accounted for during the development process. Essentially, handling these types of errors involves considering whether an operation could fail, why it might fail, and what should happen if it does. Let’s consider a few strategies for handling operational errors in Node.js.

1. Report the error up the stack

In many cases, the appropriate action is to stop the flow of the program’s execution, clean up any unfinished processes, and report the error up the stack so that it can be handled appropriately. This is often the correct way to address the error when the function where it occurred is further down the stack such that it does not have enough information to handle the error directly. Reporting the error can be done through any of the error delivery methods discussed earlier in this article.

2. Retry the operation

Reddit 503 error

Network requests to external services may sometimes fail, even if the request is completely valid. This may be due to a transient failure, which can occur if there is a network failure or server overload. Such issues are usually ephemeral, so instead of reporting the error immediately, you can retry the request a few times until it succeeds or until the maximum amount of retries is reached. The first consideration is determining whether it’s appropriate to retry the request. For example, if the initial response HTTP status code is 500, 503, or 429, it might be advantageous to retry the request after a short delay.

You can check whether the Retry-After HTTP header is present in the response. This header indicates the exact amount of time to wait before making a follow-up request. If the Retry-After header does not exist, you need to delay the follow-up request and progressively increase the delay for each consecutive retry. This is known as the exponential back-off strategy. You also need to decide the maximum delay interval and how many times to retry the request before giving up. At that point, you should inform the caller that the target service is unavailable.

3. Send the error to the client

When dealing with external input from users, it should be assumed that the input is bad by default. Therefore, the first thing to do before starting any processes is to validate the input and report any mistakes to the user promptly so that it can be corrected and resent. When delivering client errors, make sure to include all the information that the client needs to construct an error message that makes sense to the user.

4. Abort the program

In the case of unrecoverable system errors, the only reasonable course of action is to log the error and terminate the program immediately. You might not even be able to shut down the server gracefully if the exception is unrecoverable at the JavaScript layer. At that point, a sysadmin may be required to look into the issue and fix it before the program can start again.

Preventing programmer errors

Due to their nature, programmer errors cannot be handled; they are bugs in the program that arise due to broken code or logic, which must subsequently be corrected. However, there are a few things you can do to greatly reduce the frequency at which they occur in your application.

1. Adopt TypeScript

TypeScript is a strongly typed superset of JavaScript. Its primary design goal is to statically identify constructs likely to be errors without any runtime penalties. By adopting TypeScript in your project (with the strictest possible compiler options), you can eliminate a whole class of programmer errors at compile time. For example, after conducting a postmortem analysis of bugs, it was estimated that 38% of bugs in the Airbnb codebase were preventable with TypeScript.

When you migrate your entire project over to TypeScript, errors like «undefined is not a function», syntax errors, or reference errors should no longer exist in your codebase. Thankfully, this is not as daunting as it sounds. Migrating your entire Node.js application to TypeScript can be done incrementally so that you can start reaping the rewards immediately in crucial parts of the codebase. You can also adopt a tool like ts-migrate if you intend to perform the migration in one go.

2. Define the behavior for bad parameters

Many programmer errors result from passing bad parameters. These might be due not only to obvious mistakes, such as passing a string instead of a number, but also to subtle mistakes, such as when a function argument is of the correct type but outside the range of what the function can handle. When the program is running and the function is called that way, it might fail silently and produce a wrong value, such as NaN. When the failure is eventually noticed (usually after traveling through several other functions), it might be difficult to locate its origins.

You can deal with bad parameters by defining their behavior either by throwing an error or returning a special value, such as null, undefined, or -1, when the problem can be handled locally. The former is the approach used by JSON.parse(), which throws a SyntaxError exception if the string to parse is not valid JSON, while the string.indexOf() method is an example of the latter. Whichever you choose, make sure to document how the function deals with errors so that the caller knows what to expect.

3. Automated testing

On its own, the JavaScript language doesn’t do much to help you find mistakes in the logic of your program, so you have to run the program to determine whether it works as expected. The presence of an automated test suite makes it far more likely that you will spot and fix various programmer errors, especially logic errors. They are also helpful in ascertaining how a function deals with atypical values. Using a testing framework, such as Jest or Mocha, is a good way to get started with unit testing your Node.js applications.

Uncaught exceptions and unhandled promise rejections

Uncaught exceptions and unhandled promise rejections are caused by programmer errors resulting from the failure to catch a thrown exception and a promise rejection, respectively. The uncaughtException event is emitted when an exception thrown somewhere in the application is not caught before it reaches the event loop. If an uncaught exception is detected, the application will crash immediately, but you can add a handler for this event to override this behavior. Indeed, many people use this as a last resort way to swallow the error so that the application can continue running as if nothing happened:

// unsafe
process.on('uncaughtException', (err) => {
  console.error(err);
});

Enter fullscreen mode

Exit fullscreen mode

However, this is an incorrect use of this event because the presence of an uncaught exception indicates that the application is in an undefined state. Therefore, attempting to resume normally without recovering from the error is considered unsafe and could lead to further problems, such as memory leaks and hanging sockets. The appropriate use of the uncaughtException handler is to clean up any allocated resources, close connections, and log the error for later assessment before exiting the process.

// better
process.on('uncaughtException', (err) => {
  Honeybadger.notify(error); // log the error in a permanent storage
  // attempt a gracefully shutdown
  server.close(() => {
    process.exit(1); // then exit
  });

  // If a graceful shutdown is not achieved after 1 second,
  // shut down the process completely
  setTimeout(() => {
    process.abort(); // exit immediately and generate a core dump file
  }, 1000).unref()
});

Enter fullscreen mode

Exit fullscreen mode

Similarly, the unhandledRejection event is emitted when a rejected promise is not handled with a catch block. Unlike uncaughtException, these events do not cause the application to crash immediately. However, unhandled promise rejections have been deprecated and may terminate the process immediately in a future Node.js release. You can keep track of unhandled promise rejections through an unhandledRejection event listener, as shown below:

process.on('unhandledRejection', (reason, promise) => {
  Honeybadger.notify({
    message: 'Unhandled promise rejection',
    params: {
      promise,
      reason,
    },
  });
  server.close(() => {
    process.exit(1);
  });

  setTimeout(() => {
    process.abort();
  }, 1000).unref()
});

Enter fullscreen mode

Exit fullscreen mode

You should always run your servers using a process manager that will automatically restart them in the event of a crash. A common one is PM2, but you also have systemd or upstart on Linux, and Docker users can use its restart policy. Once this is in place, reliable service will be restored almost instantly, and you’ll still have the details of the uncaught exception so that it can be investigated and corrected promptly. You can go further by running more than one process and employ a load balancer to distribute incoming requests. This will help to prevent downtime in case one of the instances is lost temporarily.

Centralized error reporting

No error handling strategy is complete without a robust logging strategy for your running application. When a failure occurs, it’s important to learn why it happened by logging as much information as possible about the problem. Centralizing these logs makes it easy to get full visibility into your application. You’ll be able to sort and filter your errors, see top problems, and subscribe to alerts to get notified of new errors.

Honeybadger provides everything you need to monitor errors that occur in your production application. Follow the steps below to integrate it into your Node.js app:

1. Install the Package

Use npm to install the package:

$ npm install @honeybadger-io/js --save

Enter fullscreen mode

Exit fullscreen mode

2. Import the Library

Import the library and configure it with your API key to begin reporting errors:

const Honeybadger = require('@honeybadger-io/js');
Honeybadger.configure({
  apiKey: '[ YOUR API KEY HERE ]'
});

Enter fullscreen mode

Exit fullscreen mode

3. Report Errors

You can report an error by calling the notify() method, as shown in the following example:

try {
  // ...error producing code
} catch(error) {
  Honeybadger.notify(error);
}

Enter fullscreen mode

Exit fullscreen mode

For more information on how Honeybadger integrates with Node.js web frameworks, see the full documentation or check out the sample Node.js/Express application on GitHub.

Summary

The Error class (or a subclass) should always be used to communicate errors in your code. Technically, you can throw anything in JavaScript, not just Error objects, but this is not recommended since it greatly reduces the usefulness of the error and makes error handling error prone. By consistently using Error objects, you can reliably expect to access error.message or error.stack in places where the errors are being handled or logged. You can even augment the error class with other useful properties relevant to the context in which the error occurred.

Operational errors are unavoidable and should be accounted for in any correct program. Most of the time, a recoverable error strategy should be employed so that the program can continue running smoothly. However, if the error is severe enough, it might be appropriate to terminate the program and restart it. Try to shut down gracefully if such situations arise so that the program can start up again in a clean state.

Programmer errors cannot be handled or recovered from, but they can be mitigated with an automated test suite and static typing tools. When writing a function, define the behavior for bad parameters and act appropriately once detected. Allow the program to crash if an uncaughtException or unhandledRejection is detected. Don’t try to recover from such errors!

Use an error monitoring service, such as Honeybadger, to capture and analyze your errors. This can help you drastically improve the speed of debugging and resolution.

Conclusion

Proper error handling is a non-negotiable requirement if you’re aiming to write good and reliable software. By employing the techniques described in this article, you will be well on your way to doing just that.

Thanks for reading, and happy coding!

Applications running in Node.js will generally experience four categories of
errors:

  • Standard JavaScript errors such as {EvalError}, {SyntaxError}, {RangeError},
    {ReferenceError}, {TypeError}, and {URIError}.
  • System errors triggered by underlying operating system constraints such
    as attempting to open a file that does not exist or attempting to send data
    over a closed socket.
  • User-specified errors triggered by application code.
  • AssertionErrors are a special class of error that can be triggered when
    Node.js detects an exceptional logic violation that should never occur. These
    are raised typically by the node:assert module.

All JavaScript and system errors raised by Node.js inherit from, or are
instances of, the standard JavaScript {Error} class and are guaranteed
to provide at least the properties available on that class.

Error propagation and interception

Node.js supports several mechanisms for propagating and handling errors that
occur while an application is running. How these errors are reported and
handled depends entirely on the type of Error and the style of the API that is
called.

All JavaScript errors are handled as exceptions that immediately generate
and throw an error using the standard JavaScript throw mechanism. These
are handled using the try…catch construct provided by the
JavaScript language.

// Throws with a ReferenceError because z is not defined.
try {
  const m = 1;
  const n = m + z;
} catch (err) {
  // Handle the error here.
}

Any use of the JavaScript throw mechanism will raise an exception that
must be handled using try…catch or the Node.js process will exit
immediately.

With few exceptions, Synchronous APIs (any blocking method that does not
accept a callback function, such as fs.readFileSync), will use throw
to report errors.

Errors that occur within Asynchronous APIs may be reported in multiple ways:

  • Most asynchronous methods that accept a callback function will accept an
    Error object passed as the first argument to that function. If that first
    argument is not null and is an instance of Error, then an error occurred
    that should be handled.

    const fs = require('node:fs');
    fs.readFile('a file that does not exist', (err, data) => {
      if (err) {
        console.error('There was an error reading the file!', err);
        return;
      }
      // Otherwise handle the data
    });
  • When an asynchronous method is called on an object that is an
    EventEmitter, errors can be routed to that object’s 'error' event.

    const net = require('node:net');
    const connection = net.connect('localhost');
    
    // Adding an 'error' event handler to a stream:
    connection.on('error', (err) => {
      // If the connection is reset by the server, or if it can't
      // connect at all, or on any sort of error encountered by
      // the connection, the error will be sent here.
      console.error(err);
    });
    
    connection.pipe(process.stdout);
  • A handful of typically asynchronous methods in the Node.js API may still
    use the throw mechanism to raise exceptions that must be handled using
    try…catch. There is no comprehensive list of such methods; please
    refer to the documentation of each method to determine the appropriate
    error handling mechanism required.

The use of the 'error' event mechanism is most common for stream-based
and event emitter-based APIs, which themselves represent a series of
asynchronous operations over time (as opposed to a single operation that may
pass or fail).

For all EventEmitter objects, if an 'error' event handler is not
provided, the error will be thrown, causing the Node.js process to report an
uncaught exception and crash unless either: The domain module is
used appropriately or a handler has been registered for the
'uncaughtException' event.

const EventEmitter = require('node:events');
const ee = new EventEmitter();

setImmediate(() => {
  // This will crash the process because no 'error' event
  // handler has been added.
  ee.emit('error', new Error('This will crash'));
});

Errors generated in this way cannot be intercepted using try…catch as
they are thrown after the calling code has already exited.

Developers must refer to the documentation for each method to determine
exactly how errors raised by those methods are propagated.

Error-first callbacks

Most asynchronous methods exposed by the Node.js core API follow an idiomatic
pattern referred to as an error-first callback. With this pattern, a callback
function is passed to the method as an argument. When the operation either
completes or an error is raised, the callback function is called with the
Error object (if any) passed as the first argument. If no error was raised,
the first argument will be passed as null.

const fs = require('node:fs');

function errorFirstCallback(err, data) {
  if (err) {
    console.error('There was an error', err);
    return;
  }
  console.log(data);
}

fs.readFile('/some/file/that/does-not-exist', errorFirstCallback);
fs.readFile('/some/file/that/does-exist', errorFirstCallback);

The JavaScript try…catch mechanism cannot be used to intercept errors
generated by asynchronous APIs. A common mistake for beginners is to try to
use throw inside an error-first callback:

// THIS WILL NOT WORK:
const fs = require('node:fs');

try {
  fs.readFile('/some/file/that/does-not-exist', (err, data) => {
    // Mistaken assumption: throwing here...
    if (err) {
      throw err;
    }
  });
} catch (err) {
  // This will not catch the throw!
  console.error(err);
}

This will not work because the callback function passed to fs.readFile() is
called asynchronously. By the time the callback has been called, the
surrounding code, including the try…catch block, will have already exited.
Throwing an error inside the callback can crash the Node.js process in most
cases. If domains are enabled, or a handler has been registered with
process.on('uncaughtException'), such errors can be intercepted.

Class: Error

A generic JavaScript {Error} object that does not denote any specific
circumstance of why the error occurred. Error objects capture a «stack trace»
detailing the point in the code at which the Error was instantiated, and may
provide a text description of the error.

All errors generated by Node.js, including all system and JavaScript errors,
will either be instances of, or inherit from, the Error class.

new Error(message[, options])

  • message {string}
  • options {Object}
    • cause {any} The error that caused the newly created error.

Creates a new Error object and sets the error.message property to the
provided text message. If an object is passed as message, the text message
is generated by calling String(message). If the cause option is provided,
it is assigned to the error.cause property. The error.stack property will
represent the point in the code at which new Error() was called. Stack traces
are dependent on V8’s stack trace API. Stack traces extend only to either
(a) the beginning of synchronous code execution, or (b) the number of frames
given by the property Error.stackTraceLimit, whichever is smaller.

Error.captureStackTrace(targetObject[, constructorOpt])

  • targetObject {Object}
  • constructorOpt {Function}

Creates a .stack property on targetObject, which when accessed returns
a string representing the location in the code at which
Error.captureStackTrace() was called.

const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack;  // Similar to `new Error().stack`

The first line of the trace will be prefixed with
${myObject.name}: ${myObject.message}.

The optional constructorOpt argument accepts a function. If given, all frames
above constructorOpt, including constructorOpt, will be omitted from the
generated stack trace.

The constructorOpt argument is useful for hiding implementation
details of error generation from the user. For instance:

function MyError() {
  Error.captureStackTrace(this, MyError);
}

// Without passing MyError to captureStackTrace, the MyError
// frame would show up in the .stack property. By passing
// the constructor, we omit that frame, and retain all frames below it.
new MyError().stack;

Error.stackTraceLimit

  • {number}

The Error.stackTraceLimit property specifies the number of stack frames
collected by a stack trace (whether generated by new Error().stack or
Error.captureStackTrace(obj)).

The default value is 10 but may be set to any valid JavaScript number. Changes
will affect any stack trace captured after the value has been changed.

If set to a non-number value, or set to a negative number, stack traces will
not capture any frames.

error.cause

  • {any}

If present, the error.cause property is the underlying cause of the Error.
It is used when catching an error and throwing a new one with a different
message or code in order to still have access to the original error.

The error.cause property is typically set by calling
new Error(message, { cause }). It is not set by the constructor if the
cause option is not provided.

This property allows errors to be chained. When serializing Error objects,
util.inspect() recursively serializes error.cause if it is set.

const cause = new Error('The remote HTTP server responded with a 500 status');
const symptom = new Error('The message failed to send', { cause });

console.log(symptom);
// Prints:
//   Error: The message failed to send
//       at REPL2:1:17
//       at Script.runInThisContext (node:vm:130:12)
//       ... 7 lines matching cause stack trace ...
//       at [_line] [as _line] (node:internal/readline/interface:886:18) {
//     [cause]: Error: The remote HTTP server responded with a 500 status
//         at REPL1:1:15
//         at Script.runInThisContext (node:vm:130:12)
//         at REPLServer.defaultEval (node:repl:574:29)
//         at bound (node:domain:426:15)
//         at REPLServer.runBound [as eval] (node:domain:437:12)
//         at REPLServer.onLine (node:repl:902:10)
//         at REPLServer.emit (node:events:549:35)
//         at REPLServer.emit (node:domain:482:12)
//         at [_onLine] [as _onLine] (node:internal/readline/interface:425:12)
//         at [_line] [as _line] (node:internal/readline/interface:886:18)

error.code

  • {string}

The error.code property is a string label that identifies the kind of error.
error.code is the most stable way to identify an error. It will only change
between major versions of Node.js. In contrast, error.message strings may
change between any versions of Node.js. See Node.js error codes for details
about specific codes.

error.message

  • {string}

The error.message property is the string description of the error as set by
calling new Error(message). The message passed to the constructor will also
appear in the first line of the stack trace of the Error, however changing
this property after the Error object is created may not change the first
line of the stack trace (for example, when error.stack is read before this
property is changed).

const err = new Error('The message');
console.error(err.message);
// Prints: The message

error.stack

  • {string}

The error.stack property is a string describing the point in the code at which
the Error was instantiated.

Error: Things keep happening!
   at /home/gbusey/file.js:525:2
   at Frobnicator.refrobulate (/home/gbusey/business-logic.js:424:21)
   at Actor.<anonymous> (/home/gbusey/actors.js:400:8)
   at increaseSynergy (/home/gbusey/actors.js:701:6)

The first line is formatted as <error class name>: <error message>, and
is followed by a series of stack frames (each line beginning with «at «).
Each frame describes a call site within the code that lead to the error being
generated. V8 attempts to display a name for each function (by variable name,
function name, or object method name), but occasionally it will not be able to
find a suitable name. If V8 cannot determine a name for the function, only
location information will be displayed for that frame. Otherwise, the
determined function name will be displayed with location information appended
in parentheses.

Frames are only generated for JavaScript functions. If, for example, execution
synchronously passes through a C++ addon function called cheetahify which
itself calls a JavaScript function, the frame representing the cheetahify call
will not be present in the stack traces:

const cheetahify = require('./native-binding.node');

function makeFaster() {
  // `cheetahify()` *synchronously* calls speedy.
  cheetahify(function speedy() {
    throw new Error('oh no!');
  });
}

makeFaster();
// will throw:
//   /home/gbusey/file.js:6
//       throw new Error('oh no!');
//           ^
//   Error: oh no!
//       at speedy (/home/gbusey/file.js:6:11)
//       at makeFaster (/home/gbusey/file.js:5:3)
//       at Object.<anonymous> (/home/gbusey/file.js:10:1)
//       at Module._compile (module.js:456:26)
//       at Object.Module._extensions..js (module.js:474:10)
//       at Module.load (module.js:356:32)
//       at Function.Module._load (module.js:312:12)
//       at Function.Module.runMain (module.js:497:10)
//       at startup (node.js:119:16)
//       at node.js:906:3

The location information will be one of:

  • native, if the frame represents a call internal to V8 (as in [].forEach).
  • plain-filename.js:line:column, if the frame represents a call internal
    to Node.js.
  • /absolute/path/to/file.js:line:column, if the frame represents a call in
    a user program (using CommonJS module system), or its dependencies.
  • <transport-protocol>:///url/to/module/file.mjs:line:column, if the frame
    represents a call in a user program (using ES module system), or
    its dependencies.

The string representing the stack trace is lazily generated when the
error.stack property is accessed.

The number of frames captured by the stack trace is bounded by the smaller of
Error.stackTraceLimit or the number of available frames on the current event
loop tick.

Class: AssertionError

  • Extends: {errors.Error}

Indicates the failure of an assertion. For details, see
Class: assert.AssertionError.

Class: RangeError

  • Extends: {errors.Error}

Indicates that a provided argument was not within the set or range of
acceptable values for a function; whether that is a numeric range, or
outside the set of options for a given function parameter.

require('node:net').connect(-1);
// Throws "RangeError: "port" option should be >= 0 and < 65536: -1"

Node.js will generate and throw RangeError instances immediately as a form
of argument validation.

Class: ReferenceError

  • Extends: {errors.Error}

Indicates that an attempt is being made to access a variable that is not
defined. Such errors commonly indicate typos in code, or an otherwise broken
program.

While client code may generate and propagate these errors, in practice, only V8
will do so.

doesNotExist;
// Throws ReferenceError, doesNotExist is not a variable in this program.

Unless an application is dynamically generating and running code,
ReferenceError instances indicate a bug in the code or its dependencies.

Class: SyntaxError

  • Extends: {errors.Error}

Indicates that a program is not valid JavaScript. These errors may only be
generated and propagated as a result of code evaluation. Code evaluation may
happen as a result of eval, Function, require, or vm. These errors
are almost always indicative of a broken program.

try {
  require('node:vm').runInThisContext('binary ! isNotOk');
} catch (err) {
  // 'err' will be a SyntaxError.
}

SyntaxError instances are unrecoverable in the context that created them –
they may only be caught by other contexts.

Class: SystemError

  • Extends: {errors.Error}

Node.js generates system errors when exceptions occur within its runtime
environment. These usually occur when an application violates an operating
system constraint. For example, a system error will occur if an application
attempts to read a file that does not exist.

  • address {string} If present, the address to which a network connection
    failed
  • code {string} The string error code
  • dest {string} If present, the file path destination when reporting a file
    system error
  • errno {number} The system-provided error number
  • info {Object} If present, extra details about the error condition
  • message {string} A system-provided human-readable description of the error
  • path {string} If present, the file path when reporting a file system error
  • port {number} If present, the network connection port that is not available
  • syscall {string} The name of the system call that triggered the error

error.address

  • {string}

If present, error.address is a string describing the address to which a
network connection failed.

error.code

  • {string}

The error.code property is a string representing the error code.

error.dest

  • {string}

If present, error.dest is the file path destination when reporting a file
system error.

error.errno

  • {number}

The error.errno property is a negative number which corresponds
to the error code defined in libuv Error handling.

On Windows the error number provided by the system will be normalized by libuv.

To get the string representation of the error code, use
util.getSystemErrorName(error.errno).

error.info

  • {Object}

If present, error.info is an object with details about the error condition.

error.message

  • {string}

error.message is a system-provided human-readable description of the error.

error.path

  • {string}

If present, error.path is a string containing a relevant invalid pathname.

error.port

  • {number}

If present, error.port is the network connection port that is not available.

error.syscall

  • {string}

The error.syscall property is a string describing the syscall that failed.

Common system errors

This is a list of system errors commonly-encountered when writing a Node.js
program. For a comprehensive list, see the errno(3) man page.

  • EACCES (Permission denied): An attempt was made to access a file in a way
    forbidden by its file access permissions.

  • EADDRINUSE (Address already in use): An attempt to bind a server
    (net, http, or https) to a local address failed due to
    another server on the local system already occupying that address.

  • ECONNREFUSED (Connection refused): No connection could be made because the
    target machine actively refused it. This usually results from trying to
    connect to a service that is inactive on the foreign host.

  • ECONNRESET (Connection reset by peer): A connection was forcibly closed by
    a peer. This normally results from a loss of the connection on the remote
    socket due to a timeout or reboot. Commonly encountered via the http
    and net modules.

  • EEXIST (File exists): An existing file was the target of an operation that
    required that the target not exist.

  • EISDIR (Is a directory): An operation expected a file, but the given
    pathname was a directory.

  • EMFILE (Too many open files in system): Maximum number of
    file descriptors allowable on the system has been reached, and
    requests for another descriptor cannot be fulfilled until at least one
    has been closed. This is encountered when opening many files at once in
    parallel, especially on systems (in particular, macOS) where there is a low
    file descriptor limit for processes. To remedy a low limit, run
    ulimit -n 2048 in the same shell that will run the Node.js process.

  • ENOENT (No such file or directory): Commonly raised by fs operations
    to indicate that a component of the specified pathname does not exist. No
    entity (file or directory) could be found by the given path.

  • ENOTDIR (Not a directory): A component of the given pathname existed, but
    was not a directory as expected. Commonly raised by fs.readdir.

  • ENOTEMPTY (Directory not empty): A directory with entries was the target
    of an operation that requires an empty directory, usually fs.unlink.

  • ENOTFOUND (DNS lookup failed): Indicates a DNS failure of either
    EAI_NODATA or EAI_NONAME. This is not a standard POSIX error.

  • EPERM (Operation not permitted): An attempt was made to perform an
    operation that requires elevated privileges.

  • EPIPE (Broken pipe): A write on a pipe, socket, or FIFO for which there is
    no process to read the data. Commonly encountered at the net and
    http layers, indicative that the remote side of the stream being
    written to has been closed.

  • ETIMEDOUT (Operation timed out): A connect or send request failed because
    the connected party did not properly respond after a period of time. Usually
    encountered by http or net. Often a sign that a socket.end()
    was not properly called.

Class: TypeError

  • Extends {errors.Error}

Indicates that a provided argument is not an allowable type. For example,
passing a function to a parameter which expects a string would be a TypeError.

require('node:url').parse(() => { });
// Throws TypeError, since it expected a string.

Node.js will generate and throw TypeError instances immediately as a form
of argument validation.

Exceptions vs. errors

A JavaScript exception is a value that is thrown as a result of an invalid
operation or as the target of a throw statement. While it is not required
that these values are instances of Error or classes which inherit from
Error, all exceptions thrown by Node.js or the JavaScript runtime will be
instances of Error.

Some exceptions are unrecoverable at the JavaScript layer. Such exceptions
will always cause the Node.js process to crash. Examples include assert()
checks or abort() calls in the C++ layer.

OpenSSL errors

Errors originating in crypto or tls are of class Error, and in addition to
the standard .code and .message properties, may have some additional
OpenSSL-specific properties.

error.opensslErrorStack

An array of errors that can give context to where in the OpenSSL library an
error originates from.

error.function

The OpenSSL function the error originates in.

error.library

The OpenSSL library the error originates in.

error.reason

A human-readable string describing the reason for the error.

Node.js error codes

ABORT_ERR

Used when an operation has been aborted (typically using an AbortController).

APIs not using AbortSignals typically do not raise an error with this code.

This code does not use the regular ERR_* convention Node.js errors use in
order to be compatible with the web platform’s AbortError.

ERR_AMBIGUOUS_ARGUMENT

A function argument is being used in a way that suggests that the function
signature may be misunderstood. This is thrown by the node:assert module when
the message parameter in assert.throws(block, message) matches the error
message thrown by block because that usage suggests that the user believes
message is the expected message rather than the message the AssertionError
will display if block does not throw.

ERR_ARG_NOT_ITERABLE

An iterable argument (i.e. a value that works with for...of loops) was
required, but not provided to a Node.js API.

ERR_ASSERTION

A special type of error that can be triggered whenever Node.js detects an
exceptional logic violation that should never occur. These are raised typically
by the node:assert module.

ERR_ASYNC_CALLBACK

An attempt was made to register something that is not a function as an
AsyncHooks callback.

ERR_ASYNC_TYPE

The type of an asynchronous resource was invalid. Users are also able
to define their own types if using the public embedder API.

ERR_BROTLI_COMPRESSION_FAILED

Data passed to a Brotli stream was not successfully compressed.

ERR_BROTLI_INVALID_PARAM

An invalid parameter key was passed during construction of a Brotli stream.

ERR_BUFFER_CONTEXT_NOT_AVAILABLE

An attempt was made to create a Node.js Buffer instance from addon or embedder
code, while in a JS engine Context that is not associated with a Node.js
instance. The data passed to the Buffer method will have been released
by the time the method returns.

When encountering this error, a possible alternative to creating a Buffer
instance is to create a normal Uint8Array, which only differs in the
prototype of the resulting object. Uint8Arrays are generally accepted in all
Node.js core APIs where Buffers are; they are available in all Contexts.

ERR_BUFFER_OUT_OF_BOUNDS

An operation outside the bounds of a Buffer was attempted.

ERR_BUFFER_TOO_LARGE

An attempt has been made to create a Buffer larger than the maximum allowed
size.

ERR_CANNOT_WATCH_SIGINT

Node.js was unable to watch for the SIGINT signal.

ERR_CHILD_CLOSED_BEFORE_REPLY

A child process was closed before the parent received a reply.

ERR_CHILD_PROCESS_IPC_REQUIRED

Used when a child process is being forked without specifying an IPC channel.

ERR_CHILD_PROCESS_STDIO_MAXBUFFER

Used when the main process is trying to read data from the child process’s
STDERR/STDOUT, and the data’s length is longer than the maxBuffer option.

ERR_CLOSED_MESSAGE_PORT

There was an attempt to use a MessagePort instance in a closed
state, usually after .close() has been called.

ERR_CONSOLE_WRITABLE_STREAM

Console was instantiated without stdout stream, or Console has a
non-writable stdout or stderr stream.

ERR_CONSTRUCT_CALL_INVALID

A class constructor was called that is not callable.

ERR_CONSTRUCT_CALL_REQUIRED

A constructor for a class was called without new.

ERR_CONTEXT_NOT_INITIALIZED

The vm context passed into the API is not yet initialized. This could happen
when an error occurs (and is caught) during the creation of the
context, for example, when the allocation fails or the maximum call stack
size is reached when the context is created.

ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED

A client certificate engine was requested that is not supported by the version
of OpenSSL being used.

ERR_CRYPTO_ECDH_INVALID_FORMAT

An invalid value for the format argument was passed to the crypto.ECDH()
class getPublicKey() method.

ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY

An invalid value for the key argument has been passed to the
crypto.ECDH() class computeSecret() method. It means that the public
key lies outside of the elliptic curve.

ERR_CRYPTO_ENGINE_UNKNOWN

An invalid crypto engine identifier was passed to
require('node:crypto').setEngine().

ERR_CRYPTO_FIPS_FORCED

The --force-fips command-line argument was used but there was an attempt
to enable or disable FIPS mode in the node:crypto module.

ERR_CRYPTO_FIPS_UNAVAILABLE

An attempt was made to enable or disable FIPS mode, but FIPS mode was not
available.

ERR_CRYPTO_HASH_FINALIZED

hash.digest() was called multiple times. The hash.digest() method must
be called no more than one time per instance of a Hash object.

ERR_CRYPTO_HASH_UPDATE_FAILED

hash.update() failed for any reason. This should rarely, if ever, happen.

ERR_CRYPTO_INCOMPATIBLE_KEY

The given crypto keys are incompatible with the attempted operation.

ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS

The selected public or private key encoding is incompatible with other options.

ERR_CRYPTO_INITIALIZATION_FAILED

Initialization of the crypto subsystem failed.

ERR_CRYPTO_INVALID_AUTH_TAG

An invalid authentication tag was provided.

ERR_CRYPTO_INVALID_COUNTER

An invalid counter was provided for a counter-mode cipher.

ERR_CRYPTO_INVALID_CURVE

An invalid elliptic-curve was provided.

ERR_CRYPTO_INVALID_DIGEST

An invalid crypto digest algorithm was specified.

ERR_CRYPTO_INVALID_IV

An invalid initialization vector was provided.

ERR_CRYPTO_INVALID_JWK

An invalid JSON Web Key was provided.

ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE

The given crypto key object’s type is invalid for the attempted operation.

ERR_CRYPTO_INVALID_KEYLEN

An invalid key length was provided.

ERR_CRYPTO_INVALID_KEYPAIR

An invalid key pair was provided.

ERR_CRYPTO_INVALID_KEYTYPE

An invalid key type was provided.

ERR_CRYPTO_INVALID_MESSAGELEN

An invalid message length was provided.

ERR_CRYPTO_INVALID_SCRYPT_PARAMS

Invalid scrypt algorithm parameters were provided.

ERR_CRYPTO_INVALID_STATE

A crypto method was used on an object that was in an invalid state. For
instance, calling cipher.getAuthTag() before calling cipher.final().

ERR_CRYPTO_INVALID_TAG_LENGTH

An invalid authentication tag length was provided.

ERR_CRYPTO_JOB_INIT_FAILED

Initialization of an asynchronous crypto operation failed.

ERR_CRYPTO_JWK_UNSUPPORTED_CURVE

Key’s Elliptic Curve is not registered for use in the
JSON Web Key Elliptic Curve Registry.

ERR_CRYPTO_JWK_UNSUPPORTED_KEY_TYPE

Key’s Asymmetric Key Type is not registered for use in the
JSON Web Key Types Registry.

ERR_CRYPTO_OPERATION_FAILED

A crypto operation failed for an otherwise unspecified reason.

ERR_CRYPTO_PBKDF2_ERROR

The PBKDF2 algorithm failed for unspecified reasons. OpenSSL does not provide
more details and therefore neither does Node.js.

ERR_CRYPTO_SCRYPT_INVALID_PARAMETER

One or more crypto.scrypt() or crypto.scryptSync() parameters are
outside their legal range.

ERR_CRYPTO_SCRYPT_NOT_SUPPORTED

Node.js was compiled without scrypt support. Not possible with the official
release binaries but can happen with custom builds, including distro builds.

ERR_CRYPTO_SIGN_KEY_REQUIRED

A signing key was not provided to the sign.sign() method.

ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH

crypto.timingSafeEqual() was called with Buffer, TypedArray, or
DataView arguments of different lengths.

ERR_CRYPTO_UNKNOWN_CIPHER

An unknown cipher was specified.

ERR_CRYPTO_UNKNOWN_DH_GROUP

An unknown Diffie-Hellman group name was given. See
crypto.getDiffieHellman() for a list of valid group names.

ERR_CRYPTO_UNSUPPORTED_OPERATION

An attempt to invoke an unsupported crypto operation was made.

ERR_DEBUGGER_ERROR

An error occurred with the debugger.

ERR_DEBUGGER_STARTUP_ERROR

The debugger timed out waiting for the required host/port to be free.

ERR_DLOPEN_DISABLED

Loading native addons has been disabled using --no-addons.

ERR_DLOPEN_FAILED

A call to process.dlopen() failed.

ERR_DIR_CLOSED

The fs.Dir was previously closed.

ERR_DIR_CONCURRENT_OPERATION

A synchronous read or close call was attempted on an fs.Dir which has
ongoing asynchronous operations.

ERR_DNS_SET_SERVERS_FAILED

c-ares failed to set the DNS server.

ERR_DOMAIN_CALLBACK_NOT_AVAILABLE

The node:domain module was not usable since it could not establish the
required error handling hooks, because
process.setUncaughtExceptionCaptureCallback() had been called at an
earlier point in time.

ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE

process.setUncaughtExceptionCaptureCallback() could not be called
because the node:domain module has been loaded at an earlier point in time.

The stack trace is extended to include the point in time at which the
node:domain module had been loaded.

ERR_DUPLICATE_STARTUP_SNAPSHOT_MAIN_FUNCTION

v8.startupSnapshot.setDeserializeMainFunction() could not be called
because it had already been called before.

ERR_ENCODING_INVALID_ENCODED_DATA

Data provided to TextDecoder() API was invalid according to the encoding
provided.

ERR_ENCODING_NOT_SUPPORTED

Encoding provided to TextDecoder() API was not one of the
WHATWG Supported Encodings.

ERR_EVAL_ESM_CANNOT_PRINT

--print cannot be used with ESM input.

ERR_EVENT_RECURSION

Thrown when an attempt is made to recursively dispatch an event on EventTarget.

ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE

The JS execution context is not associated with a Node.js environment.
This may occur when Node.js is used as an embedded library and some hooks
for the JS engine are not set up properly.

ERR_FALSY_VALUE_REJECTION

A Promise that was callbackified via util.callbackify() was rejected with a
falsy value.

ERR_FEATURE_UNAVAILABLE_ON_PLATFORM

Used when a feature that is not available
to the current platform which is running Node.js is used.

ERR_FS_CP_DIR_TO_NON_DIR

An attempt was made to copy a directory to a non-directory (file, symlink,
etc.) using fs.cp().

ERR_FS_CP_EEXIST

An attempt was made to copy over a file that already existed with
fs.cp(), with the force and errorOnExist set to true.

ERR_FS_CP_EINVAL

When using fs.cp(), src or dest pointed to an invalid path.

ERR_HTTP_CONTENT_LENGTH_MISMATCH

Response body size doesn’t match with the specified content-length header value.

ERR_FS_CP_FIFO_PIPE

An attempt was made to copy a named pipe with fs.cp().

ERR_FS_CP_NON_DIR_TO_DIR

An attempt was made to copy a non-directory (file, symlink, etc.) to a directory
using fs.cp().

ERR_FS_CP_SOCKET

An attempt was made to copy to a socket with fs.cp().

ERR_FS_CP_SYMLINK_TO_SUBDIRECTORY

When using fs.cp(), a symlink in dest pointed to a subdirectory
of src.

ERR_FS_CP_UNKNOWN

An attempt was made to copy to an unknown file type with fs.cp().

ERR_FS_EISDIR

Path is a directory.

ERR_FS_FILE_TOO_LARGE

An attempt has been made to read a file whose size is larger than the maximum
allowed size for a Buffer.

ERR_FS_INVALID_SYMLINK_TYPE

An invalid symlink type was passed to the fs.symlink() or
fs.symlinkSync() methods.

ERR_HTTP_HEADERS_SENT

An attempt was made to add more headers after the headers had already been sent.

ERR_HTTP_INVALID_HEADER_VALUE

An invalid HTTP header value was specified.

ERR_HTTP_INVALID_STATUS_CODE

Status code was outside the regular status code range (100-999).

ERR_HTTP_REQUEST_TIMEOUT

The client has not sent the entire request within the allowed time.

ERR_HTTP_SOCKET_ENCODING

Changing the socket encoding is not allowed per RFC 7230 Section 3.

ERR_HTTP_TRAILER_INVALID

The Trailer header was set even though the transfer encoding does not support
that.

ERR_HTTP2_ALTSVC_INVALID_ORIGIN

HTTP/2 ALTSVC frames require a valid origin.

ERR_HTTP2_ALTSVC_LENGTH

HTTP/2 ALTSVC frames are limited to a maximum of 16,382 payload bytes.

ERR_HTTP2_CONNECT_AUTHORITY

For HTTP/2 requests using the CONNECT method, the :authority pseudo-header
is required.

ERR_HTTP2_CONNECT_PATH

For HTTP/2 requests using the CONNECT method, the :path pseudo-header is
forbidden.

ERR_HTTP2_CONNECT_SCHEME

For HTTP/2 requests using the CONNECT method, the :scheme pseudo-header is
forbidden.

ERR_HTTP2_ERROR

A non-specific HTTP/2 error has occurred.

ERR_HTTP2_GOAWAY_SESSION

New HTTP/2 Streams may not be opened after the Http2Session has received a
GOAWAY frame from the connected peer.

ERR_HTTP2_HEADER_SINGLE_VALUE

Multiple values were provided for an HTTP/2 header field that was required to
have only a single value.

ERR_HTTP2_HEADERS_AFTER_RESPOND

An additional headers was specified after an HTTP/2 response was initiated.

ERR_HTTP2_HEADERS_SENT

An attempt was made to send multiple response headers.

ERR_HTTP2_INFO_STATUS_NOT_ALLOWED

Informational HTTP status codes (1xx) may not be set as the response status
code on HTTP/2 responses.

ERR_HTTP2_INVALID_CONNECTION_HEADERS

HTTP/1 connection specific headers are forbidden to be used in HTTP/2
requests and responses.

ERR_HTTP2_INVALID_HEADER_VALUE

An invalid HTTP/2 header value was specified.

ERR_HTTP2_INVALID_INFO_STATUS

An invalid HTTP informational status code has been specified. Informational
status codes must be an integer between 100 and 199 (inclusive).

ERR_HTTP2_INVALID_ORIGIN

HTTP/2 ORIGIN frames require a valid origin.

ERR_HTTP2_INVALID_PACKED_SETTINGS_LENGTH

Input Buffer and Uint8Array instances passed to the
http2.getUnpackedSettings() API must have a length that is a multiple of
six.

ERR_HTTP2_INVALID_PSEUDOHEADER

Only valid HTTP/2 pseudoheaders (:status, :path, :authority, :scheme,
and :method) may be used.

ERR_HTTP2_INVALID_SESSION

An action was performed on an Http2Session object that had already been
destroyed.

ERR_HTTP2_INVALID_SETTING_VALUE

An invalid value has been specified for an HTTP/2 setting.

ERR_HTTP2_INVALID_STREAM

An operation was performed on a stream that had already been destroyed.

ERR_HTTP2_MAX_PENDING_SETTINGS_ACK

Whenever an HTTP/2 SETTINGS frame is sent to a connected peer, the peer is
required to send an acknowledgment that it has received and applied the new
SETTINGS. By default, a maximum number of unacknowledged SETTINGS frames may
be sent at any given time. This error code is used when that limit has been
reached.

ERR_HTTP2_NESTED_PUSH

An attempt was made to initiate a new push stream from within a push stream.
Nested push streams are not permitted.

ERR_HTTP2_NO_MEM

Out of memory when using the http2session.setLocalWindowSize(windowSize) API.

ERR_HTTP2_NO_SOCKET_MANIPULATION

An attempt was made to directly manipulate (read, write, pause, resume, etc.) a
socket attached to an Http2Session.

ERR_HTTP2_ORIGIN_LENGTH

HTTP/2 ORIGIN frames are limited to a length of 16382 bytes.

ERR_HTTP2_OUT_OF_STREAMS

The number of streams created on a single HTTP/2 session reached the maximum
limit.

ERR_HTTP2_PAYLOAD_FORBIDDEN

A message payload was specified for an HTTP response code for which a payload is
forbidden.

ERR_HTTP2_PING_CANCEL

An HTTP/2 ping was canceled.

ERR_HTTP2_PING_LENGTH

HTTP/2 ping payloads must be exactly 8 bytes in length.

ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED

An HTTP/2 pseudo-header has been used inappropriately. Pseudo-headers are header
key names that begin with the : prefix.

ERR_HTTP2_PUSH_DISABLED

An attempt was made to create a push stream, which had been disabled by the
client.

ERR_HTTP2_SEND_FILE

An attempt was made to use the Http2Stream.prototype.responseWithFile() API to
send a directory.

ERR_HTTP2_SEND_FILE_NOSEEK

An attempt was made to use the Http2Stream.prototype.responseWithFile() API to
send something other than a regular file, but offset or length options were
provided.

ERR_HTTP2_SESSION_ERROR

The Http2Session closed with a non-zero error code.

ERR_HTTP2_SETTINGS_CANCEL

The Http2Session settings canceled.

ERR_HTTP2_SOCKET_BOUND

An attempt was made to connect a Http2Session object to a net.Socket or
tls.TLSSocket that had already been bound to another Http2Session object.

ERR_HTTP2_SOCKET_UNBOUND

An attempt was made to use the socket property of an Http2Session that
has already been closed.

ERR_HTTP2_STATUS_101

Use of the 101 Informational status code is forbidden in HTTP/2.

ERR_HTTP2_STATUS_INVALID

An invalid HTTP status code has been specified. Status codes must be an integer
between 100 and 599 (inclusive).

ERR_HTTP2_STREAM_CANCEL

An Http2Stream was destroyed before any data was transmitted to the connected
peer.

ERR_HTTP2_STREAM_ERROR

A non-zero error code was been specified in an RST_STREAM frame.

ERR_HTTP2_STREAM_SELF_DEPENDENCY

When setting the priority for an HTTP/2 stream, the stream may be marked as
a dependency for a parent stream. This error code is used when an attempt is
made to mark a stream and dependent of itself.

ERR_HTTP2_TOO_MANY_INVALID_FRAMES

The limit of acceptable invalid HTTP/2 protocol frames sent by the peer,
as specified through the maxSessionInvalidFrames option, has been exceeded.

ERR_HTTP2_TRAILERS_ALREADY_SENT

Trailing headers have already been sent on the Http2Stream.

ERR_HTTP2_TRAILERS_NOT_READY

The http2stream.sendTrailers() method cannot be called until after the
'wantTrailers' event is emitted on an Http2Stream object. The
'wantTrailers' event will only be emitted if the waitForTrailers option
is set for the Http2Stream.

ERR_HTTP2_UNSUPPORTED_PROTOCOL

http2.connect() was passed a URL that uses any protocol other than http: or
https:.

ERR_ILLEGAL_CONSTRUCTOR

An attempt was made to construct an object using a non-public constructor.

ERR_IMPORT_ASSERTION_TYPE_FAILED

An import assertion has failed, preventing the specified module to be imported.

ERR_IMPORT_ASSERTION_TYPE_MISSING

An import assertion is missing, preventing the specified module to be imported.

ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED

An import assertion is not supported by this version of Node.js.

ERR_INCOMPATIBLE_OPTION_PAIR

An option pair is incompatible with each other and cannot be used at the same
time.

ERR_INPUT_TYPE_NOT_ALLOWED

Stability: 1 — Experimental

The --input-type flag was used to attempt to execute a file. This flag can
only be used with input via --eval, --print, or STDIN.

ERR_INSPECTOR_ALREADY_ACTIVATED

While using the node:inspector module, an attempt was made to activate the
inspector when it already started to listen on a port. Use inspector.close()
before activating it on a different address.

ERR_INSPECTOR_ALREADY_CONNECTED

While using the node:inspector module, an attempt was made to connect when the
inspector was already connected.

ERR_INSPECTOR_CLOSED

While using the node:inspector module, an attempt was made to use the
inspector after the session had already closed.

ERR_INSPECTOR_COMMAND

An error occurred while issuing a command via the node:inspector module.

ERR_INSPECTOR_NOT_ACTIVE

The inspector is not active when inspector.waitForDebugger() is called.

ERR_INSPECTOR_NOT_AVAILABLE

The node:inspector module is not available for use.

ERR_INSPECTOR_NOT_CONNECTED

While using the node:inspector module, an attempt was made to use the
inspector before it was connected.

ERR_INSPECTOR_NOT_WORKER

An API was called on the main thread that can only be used from
the worker thread.

ERR_INTERNAL_ASSERTION

There was a bug in Node.js or incorrect usage of Node.js internals.
To fix the error, open an issue at https://github.com/nodejs/node/issues.

ERR_INVALID_ADDRESS_FAMILY

The provided address family is not understood by the Node.js API.

ERR_INVALID_ARG_TYPE

An argument of the wrong type was passed to a Node.js API.

ERR_INVALID_ARG_VALUE

An invalid or unsupported value was passed for a given argument.

ERR_INVALID_ASYNC_ID

An invalid asyncId or triggerAsyncId was passed using AsyncHooks. An id
less than -1 should never happen.

ERR_INVALID_BUFFER_SIZE

A swap was performed on a Buffer but its size was not compatible with the
operation.

ERR_INVALID_CHAR

Invalid characters were detected in headers.

ERR_INVALID_CURSOR_POS

A cursor on a given stream cannot be moved to a specified row without a
specified column.

ERR_INVALID_FD

A file descriptor (‘fd’) was not valid (e.g. it was a negative value).

ERR_INVALID_FD_TYPE

A file descriptor (‘fd’) type was not valid.

ERR_INVALID_FILE_URL_HOST

A Node.js API that consumes file: URLs (such as certain functions in the
fs module) encountered a file URL with an incompatible host. This
situation can only occur on Unix-like systems where only localhost or an empty
host is supported.

ERR_INVALID_FILE_URL_PATH

A Node.js API that consumes file: URLs (such as certain functions in the
fs module) encountered a file URL with an incompatible path. The exact
semantics for determining whether a path can be used is platform-dependent.

ERR_INVALID_HANDLE_TYPE

An attempt was made to send an unsupported «handle» over an IPC communication
channel to a child process. See subprocess.send() and process.send()
for more information.

ERR_INVALID_HTTP_TOKEN

An invalid HTTP token was supplied.

ERR_INVALID_IP_ADDRESS

An IP address is not valid.

ERR_INVALID_MIME_SYNTAX

The syntax of a MIME is not valid.

ERR_INVALID_MODULE

An attempt was made to load a module that does not exist or was otherwise not
valid.

ERR_INVALID_MODULE_SPECIFIER

The imported module string is an invalid URL, package name, or package subpath
specifier.

ERR_INVALID_OBJECT_DEFINE_PROPERTY

An error occurred while setting an invalid attribute on the property of
an object.

ERR_INVALID_PACKAGE_CONFIG

An invalid package.json file failed parsing.

ERR_INVALID_PACKAGE_TARGET

The package.json "exports" field contains an invalid target mapping
value for the attempted module resolution.

ERR_INVALID_PERFORMANCE_MARK

While using the Performance Timing API (perf_hooks), a performance mark is
invalid.

ERR_INVALID_PROTOCOL

An invalid options.protocol was passed to http.request().

ERR_INVALID_REPL_EVAL_CONFIG

Both breakEvalOnSigint and eval options were set in the REPL config,
which is not supported.

ERR_INVALID_REPL_INPUT

The input may not be used in the REPL. The conditions under which this
error is used are described in the REPL documentation.

ERR_INVALID_RETURN_PROPERTY

Thrown in case a function option does not provide a valid value for one of its
returned object properties on execution.

ERR_INVALID_RETURN_PROPERTY_VALUE

Thrown in case a function option does not provide an expected value
type for one of its returned object properties on execution.

ERR_INVALID_RETURN_VALUE

Thrown in case a function option does not return an expected value
type on execution, such as when a function is expected to return a promise.

ERR_INVALID_STATE

Indicates that an operation cannot be completed due to an invalid state.
For instance, an object may have already been destroyed, or may be
performing another operation.

ERR_INVALID_SYNC_FORK_INPUT

A Buffer, TypedArray, DataView, or string was provided as stdio input to
an asynchronous fork. See the documentation for the child_process module
for more information.

ERR_INVALID_THIS

A Node.js API function was called with an incompatible this value.

const urlSearchParams = new URLSearchParams('foo=bar&baz=new');

const buf = Buffer.alloc(1);
urlSearchParams.has.call(buf, 'foo');
// Throws a TypeError with code 'ERR_INVALID_THIS'

ERR_INVALID_TRANSFER_OBJECT

An invalid transfer object was passed to postMessage().

ERR_INVALID_TUPLE

An element in the iterable provided to the WHATWG
URLSearchParams constructor did not
represent a [name, value] tuple – that is, if an element is not iterable, or
does not consist of exactly two elements.

ERR_INVALID_URI

An invalid URI was passed.

ERR_INVALID_URL

An invalid URL was passed to the WHATWG URL
constructor or the legacy url.parse() to be parsed.
The thrown error object typically has an additional property 'input' that
contains the URL that failed to parse.

ERR_INVALID_URL_SCHEME

An attempt was made to use a URL of an incompatible scheme (protocol) for a
specific purpose. It is only used in the WHATWG URL API support in the
fs module (which only accepts URLs with 'file' scheme), but may be used
in other Node.js APIs as well in the future.

ERR_IPC_CHANNEL_CLOSED

An attempt was made to use an IPC communication channel that was already closed.

ERR_IPC_DISCONNECTED

An attempt was made to disconnect an IPC communication channel that was already
disconnected. See the documentation for the child_process module
for more information.

ERR_IPC_ONE_PIPE

An attempt was made to create a child Node.js process using more than one IPC
communication channel. See the documentation for the child_process module
for more information.

ERR_IPC_SYNC_FORK

An attempt was made to open an IPC communication channel with a synchronously
forked Node.js process. See the documentation for the child_process module
for more information.

ERR_LOADER_CHAIN_INCOMPLETE

An ESM loader hook returned without calling next() and without explicitly
signaling a short circuit.

ERR_MANIFEST_ASSERT_INTEGRITY

An attempt was made to load a resource, but the resource did not match the
integrity defined by the policy manifest. See the documentation for policy
manifests for more information.

ERR_MANIFEST_DEPENDENCY_MISSING

An attempt was made to load a resource, but the resource was not listed as a
dependency from the location that attempted to load it. See the documentation
for policy manifests for more information.

ERR_MANIFEST_INTEGRITY_MISMATCH

An attempt was made to load a policy manifest, but the manifest had multiple
entries for a resource which did not match each other. Update the manifest
entries to match in order to resolve this error. See the documentation for
policy manifests for more information.

ERR_MANIFEST_INVALID_RESOURCE_FIELD

A policy manifest resource had an invalid value for one of its fields. Update
the manifest entry to match in order to resolve this error. See the
documentation for policy manifests for more information.

ERR_MANIFEST_INVALID_SPECIFIER

A policy manifest resource had an invalid value for one of its dependency
mappings. Update the manifest entry to match to resolve this error. See the
documentation for policy manifests for more information.

ERR_MANIFEST_PARSE_POLICY

An attempt was made to load a policy manifest, but the manifest was unable to
be parsed. See the documentation for policy manifests for more information.

ERR_MANIFEST_TDZ

An attempt was made to read from a policy manifest, but the manifest
initialization has not yet taken place. This is likely a bug in Node.js.

ERR_MANIFEST_UNKNOWN_ONERROR

A policy manifest was loaded, but had an unknown value for its «onerror»
behavior. See the documentation for policy manifests for more information.

ERR_MEMORY_ALLOCATION_FAILED

An attempt was made to allocate memory (usually in the C++ layer) but it
failed.

ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE

A message posted to a MessagePort could not be deserialized in the target
vm Context. Not all Node.js objects can be successfully instantiated in
any context at this time, and attempting to transfer them using postMessage()
can fail on the receiving side in that case.

ERR_METHOD_NOT_IMPLEMENTED

A method is required but not implemented.

ERR_MISSING_ARGS

A required argument of a Node.js API was not passed. This is only used for
strict compliance with the API specification (which in some cases may accept
func(undefined) but not func()). In most native Node.js APIs,
func(undefined) and func() are treated identically, and the
ERR_INVALID_ARG_TYPE error code may be used instead.

ERR_MISSING_OPTION

For APIs that accept options objects, some options might be mandatory. This code
is thrown if a required option is missing.

ERR_MISSING_PASSPHRASE

An attempt was made to read an encrypted key without specifying a passphrase.

ERR_MISSING_PLATFORM_FOR_WORKER

The V8 platform used by this instance of Node.js does not support creating
Workers. This is caused by lack of embedder support for Workers. In particular,
this error will not occur with standard builds of Node.js.

ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST

An object that needs to be explicitly listed in the transferList argument
is in the object passed to a postMessage() call, but is not provided
in the transferList for that call. Usually, this is a MessagePort.

In Node.js versions prior to v15.0.0, the error code being used here was
ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST. However, the set of
transferable object types has been expanded to cover more types than
MessagePort.

ERR_MODULE_NOT_FOUND

A module file could not be resolved by the ECMAScript modules loader while
attempting an import operation or when loading the program entry point.

ERR_MULTIPLE_CALLBACK

A callback was called more than once.

A callback is almost always meant to only be called once as the query
can either be fulfilled or rejected but not both at the same time. The latter
would be possible by calling a callback more than once.

ERR_NAPI_CONS_FUNCTION

While using Node-API, a constructor passed was not a function.

ERR_NAPI_INVALID_DATAVIEW_ARGS

While calling napi_create_dataview(), a given offset was outside the bounds
of the dataview or offset + length was larger than a length of given buffer.

ERR_NAPI_INVALID_TYPEDARRAY_ALIGNMENT

While calling napi_create_typedarray(), the provided offset was not a
multiple of the element size.

ERR_NAPI_INVALID_TYPEDARRAY_LENGTH

While calling napi_create_typedarray(), (length * size_of_element) + byte_offset was larger than the length of given buffer.

ERR_NAPI_TSFN_CALL_JS

An error occurred while invoking the JavaScript portion of the thread-safe
function.

ERR_NAPI_TSFN_GET_UNDEFINED

An error occurred while attempting to retrieve the JavaScript undefined
value.

ERR_NAPI_TSFN_START_IDLE_LOOP

On the main thread, values are removed from the queue associated with the
thread-safe function in an idle loop. This error indicates that an error
has occurred when attempting to start the loop.

ERR_NAPI_TSFN_STOP_IDLE_LOOP

Once no more items are left in the queue, the idle loop must be suspended. This
error indicates that the idle loop has failed to stop.

ERR_NOT_BUILDING_SNAPSHOT

An attempt was made to use operations that can only be used when building
V8 startup snapshot even though Node.js isn’t building one.

ERR_NO_CRYPTO

An attempt was made to use crypto features while Node.js was not compiled with
OpenSSL crypto support.

ERR_NO_ICU

An attempt was made to use features that require ICU, but Node.js was not
compiled with ICU support.

ERR_NON_CONTEXT_AWARE_DISABLED

A non-context-aware native addon was loaded in a process that disallows them.

ERR_OUT_OF_RANGE

A given value is out of the accepted range.

ERR_PACKAGE_IMPORT_NOT_DEFINED

The package.json "imports" field does not define the given internal
package specifier mapping.

ERR_PACKAGE_PATH_NOT_EXPORTED

The package.json "exports" field does not export the requested subpath.
Because exports are encapsulated, private internal modules that are not exported
cannot be imported through the package resolution, unless using an absolute URL.

ERR_PARSE_ARGS_INVALID_OPTION_VALUE

When strict set to true, thrown by util.parseArgs() if a {boolean}
value is provided for an option of type {string}, or if a {string}
value is provided for an option of type {boolean}.

ERR_PARSE_ARGS_UNEXPECTED_POSITIONAL

Thrown by util.parseArgs(), when a positional argument is provided and
allowPositionals is set to false.

ERR_PARSE_ARGS_UNKNOWN_OPTION

When strict set to true, thrown by util.parseArgs() if an argument
is not configured in options.

ERR_PERFORMANCE_INVALID_TIMESTAMP

An invalid timestamp value was provided for a performance mark or measure.

ERR_PERFORMANCE_MEASURE_INVALID_OPTIONS

Invalid options were provided for a performance measure.

ERR_PROTO_ACCESS

Accessing Object.prototype.__proto__ has been forbidden using
--disable-proto=throw. Object.getPrototypeOf and
Object.setPrototypeOf should be used to get and set the prototype of an
object.

ERR_REQUIRE_ESM

Stability: 1 — Experimental

An attempt was made to require() an ES Module.

ERR_SCRIPT_EXECUTION_INTERRUPTED

Script execution was interrupted by SIGINT (For
example, Ctrl+C was pressed.)

ERR_SCRIPT_EXECUTION_TIMEOUT

Script execution timed out, possibly due to bugs in the script being executed.

ERR_SERVER_ALREADY_LISTEN

The server.listen() method was called while a net.Server was already
listening. This applies to all instances of net.Server, including HTTP, HTTPS,
and HTTP/2 Server instances.

ERR_SERVER_NOT_RUNNING

The server.close() method was called when a net.Server was not
running. This applies to all instances of net.Server, including HTTP, HTTPS,
and HTTP/2 Server instances.

ERR_SOCKET_ALREADY_BOUND

An attempt was made to bind a socket that has already been bound.

ERR_SOCKET_BAD_BUFFER_SIZE

An invalid (negative) size was passed for either the recvBufferSize or
sendBufferSize options in dgram.createSocket().

ERR_SOCKET_BAD_PORT

An API function expecting a port >= 0 and < 65536 received an invalid value.

ERR_SOCKET_BAD_TYPE

An API function expecting a socket type (udp4 or udp6) received an invalid
value.

ERR_SOCKET_BUFFER_SIZE

While using dgram.createSocket(), the size of the receive or send Buffer
could not be determined.

ERR_SOCKET_CLOSED

An attempt was made to operate on an already closed socket.

ERR_SOCKET_CLOSED_BEFORE_CONNECTION

When calling net.Socket.write() on a connecting socket and the socket was
closed before the connection was established.

ERR_SOCKET_DGRAM_IS_CONNECTED

A dgram.connect() call was made on an already connected socket.

ERR_SOCKET_DGRAM_NOT_CONNECTED

A dgram.disconnect() or dgram.remoteAddress() call was made on a
disconnected socket.

ERR_SOCKET_DGRAM_NOT_RUNNING

A call was made and the UDP subsystem was not running.

ERR_SRI_PARSE

A string was provided for a Subresource Integrity check, but was unable to be
parsed. Check the format of integrity attributes by looking at the
Subresource Integrity specification.

ERR_STREAM_ALREADY_FINISHED

A stream method was called that cannot complete because the stream was
finished.

ERR_STREAM_CANNOT_PIPE

An attempt was made to call stream.pipe() on a Writable stream.

ERR_STREAM_DESTROYED

A stream method was called that cannot complete because the stream was
destroyed using stream.destroy().

ERR_STREAM_NULL_VALUES

An attempt was made to call stream.write() with a null chunk.

ERR_STREAM_PREMATURE_CLOSE

An error returned by stream.finished() and stream.pipeline(), when a stream
or a pipeline ends non gracefully with no explicit error.

ERR_STREAM_PUSH_AFTER_EOF

An attempt was made to call stream.push() after a null(EOF) had been
pushed to the stream.

ERR_STREAM_UNSHIFT_AFTER_END_EVENT

An attempt was made to call stream.unshift() after the 'end' event was
emitted.

ERR_STREAM_WRAP

Prevents an abort if a string decoder was set on the Socket or if the decoder
is in objectMode.

const Socket = require('node:net').Socket;
const instance = new Socket();

instance.setEncoding('utf8');

ERR_STREAM_WRITE_AFTER_END

An attempt was made to call stream.write() after stream.end() has been
called.

ERR_STRING_TOO_LONG

An attempt has been made to create a string longer than the maximum allowed
length.

ERR_SYNTHETIC

An artificial error object used to capture the call stack for diagnostic
reports.

ERR_SYSTEM_ERROR

An unspecified or non-specific system error has occurred within the Node.js
process. The error object will have an err.info object property with
additional details.

ERR_TAP_LEXER_ERROR

An error representing a failing lexer state.

ERR_TAP_PARSER_ERROR

An error representing a failing parser state. Additional information about
the token causing the error is available via the cause property.

ERR_TAP_VALIDATION_ERROR

This error represents a failed TAP validation.

ERR_TEST_FAILURE

This error represents a failed test. Additional information about the failure
is available via the cause property. The failureType property specifies
what the test was doing when the failure occurred.

ERR_TLS_CERT_ALTNAME_FORMAT

This error is thrown by checkServerIdentity if a user-supplied
subjectaltname property violates encoding rules. Certificate objects produced
by Node.js itself always comply with encoding rules and will never cause
this error.

ERR_TLS_CERT_ALTNAME_INVALID

While using TLS, the host name/IP of the peer did not match any of the
subjectAltNames in its certificate.

ERR_TLS_DH_PARAM_SIZE

While using TLS, the parameter offered for the Diffie-Hellman (DH)
key-agreement protocol is too small. By default, the key length must be greater
than or equal to 1024 bits to avoid vulnerabilities, even though it is strongly
recommended to use 2048 bits or larger for stronger security.

ERR_TLS_HANDSHAKE_TIMEOUT

A TLS/SSL handshake timed out. In this case, the server must also abort the
connection.

ERR_TLS_INVALID_CONTEXT

The context must be a SecureContext.

ERR_TLS_INVALID_PROTOCOL_METHOD

The specified secureProtocol method is invalid. It is either unknown, or
disabled because it is insecure.

ERR_TLS_INVALID_PROTOCOL_VERSION

Valid TLS protocol versions are 'TLSv1', 'TLSv1.1', or 'TLSv1.2'.

ERR_TLS_INVALID_STATE

The TLS socket must be connected and securely established. Ensure the ‘secure’
event is emitted before continuing.

ERR_TLS_PROTOCOL_VERSION_CONFLICT

Attempting to set a TLS protocol minVersion or maxVersion conflicts with an
attempt to set the secureProtocol explicitly. Use one mechanism or the other.

ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED

Failed to set PSK identity hint. Hint may be too long.

ERR_TLS_RENEGOTIATION_DISABLED

An attempt was made to renegotiate TLS on a socket instance with renegotiation
disabled.

ERR_TLS_REQUIRED_SERVER_NAME

While using TLS, the server.addContext() method was called without providing
a host name in the first parameter.

ERR_TLS_SESSION_ATTACK

An excessive amount of TLS renegotiations is detected, which is a potential
vector for denial-of-service attacks.

ERR_TLS_SNI_FROM_SERVER

An attempt was made to issue Server Name Indication from a TLS server-side
socket, which is only valid from a client.

ERR_TRACE_EVENTS_CATEGORY_REQUIRED

The trace_events.createTracing() method requires at least one trace event
category.

ERR_TRACE_EVENTS_UNAVAILABLE

The node:trace_events module could not be loaded because Node.js was compiled
with the --without-v8-platform flag.

ERR_TRANSFORM_ALREADY_TRANSFORMING

A Transform stream finished while it was still transforming.

ERR_TRANSFORM_WITH_LENGTH_0

A Transform stream finished with data still in the write buffer.

ERR_TTY_INIT_FAILED

The initialization of a TTY failed due to a system error.

ERR_UNAVAILABLE_DURING_EXIT

Function was called within a process.on('exit') handler that shouldn’t be
called within process.on('exit') handler.

ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET

process.setUncaughtExceptionCaptureCallback() was called twice,
without first resetting the callback to null.

This error is designed to prevent accidentally overwriting a callback registered
from another module.

ERR_UNESCAPED_CHARACTERS

A string that contained unescaped characters was received.

ERR_UNHANDLED_ERROR

An unhandled error occurred (for instance, when an 'error' event is emitted
by an EventEmitter but an 'error' handler is not registered).

ERR_UNKNOWN_BUILTIN_MODULE

Used to identify a specific kind of internal Node.js error that should not
typically be triggered by user code. Instances of this error point to an
internal bug within the Node.js binary itself.

ERR_UNKNOWN_CREDENTIAL

A Unix group or user identifier that does not exist was passed.

ERR_UNKNOWN_ENCODING

An invalid or unknown encoding option was passed to an API.

ERR_UNKNOWN_FILE_EXTENSION

Stability: 1 — Experimental

An attempt was made to load a module with an unknown or unsupported file
extension.

ERR_UNKNOWN_MODULE_FORMAT

Stability: 1 — Experimental

An attempt was made to load a module with an unknown or unsupported format.

ERR_UNKNOWN_SIGNAL

An invalid or unknown process signal was passed to an API expecting a valid
signal (such as subprocess.kill()).

ERR_UNSUPPORTED_DIR_IMPORT

import a directory URL is unsupported. Instead,
self-reference a package using its name and define a custom subpath in
the "exports" field of the package.json file.

import './'; // unsupported
import './index.js'; // supported
import 'package-name'; // supported

ERR_UNSUPPORTED_ESM_URL_SCHEME

import with URL schemes other than file and data is unsupported.

ERR_USE_AFTER_CLOSE

Stability: 1 — Experimental

An attempt was made to use something that was already closed.

ERR_VALID_PERFORMANCE_ENTRY_TYPE

While using the Performance Timing API (perf_hooks), no valid performance
entry types are found.

ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING

A dynamic import callback was not specified.

ERR_VM_MODULE_ALREADY_LINKED

The module attempted to be linked is not eligible for linking, because of one of
the following reasons:

  • It has already been linked (linkingStatus is 'linked')
  • It is being linked (linkingStatus is 'linking')
  • Linking has failed for this module (linkingStatus is 'errored')

ERR_VM_MODULE_CACHED_DATA_REJECTED

The cachedData option passed to a module constructor is invalid.

ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA

Cached data cannot be created for modules which have already been evaluated.

ERR_VM_MODULE_DIFFERENT_CONTEXT

The module being returned from the linker function is from a different context
than the parent module. Linked modules must share the same context.

ERR_VM_MODULE_LINK_FAILURE

The module was unable to be linked due to a failure.

ERR_VM_MODULE_NOT_MODULE

The fulfilled value of a linking promise is not a vm.Module object.

ERR_VM_MODULE_STATUS

The current module’s status does not allow for this operation. The specific
meaning of the error depends on the specific function.

ERR_WASI_ALREADY_STARTED

The WASI instance has already started.

ERR_WASI_NOT_STARTED

The WASI instance has not been started.

ERR_WEBASSEMBLY_RESPONSE

The Response that has been passed to WebAssembly.compileStreaming or to
WebAssembly.instantiateStreaming is not a valid WebAssembly response.

ERR_WORKER_INIT_FAILED

The Worker initialization failed.

ERR_WORKER_INVALID_EXEC_ARGV

The execArgv option passed to the Worker constructor contains
invalid flags.

ERR_WORKER_NOT_RUNNING

An operation failed because the Worker instance is not currently running.

ERR_WORKER_OUT_OF_MEMORY

The Worker instance terminated because it reached its memory limit.

ERR_WORKER_PATH

The path for the main script of a worker is neither an absolute path
nor a relative path starting with ./ or ../.

ERR_WORKER_UNSERIALIZABLE_ERROR

All attempts at serializing an uncaught exception from a worker thread failed.

ERR_WORKER_UNSUPPORTED_OPERATION

The requested functionality is not supported in worker threads.

ERR_ZLIB_INITIALIZATION_FAILED

Creation of a zlib object failed due to incorrect configuration.

HPE_HEADER_OVERFLOW

Too much HTTP header data was received. In order to protect against malicious or
malconfigured clients, if more than 8 KiB of HTTP header data is received then
HTTP parsing will abort without a request or response object being created, and
an Error with this code will be emitted.

HPE_UNEXPECTED_CONTENT_LENGTH

Server is sending both a Content-Length header and Transfer-Encoding: chunked.

Transfer-Encoding: chunked allows the server to maintain an HTTP persistent
connection for dynamically generated content.
In this case, the Content-Length HTTP header cannot be used.

Use Content-Length or Transfer-Encoding: chunked.

MODULE_NOT_FOUND

A module file could not be resolved by the CommonJS modules loader while
attempting a require() operation or when loading the program entry point.

Legacy Node.js error codes

Stability: 0 — Deprecated. These error codes are either inconsistent, or have
been removed.

ERR_CANNOT_TRANSFER_OBJECT

The value passed to postMessage() contained an object that is not supported
for transferring.

ERR_CRYPTO_HASH_DIGEST_NO_UTF16

The UTF-16 encoding was used with hash.digest(). While the
hash.digest() method does allow an encoding argument to be passed in,
causing the method to return a string rather than a Buffer, the UTF-16
encoding (e.g. ucs or utf16le) is not supported.

ERR_HTTP2_FRAME_ERROR

Used when a failure occurs sending an individual frame on the HTTP/2
session.

ERR_HTTP2_HEADERS_OBJECT

Used when an HTTP/2 Headers Object is expected.

ERR_HTTP2_HEADER_REQUIRED

Used when a required header is missing in an HTTP/2 message.

ERR_HTTP2_INFO_HEADERS_AFTER_RESPOND

HTTP/2 informational headers must only be sent prior to calling the
Http2Stream.prototype.respond() method.

ERR_HTTP2_STREAM_CLOSED

Used when an action has been performed on an HTTP/2 Stream that has already
been closed.

ERR_HTTP_INVALID_CHAR

Used when an invalid character is found in an HTTP response status message
(reason phrase).

ERR_INDEX_OUT_OF_RANGE

A given index was out of the accepted range (e.g. negative offsets).

ERR_INVALID_OPT_VALUE

An invalid or unexpected value was passed in an options object.

ERR_INVALID_OPT_VALUE_ENCODING

An invalid or unknown file encoding was passed.

ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST

This error code was replaced by ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST
in Node.js v15.0.0, because it is no longer accurate as other types of
transferable objects also exist now.

ERR_NAPI_CONS_PROTOTYPE_OBJECT

Used by the Node-API when Constructor.prototype is not an object.

ERR_NETWORK_IMPORT_BAD_RESPONSE

Stability: 1 — Experimental

Response was received but was invalid when importing a module over the network.

ERR_NETWORK_IMPORT_DISALLOWED

Stability: 1 — Experimental

A network module attempted to load another module that it is not allowed to
load. Likely this restriction is for security reasons.

ERR_NO_LONGER_SUPPORTED

A Node.js API was called in an unsupported manner, such as
Buffer.write(string, encoding, offset[, length]).

ERR_OPERATION_FAILED

An operation failed. This is typically used to signal the general failure
of an asynchronous operation.

ERR_OUTOFMEMORY

Used generically to identify that an operation caused an out of memory
condition.

ERR_PARSE_HISTORY_DATA

The node:repl module was unable to parse data from the REPL history file.

ERR_SOCKET_CANNOT_SEND

Data could not be sent on a socket.

ERR_STDERR_CLOSE

An attempt was made to close the process.stderr stream. By design, Node.js
does not allow stdout or stderr streams to be closed by user code.

ERR_STDOUT_CLOSE

An attempt was made to close the process.stdout stream. By design, Node.js
does not allow stdout or stderr streams to be closed by user code.

ERR_STREAM_READ_NOT_IMPLEMENTED

Used when an attempt is made to use a readable stream that has not implemented
readable._read().

ERR_TLS_RENEGOTIATION_FAILED

Used when a TLS renegotiation request has failed in a non-specific way.

ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER

A SharedArrayBuffer whose memory is not managed by the JavaScript engine
or by Node.js was encountered during serialization. Such a SharedArrayBuffer
cannot be serialized.

This can only happen when native addons create SharedArrayBuffers in
«externalized» mode, or put existing SharedArrayBuffer into externalized mode.

ERR_UNKNOWN_STDIN_TYPE

An attempt was made to launch a Node.js process with an unknown stdin file
type. This error is usually an indication of a bug within Node.js itself,
although it is possible for user code to trigger it.

ERR_UNKNOWN_STREAM_TYPE

An attempt was made to launch a Node.js process with an unknown stdout or
stderr file type. This error is usually an indication of a bug within Node.js
itself, although it is possible for user code to trigger it.

ERR_V8BREAKITERATOR

The V8 BreakIterator API was used but the full ICU data set is not installed.

ERR_VALUE_OUT_OF_RANGE

Used when a given value is out of the accepted range.

ERR_VM_MODULE_NOT_LINKED

The module must be successfully linked before instantiation.

ERR_VM_MODULE_LINKING_ERRORED

The linker function returned a module for which linking has failed.

ERR_WORKER_UNSUPPORTED_EXTENSION

The pathname used for the main script of a worker has an
unknown file extension.

ERR_ZLIB_BINDING_CLOSED

Used when an attempt is made to use a zlib object after it has already been
closed.

ERR_CPU_USAGE

The native call from process.cpuUsage could not be processed.

Node.js is a JavaScript extension used for server-side scripting. Error handling is a mandatory step in application development. A Node.js developer may work with both synchronous and asynchronous functions simultaneously. Handling errors in asynchronous functions is important because their behavior may vary, unlike synchronous functions. While try-catch blocks are effective for synchronous functions, asynchronous functions can be dealt with callbacks, promises, and async-await. Try-catch is synchronous means that if an asynchronous function throws an error in a synchronous try/catch block, no error throws. Errors thrown in Node.js applications can be handled in the following ways:

  1. Using try-catch block
  2. Using callbacks
  3. Using promises and promise callbacks
  4. Using async-await

Using try-catch block: The try-catch block can be used to handle errors thrown by a block of code.

function dosomething(){

    throw new Error(

    'a error is thrown from dosomething');

}

function init(){

    try{

        dosomething();

    }

    catch(e){

        console.log(e);

    }

 console.log(

    "After successful error handling");

}

init();

Output:

Error: a error is thrown from dosomething
    at dosomething (/home/cg/root/6422736/main.js:4:11)
    at init (/home/cg/root/6422736/main.js:9:9)
    at Object. (/home/cg/root/6422736/main.js:17:1)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
After successful error handling

Explanation: The init() function is called which in turn calls dosomething() function which throws an error object. This error object is caught by the catch block of the init() method. If there is no proper handling of the error the program will terminate. The catch block prints the call stack to show the point where the error occurred.

Using callbacks: A callback is a function called at the completion of a certain task. Callbacks are widely used in Node.js as it prevents any blocking, and allows other code to be run in the meantime. The program does not wait for file reading to complete and proceeds to print “Program Ended” while continuing to read the file. If any error occurs like file does not exist in the system then the error is printed after “Program Ended”, else the content of the file is outputted.

var fs = require("fs");

fs.readFile('foo.txt', function (err, data) {

   if (err) {

       console.error(err);

}else{

   console.log(data.toString());

}

});

console.log("Program Ended");

Output:

Program Ended
[Error: ENOENT: no such file or directory, 
  open 'C:UsersUserDesktopfoo.txt'] {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'C:\Users\User\Desktop\foo.txt'
}

Explanation: In this case, the file does not exist in the system hence the error is thrown.

Using promises and promise callbacks: Promises are an enhancement to Node.js callbacks. When defining the callback, the value which is returned is called a “promise”. The key difference between a promise and a callback is the return value. There is no concept of a return value in callbacks. The return value provides more control for defining the callback function. In order to use promises, the promise module must be installed and imported in the application. The .then clause handles the output of the promise. If an error occurs in any .then clause or if any of the promises above rejects, it is passed to the immediate .catch clause. In case of a promise being rejected, and there is no error handler then the program terminates.

var Promise = require('promise');

var MongoClient = require('mongodb').MongoClient;

MongoClient.connect(url)

    .then(function(err, db) {

        db.collection('Test').updateOne({

            "Name": "Joe"

        }, {

            $set: {

                "Name": "Beck"

            }

        });

    });

   .catch(error => alert(error.message)) 

Output:

// In this case we assume the url is wrong
MongoError: failed to connect to server [localhost:27017]
// error message may vary

Using async-await: Async-await is a special syntax to work with promises in a simpler way that is easy to understand. When we use async/await, .then is replaced by await which handles the waiting in the function. The error handling is done by the .catch clause. Async-await can also be wrapped in a try-catch block for error handling. In case no error handler exists the program terminates due to uncaught error.

async function f() {

}

f().catch(alert);

Output:

// the url is wrong //
TypeError: failed to fetch 

No one is perfect in this world including machines. None of our days pass without having errors faced in our professional life. Whenever we are facing any issues/errors while working rather than worrying, let us all fix our mind like we are going to learn something new. This will make your tasks easier.

One of our friend Error Handling will help us in fighting with these errors. These reduce our pressure by finding the errors and guide us to achieve the desired output.

It might be a tiresome task in Node.js but don’t worry here are a few best practices about error handling in Node.js learn it and master in error handling methods.

Let’s walk through into the article to learn more about it.

  1. What is Error Handling in Node.js?

  2. Types of Errors

  3. What are error objects?

  4. Best Practices to Handle Errors in Node.js

1. What is Error Handling in Node.js?

While talking about errors first know what errors are and then we can learn about error handling and the best practices in it.

Error is nothing but an abnormal working of the program which causes an unexpected or incorrect result. These errors are detected only when you compile or execute a program.

Some people might detect and fix the bugs easily while others might find it difficult to handle the errors and some might totally miss all the errors. Handling errors helps you in reducing the development time by detecting the bugs and resolving them quickly.

Many node.js developers find error handling a difficult process and they keep asking themselves “Is Node.js bad at handling errors?

Here is my answer, Yes it is hard for any beginner to handle errors while it might become easier upon practicing it often.

Due to the asynchronous nature of node.js detecting and handling errors might be a pain point for developers.

Take a look at my article and see what are the different ways to handle errors in Node.js.

2. Types of Errors

There are two main types of errors in node.js. Errors can be operational or programmer. See what they are and how they occur in your code.

i) Programmer Errors

Programming errors also known as bugs or faults which represent unexpected issues in a poorly written code. It means the developer who built the code made mistakes and should be fixed as soon as possible since it might cause poor end-user experience.

Some of the basic instances that cause programming errors.

  • Asynchronous function without a callback.
  • Passing an object where a string is required.
  • Reading an undefined property.
  • Passing incorrect parameters to a function
  • Did not resolve a promise

ii) Operational Errors

These are run-time errors which usually occur when there is a problem with your system, system configurations, the network or the remote services.  These errors should be fixed in a proper way.

Some of the operational errors include:

  • Request timeout
  • Invalid user input
  • Socket hang up
  • Out of memory
  • Failed to connect DB server

These are the different types of errors which you will encounter while building your application with node.js.

The major reason behind dividing errors into these two categories is

  1. You might think of restarting your app if there is an error user not found error? Definitely not since other users might enjoy using your application which is an example of operational error.
  2. You must restart your app when you fail to catch an rejected promise since this bug might threaten your app which is an example of programmer error.

3. What are error objects?

Error Object is either the instance of the object or extends the Error class which is a built-in object in the node.js runtime. It gives a set of error information when used properly.

Example:

const error = new Error("Error message");
console.log(error);
console.log(error.stack);

Output:

{ stack: [Getter/Setter],
  arguments: undefined,
  type: undefined,
  message: 'Error message' }
Error: The error message
    at Object.<anonymous> (/home/nico/example.js:1:75)
    at Module._compile (module.js:407:26)
    at Object..js (module.js:413:10)
    at Module.load (module.js:339:31)
    at Function._load (module.js:298:12)
    at Array.0 (module.js:426:10)
    at EventEmitter._tickCallback (node.js:126:26)

The above code shows the complete stack trace of an error and also you can view the functions that were called before the error occurred.

You can also add more properties if you want to know some more information about the Error object.

const error = new Error("Error message");
error.status_code = 404;
console.log(error);

4. Best practices to Handle Errors in Node.js

As I said before, error handling in node.js is a troublesome task. It would take much time to achieve this stage.

Before entering into how to handle errors in node.js in a best way you should know how Node.js architecture, frameworks and libraries work with all the developer practices.

Do not repeat your mistakes rather handle them with utmost care to resolve them faster. Here are some of the best ways to handle all your errors in your application.

i.) Handling asynchronous errors

It is quite tricky to handle errors in an asynchronous code if you’re not quite familiar with it. There are three ways you can handle asynchronous operations.

a.) Using Promises

Promises are the new and improved way of writing asynchronous code which can be used to replace callback methods.

Use .catch() while handling errors using promises.

Usage:

doSomething1()
  .then(doSomething2)
  .then(doSomething3)
  .catch(err => console.error(err))

Example:

function myAsyncFunction() {
   return new Promise((resolve, reject) => {
      setTimeout(() => {
        reject(new Error('oops'))
      }, 1000);
   })
}

myAsyncFunction()
  .then(() => {
      // happy path
  })
  .catch((err) => {
      // handle error
  })

b.) Using Callbacks

Callbacks were the most basic and oldest way of delivering the asynchronous errors.

The callback function can be passed as the parameter to the calling function, which you later invoke when the asynchronous function completes executing.

Usage:

callback(err, result)

Example:

function myAsyncFunction(callback) {
  setTimeout(() => {
    callback(new Error('oops'))
  }, 1000);
}

myAsyncFunction((err) => {
  if (err) {
    // handle error
  } else {
    // happy path 
  }
})

c.) Using async-wait

To catch errors using async-wait method you can do it this way:

function myAsyncFunction() {
   return new Promise((resolve, reject) => {
      setTimeout(() => {
        reject(new Error('oops'))
      }, 1000);
   });
}

(async() => {
  try {
    await myAsyncFunction();
    // happy path
  } catch (err) {
    // handle error
  }
})();

ii.) Use a Middleware

You can configure a centralized error handling method when you have a set of custom errors. To catch all the errors you can use middleware and from there you can decide whether to log all the errors or you need to get notified whenever an error occurs.  

To forward the errors to the error handler middleware use the next() function.

Usage:

app.post('/user', async (req, res, next) => {
  try {
    const newUser = User.create(req.body)
  } catch (error) {
    next(error)
  }
})

iii.) Catch Uncaught Exceptions

Your application will crash if an uncaught exception is thrown during the execution of your code.

Even after handling the most crucial errors in your app you might end up missing a few errors which could lead to an uncaught exception.

Listen to the event uncaughtException emitted by the process object and this might also cause unexpected effects in your application. But if you are listening to these events you should follow the below steps.

  • Log the error information so that you can take a look at it later.
  • Exit your application forcefully to launch a replacement process.

It is an example on bad way of writing the uncaught exception code.

process.on('uncaughtException', (err) => {
  logger.fatal('an uncaught exception detected', err);
});
  
process.on('unhandledRejection', (err) => {
  logger.fatal('an unhandled rejection detected', err)
});

This is an example of good way of writing it.

process.on('uncaughtException', (err) => {
  logger.fatal('an uncaught exception detected', err);
  process.exit(-1);
});
  
process.on('unhandledRejection', (err) => {
  logger.fatal('an unhandled rejection detected', err);
  process.exit(-1);
});

iv.) Catch Unhandled Promise Rejections

When a promise is rejected it always looks for a rejection handler,if it finds one it calls the function with the error.

Handle them properly by using fallback.

process.on(“unhandledRejection” , callback)

Example:

...
const user = User.getUserById(req.params.id)
 .then(user => user)
 // missing a .catch() block
...

// if the Promise is rejected this will catch it
process.on('unhandledRejection', error => { 
  throw error
})

process.on('uncaughtException', error => {
 logError(error)

 if (!isOperationalError(error)) {
   process.exit(1)
 }
})

v.) Use the appropriate log levels for errors and error alerting

You should not only choose the best logging library to log your errors instead you should also know how well you can use it to log all the error information that you catch.

You can also log all the error messages at different log levels which can then be sent to different destinations such as stdout, syslog, files etc.

You should also opt the perfect log levels for your message based on the priority of log messages.

Here are some of the basic log levels which could be used often.

  • log.info — If informative messages occur frequently it could become a noise. These messages are used for reporting significant successful actions.
  • log.error — All critical error messages which require instant attention and could possibly cause any dire consequences.
  • log.warn —  This warning message occurs when something unusual happens which is not critical but it could be useful if we review it and resolve the error.
  • log.debug — These messages are not very crucial but could be useful while debugging.

Manage your errors in Node.js with Atatus

We at Atatus, offer you a comprehensive view on the errors that occur in your application. You can also log all the errors that rise in your application which can be used later.

Atatus captures all the errors and provides you detailed insights on error messages with a complete stack trace.

Along with Error tracking, Atatus provides features such as:

  • Log Monitoring
  • Real User Monitoring
  • Application Performance Monitoring
  • Infrastructure Monitoring

Sign up with Atatus and get a 14-day free trial.

Final Words

Take the right approach before handling errors in your application. It is bit tricky handling the errors in Node.js if you do not know the source of the errors. Since node.js is an emerging technology you should know in-depth knowledge on how it works, the frameworks in it and the possible ways to handle the errors that your application might throw.

Make your application robust to impress your users by building a proper error-handling system. Also monitor your errors frequently with any monitoring tools to fix the issues at a faster rate.

Share your thoughts with us in the below comment section!!!

Понравилась статья? Поделить с друзьями:
  • Error handling file saving did the server never start
  • Error handling discord py
  • Error handler threw an exception
  • Error handler stm32
  • Error handler stardew valley