Skip to content

Commit 4351385

Browse files
author
Danial Manavi
committed
feat: Add unit tests.
1 parent e6a10aa commit 4351385

18 files changed

Lines changed: 697 additions & 51 deletions

README.md

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
[![NPM downloads][downloads-image]](https://npmjs.org/package/error-lib)
55
[![Build Status][github-actions-publish-npm-package]](https://github.com/DManavi/error_lib/actions/workflows/publish_npm_package.yml)
66

7-
## Deprecated
8-
9-
This package is deprecated. Please use [@speedup/errors](https://www.npmjs.com/package/@speedup/error).
10-
117
## About
128

139
The error-lib project helps developers having a unified error structure in their NodeJS/Browser (JavaScript/TypeScript) projects.
@@ -31,13 +27,47 @@ pnpm add error-lib
3127

3228
## Usage
3329

34-
See the [docs](https://github.com/DManavi/error_lib/wiki)
30+
To use any of the custom error libraries you need to simply import them in your typescript/javascript application.
31+
32+
```js
33+
34+
// for NodeJS applications (Common JS)
35+
const { ApplicationError, NotFoundError } = require('error-lib');
36+
37+
try {
38+
39+
if (fileNotFound === true) {
40+
throw new NotFoundError('config.json was not found');
41+
}
42+
}
43+
catch (err) {
44+
45+
if (err instanceof NotFoundError) {
46+
//
47+
}
48+
}
49+
50+
```
51+
52+
```ts
53+
54+
// For typescript/javascript (ES Module)
55+
import { ApplicationError, NotFoundError } from 'error-lib';
56+
57+
58+
59+
60+
```
61+
62+
## Extend / Custom errors
63+
64+
3565

3666
And you're good to go!
3767

3868
## License
3969

40-
GPL v3
70+
MIT
4171

4272
[npm-image]: https://img.shields.io/npm/v/error-lib
4373
[npm-url]: https://npmjs.org/package/error-lib

package-lock.json

Lines changed: 163 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
"clean": "rimraf dist/",
2525
"build:import": "tsc -b ./tsconfig.build.import.json",
2626
"build:require": "tsc -b ./tsconfig.build.require.json",
27-
"build": "run-p build:import build:require",
28-
"rebuild": "run-s clean build",
27+
"build": "run-p clean build:import build:require",
2928
"test": "jest",
3029
"test:watch": "jest --watch",
3130
"coverage": "jest --coverage"
@@ -53,6 +52,7 @@
5352
},
5453
"homepage": "https://github.com/DManavi/error_lib#readme",
5554
"devDependencies": {
55+
"@types/jest": "27.5.2",
5656
"jest": "28.1.0",
5757
"npm-run-all": "4.1.5",
5858
"rimraf": "3.0.2",

resources/diagram.png

2.06 KB
Loading

src/application_error.spec.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { ApplicationError } from './application_error';
2+
3+
describe('ApplicationError', () => {
4+
it('check inheritance tree', () => {
5+
const err = new ApplicationError();
6+
7+
expect(err instanceof Error).toBeTruthy();
8+
expect(err instanceof ApplicationError).toBeTruthy();
9+
});
10+
11+
it('should set default error message', () => {
12+
const err = new ApplicationError();
13+
14+
expect(err).toHaveProperty('message');
15+
expect(typeof err.message).toBe('string');
16+
expect(err.message).toBe('ApplicationError');
17+
});
18+
19+
it('should set custom error message', () => {
20+
const err = new ApplicationError('MyErrorMessage');
21+
22+
expect(err).toHaveProperty('message');
23+
expect(typeof err.message).toBe('string');
24+
expect(err.message).toBe('MyErrorMessage');
25+
});
26+
27+
it('should set default error code', () => {
28+
const err = new ApplicationError();
29+
30+
expect(err).toHaveProperty('code');
31+
expect(typeof err.code).toBe('string');
32+
expect(err.code).toBe('E_APPLICATION_ERROR');
33+
});
34+
35+
it('should set custom error code', () => {
36+
const err = new ApplicationError(undefined, { code: 'E_CUSTOM_ERROR' });
37+
38+
expect(err).toHaveProperty('code');
39+
expect(typeof err.code).toBe('string');
40+
expect(err.code).toBe('E_CUSTOM_ERROR');
41+
});
42+
43+
it('should set default cause (undefined)', () => {
44+
const err = new ApplicationError();
45+
46+
expect(err).toHaveProperty('cause');
47+
expect(typeof err.cause).toBe('undefined');
48+
expect(err.cause).toBe(undefined);
49+
});
50+
51+
it('should set custom cause (another application error)', () => {
52+
const cause = new ApplicationError(undefined, { code: 'E_CAUSE' });
53+
const err = new ApplicationError(undefined, { cause });
54+
55+
expect(err).toHaveProperty('cause');
56+
expect(err.cause instanceof ApplicationError).toBeTruthy();
57+
expect((err.cause as ApplicationError).code).toBe('E_CAUSE');
58+
});
59+
});

src/application_error.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,7 @@ export class ApplicationError<TCause extends Error = Error> extends Error {
4141
this.code = opts?.code ?? 'E_APPLICATION_ERROR';
4242
this.cause = opts?.cause;
4343

44-
this.configureSubError(ApplicationError);
45-
}
46-
47-
/**
48-
* Configure sub error
49-
* @param cls Constructor/class of the error
50-
*/
51-
protected configureSubError<T>(cls: Class<T>) {
52-
this.captureStackTrace(cls);
53-
this.setPrototypeOf(cls);
54-
}
55-
56-
/**
57-
* Capture stack trace in the error
58-
* @param cls Constructor/class of the error
59-
*/
60-
private captureStackTrace<T>(cls: Class<T>) {
61-
Error.captureStackTrace(this, cls);
62-
}
63-
64-
/**
65-
* Set prototype of the object to enable instanceOf operator
66-
* @param cls Constructor/class of the error
67-
*/
68-
private setPrototypeOf<T>(cls: Class<T>) {
69-
Object.setPrototypeOf(this, cls.prototype);
44+
Error.captureStackTrace(this, ApplicationError);
45+
Object.setPrototypeOf(this, ApplicationError.prototype);
7046
}
7147
}

0 commit comments

Comments
 (0)