1.0.1 • Published 11 months ago

@origin-yaropolk/debug-assert v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

Small lib that helps your app meet fail-fast and fail-save principles.

Usage

import { assert } from 'debug-assert';

assert(myCondition, 'Print this if failed');

You can setup your own fail-fast function:

import { assert, setupFailFast } from 'debug-assert';

setupFailFast((err: Error) => {
  console.error(err);
  collectSomeData();
  process.abort();
});

assert(myCondition, 'Print this if failed');

Change the principle by calling setupMode function:

import { assert, setupMode } from 'debug-assert';

// calls fail-fast function
// it's default behaviour
assert(myCondition, 'Print this if failed');

setupMode('fail-safe');
// throws an exception
assert(myCondition, 'Print this if failed');

setupMode('ignore');
// replaced with noop
assert(myCondition, 'Print this if failed');

There is an assertNotNull alias:

import { assertNotNull } from 'debug-assert';

assertNotNull(null, 'Print this if failed'); // will fail
assertNotNull(undefined, 'Print this if failed'); // not fail
assertNotNull(0, 'Print this if failed'); // not fail
1.0.1

11 months ago

1.0.0

11 months ago