From a56d7108ad78deb9af02222dec3117353a2e98e8 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Mon, 4 Nov 2019 14:59:17 +0100 Subject: [PATCH] Update CONTRIBUTING_JS.md add section on error codes Ref. https://github.com/ipfs/js-ipfs/pull/2547#discussion_r336466026 --- CONTRIBUTING_JS.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/CONTRIBUTING_JS.md b/CONTRIBUTING_JS.md index 63d96e0d..17a7b487 100644 --- a/CONTRIBUTING_JS.md +++ b/CONTRIBUTING_JS.md @@ -19,6 +19,7 @@ Our toolkit for each of these is not set in stone, and we don't plan to halt our - [Guidelines](#guidelines) - [Supported versions](#supported-versions) - [Linting & Code Style](#linting--code-style) + - [Error Codes](#error-codes) - [Dependency Versions](#dependency-versions) - [Testing](#testing) - [Releasing](#releasing) @@ -100,6 +101,25 @@ However, we've added an extra linting rule: Enforce the use of [strict mode](htt Using [aegir-lint](#aegir) will help you do this easily; it automatically lints your code. +#### Error Codes + +When introducing a new error code that may be useful outside of the current scope, make sure it is defined in a named variable and can be exported and used in other places. This enables others to reuse those definitions and decreases number of hardcoded values across our codebases. For example: + +```js +const { Errors } = require('interface-datastore') + +// throw predefined errors if possible +if (!value) { + throw Errors.notFoundError() +} + +// compare value from +const ERR_NOT_FOUND = Errors.notFoundError().code +if (err.code === ERR_NOT_FOUND) { + // handle +} +``` + #### Dependency Versions Our rule is: Use ~ for everything below 1.0.0 and ^ for everything above 1.0.0. If you find a package.json that is not following this rule, please submit a PR.