3.0.2 • Published 29 days ago

@jakubmazanec/error v3.0.2

Weekly downloads
-
License
LGPL-3.0-only
Repository
github
Last release
29 days ago

@jakubmazanec/error

Collection of utilities for creating and handling errors.

Installation

npm install @jakubmazanec/error

⚠️ This is an ESM package! It cannot be required from a CommonJS module.

Prerequisites

  • Node.js 20 or later
  • TypeScript 5 or later

Usage

Create custom error classes to distinguish between errors and to ensure consistent error messages:

import {createCustomError} from '@jakubmazanec/error';

let FoobarError = createCustomError('FoobarError', {
  FOOBAR_NOT_FOUND: 'Foobar was not found!',
});

try {
  throw new FoobarError('FOOBAR_NOT_FOUND');
} catch (error: unknown) {
  console.log(error instanceof FoobarError); // -> true
  console.log(error.message); // -> 'Foobar was not found!'
  console.log(error.code); // -> 'FOOBAR_NOT_FOUND'
}

You can also have arbitrary data attached:

import {createCustomErrorWithData} from '@jakubmazanec/error';
import {z} from 'zod';

let FoobarError = createCustomErrorWithData(
  'FoobarError',
  {FOOBAR_FAILED: 'Foobar failed with exit code "{0}" and message "{1}"!'},
  z.object({
    cwd: z.string(),
  })
);

function runFoobar() {
  let foobarOptions = {
    cwd: '/foobar',
  };

  try {
    foobar(foobarOptions);
  } catch (error: unknown) {
    if (error instanceof Error) {
      throw new FoobarError('FOOBAR_FAILED', {
        messageParameters: [42, 'Oops :('],
        data: foobarOptions,
        cause: error,
      });
    }
  }
}

try {
  runFoobar();
} catch (error: unknown) {
  console.log(error instanceof FoobarError); // -> true
  console.log(error.message); // -> 'Foobar failed with exit code "42" and message "Oops :("!'
  console.log(error.code); // -> 'FOOBAR_FAILED'
  console.log(error.data); // -> { cwd: '/foobar' }
  console.log(error.cause); // -> Error
}

Documentation

See API reference for auto-generated documentation.

Contributing

If you want to contribute, see CONTRIBUTING for details.

License

This package is licensed under the GNU Lesser General Public License v3. See LICENSE for details.

3.0.2

29 days ago

3.0.1-next.4fd23cd

3 months ago

3.0.1-next.60933d4

3 months ago

3.0.1-next.1788346

3 months ago

3.0.1-next.01330c0

3 months ago

3.0.1-next.007199a

3 months ago

3.0.1

3 months ago

3.0.1-next.b10d52c

3 months ago

3.0.0

4 months ago

3.0.0-next.6fd9f2b

4 months ago

3.0.0-next.2fa8a2c

4 months ago

3.0.0-next.b3909a5

4 months ago

3.0.0-next.bc04e4f

4 months ago

2.0.3-a1d4551

10 months ago

2.0.3

9 months ago

2.0.2

10 months ago

2.0.1-8a0a4d5

10 months ago

2.0.3-b8faa49

9 months ago

2.0.1

10 months ago

2.0.2-next.990a7af

10 months ago

2.0.2-186a8e5

10 months ago

2.0.1-6cee770

11 months ago

2.0.1-c2efc51

10 months ago

2.0.3-next.121ef1e

9 months ago

2.0.1-7dbb232

10 months ago

2.0.1-2f12db0

10 months ago

2.0.3-2487994

10 months ago

3.0.0-next.b19329b

7 months ago

2.0.1-next.336fe21

10 months ago

2.0.1-dee6883

10 months ago

3.0.0-next.a956e19

7 months ago

2.0.1-bc209e5

11 months ago

3.0.0-next.f3375f0

7 months ago

3.0.0-next.7a1e391

7 months ago

3.0.0-next.7305494

7 months ago

3.0.0-next.f9ef3d7

7 months ago

2.0.1-2d1145f

11 months ago

2.0.1-c5ebb6f

11 months ago

2.0.1-593aae1

11 months ago

2.0.1-265acad

11 months ago

2.0.1-5f6d70c

11 months ago

2.0.1-9029e49

11 months ago

2.0.1-11dd4a3

11 months ago

2.0.1-a500263

11 months ago

2.0.0-f7731ef

1 year ago

2.0.0-9cfd7b2

1 year ago

2.0.0

1 year ago

2.0.0-a3e1bca

1 year ago

1.0.1-39b9d1a

1 year ago

1.0.1-5b1b7ef

2 years ago

1.0.0

2 years ago

1.0.0-5bca5d4

2 years ago

1.0.0-1fd57ce

2 years ago