@zerodep/to.errors v0.1.5
@zerodep/errors
All @zerodep/to packages throw ZeroDepErrorTo
errors. This error extends the ZeroDepError.
The ZeroDepErrorTo
is further subclassed with:
ZeroDepErrorToType
- for invalid typesZeroDepErrorToRange
- for values that are outside of specific ranges
Table of Contents
Tos & Errors
We have an opinion about when appropriately throwing errors.
The spirit/intention of tos is to protect functions from incorrect runtime values that would otherwise cause defects in the software.
All tos will throw a ZeroDepErrorTo
(or instance thereof) error when they encounter an invalid value.
Extending Errors
The ZeroDepErrorTo
object includes the standard Error
fields (message, stack and name) as wel as:
- category of the error (
type
,range
,reference
,syntax
,uri
, orunknown
) - source of the error within the @zerodep ecosystem (a to, network, storage, etc)
- value of the problem (the string|number|array|object that caused the issue, if there is one. We have found this incredibly useful in Promise.all() and similar situations)
Why category
, you ask? The word type
would be semantically most appropriate, followed closely by class
. Typescript already has a type
property on its errors. class
is a reserved word. Some editors treat type
in a special manner. We want to avoid collisions with other libraries.
We add the above fields to each of our errors, while ensuring the message
, stack
and name
values of the Error
work as expected to ensure any existing code works as expected. We also ALWAYS add a message
, which should simplify error logging.
Install
This package is available from three differently sized and tree shakeable, npm packages:
// entire zerodep utils suite
npm install @zerodep/utils
// all @zerodep/to utilities
npm install @zerodep/to
// all @zerodep/to errors
npm install @zerodep/to.errors
Of course, you may use yarn
, pnpm
, or the package manager of your choice. Only npm
examples are shown for brevity.
For completeness, links to the @zerodep repositories with this function:
How to Use
To Error
import { ZeroDepErrorTo } from '@@zerodep/utils';
// or
import { ZeroDepErrorTo } from '@@zerodep/to';
// or
import { ZeroDepErrorTo } from '@@zerodep/to.errors';
// all arguments are optional
const error = new ZeroDepErrorToType();
error.value = 42;
// properties may be set after instantiation
console.log(error.message); // "Value is invalid"
console.log(error.category); // "unknown"
console.log(error.source); // "to"
console.log(error.value); // 42
To Type Error
import { ZeroDepErrorToType } from '@@zerodep/utils';
// or
import { ZeroDepErrorToType } from '@@zerodep/to';
// or
import { ZeroDepErrorToType } from '@@zerodep/to.errors';
// properties may be set after instantiation
const error = new ZeroDepErrorToType();
error.value = '42';
// error properties
console.log(error.message); // "Value is incorrect type"
console.log(error.category); // "type"
console.log(error.source); // "to"
console.log(error.value); // "42"
To Range Error
import { ZeroDepErrorToRange } from '@@zerodep/utils';
// or
import { ZeroDepErrorToRange } from '@@zerodep/to';
// or
import { ZeroDepErrorToRange } from '@@zerodep/to.errors';
// properties may be set after instantiation
const error = new ZeroDepErrorGuardType();
error.value = { answer: 42 };
// error properties
console.log(error.message); // "Value is out-of-range"
console.log(error.category); // "range"
console.log(error.source); // "guard"
console.log(error.value); // { answer: 42 }
More information about error configuration can be found in the @zerodep/errors documentation.
Advantages of @zerodep Packages
We help make source code more readable, more secure, faster to craft, less likely to have hidden defects, and easier to maintain.
- Zero npm dependencies - completely eliminates all risk of supply-chain attacks, decreases
node_modules
folder size - Fully typed - typescript definitions are provided for every package for a better developer experience
- Semantically named - package and method names are easy to grok, remember, use, and read
- Documented - actually useful documentation with examples and helpful tips
- Intelligently Packaged - multiple npm packages of different sizes available allowing a menu or a-la-carte composition of capabilities
- 100% Tested - all methods and packages are fully unit tested
- ESM & CJS - has both ecmascript modules and common javascript exports, both are fully tree-shakable
- FP Inspired - gently opinionated to encourage functional programming style for cleaner and more maintainable software
- Predictably Versioned - semantically versioned for peace-of-mind upgrading, this includes changelogs
- MIT Licensed - permissively licensed for maximum usability
Support
All @zerodep packages are built for the ES2020 specification. Should you need to support older environments you will need to add appropriate polyfills. All packages are tested on the following platforms/browsers:
Browsers
- Chrome - last 2 major versions
- Firefox - last 2 major versions
- Safari - last 2 major versions
- Edge - last 2 major versions
- Android - last 2 major versions
- iOS - last 2 major versions
Node
- v16.x - Gallium LTS
- v14.x - Fermium LTS
It is likely the package will work on other technologies and version, however development and testing effort is only spent on the above.
Semver
All @zerodep packages, including this one, adhere to Semantic Versioning practices:
- major versions: correlates with breaking changes to one or more method signatures
- minor versions: includes addition of new functionality or backwards-compatible software improvements
- patch versions: are reserved for copy changes, documentation enhancements and bug fixes
The above said, a security best practice is to pin your software packages to specific versions and only upgrade to more recent releases after careful inspection of any CHANGELOG, release notes and all software changes.