https://github.com/offlinehacker/errorjs
Extendable javascript errors
https://github.com/offlinehacker/errorjs
error errors flowtype javascript typescript
Last synced: 2 months ago
JSON representation
Extendable javascript errors
- Host: GitHub
- URL: https://github.com/offlinehacker/errorjs
- Owner: offlinehacker
- License: mit
- Created: 2016-11-28T01:07:52.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-10-10T07:36:07.000Z (about 8 years ago)
- Last Synced: 2025-03-15T04:46:19.698Z (9 months ago)
- Topics: error, errors, flowtype, javascript, typescript
- Language: TypeScript
- Homepage: https://offlinehacker.github.io/errorjs/
- Size: 221 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ErrorJS
[](http://badge.fury.io/js/errorjs)
[](https://github.com/prettier/prettier)
[](https://greenkeeper.io/)
[](https://travis-ci.org/offlinehacker/errorjs)
[](https://coveralls.io/github/offlinehacker/errorjs)
[](https://david-dm.org/offlinehacker/errorjs?type=dev)
Extendable javascript errors
## About
After extending javascript `Error` class in every single project and copying
allways different bad snippets from stackoverflow, i wanted to have a good
error base that i can reuse in different javascript/typescript projects.
This library tries to provide good simple base for javascript errors.
## Install
```
npm install --save errorjs
```
## Features
### Sane predefined errors
ErrorJS has a list of predefined errors, that you can reuse while devloping a service.
```javascript
import {
NotImplementedError,
InternalError,
NotFoundError,
UnauthorizedError,
ConnectionError,
ValidationError,
ConflictError,
LogicalError
} from 'errorjs';
throw new NotImplementedError('some_feature_not_implemented');
throw new InternalError('something_internally');
throw new NotFoundError('something_was_not_found');
throw new UnauthorizedError('access_was_unauthroized');
throw new ConnectionError('error_with_connection_to_server');
throw new ValidationError('error_validating_resource');
throw new ConflictError('some_conflict');
throw new LogicalError('some_logic_error');
```
### Enforced error codes
Every error must have error code provided, which should uniquely identify an error
```javascript
import {ConflictError} from 'errorjs';
throw new ConflictError(); // <-- invalid since no error code is provided
throw new ConflictError('some_error_code'); // <-- good, since error code is provided
```
### Support for error context
Additional context can be passed when creating an error
**Simple error context**
```javascript
import {ConflictError} from 'errorjs';
throw new ConflictError('some_error_code', {some: 'context'});
```
**Error class with predefined error context**
```javascript
const ConflictWithContext = ConflictError.withContext({some: 'context'});
throw new ConflictWithContext('some_error_code', {some: 'othercontext'});
```
**Error factory with predefined error context**
```javascript
import {errors} from 'errorjs';
const contextErrors = errors.withContext({userId: ''});
const error = new contextErrors.ConflictError('user_exists', {errorId: ''});
error.context // {userId: '', errorId: ''}
```
### Support for child errors
Child errors can be passed to an error to encapsulate errors
```javascript
import {ConflictError} from 'errorjs';
const baseError = new Error('some base error');
const baseError2 = new Error('some other error');
throw new ConflictError('some_error_code', baseError, baseError2);
```
### Variable arguments
Error constructor accepts variable number of arguments. This enable to
pass multiple context objects to error.
```javascript
import {ValidationError} from 'errorjs';
const baseError = new Error('some error has happend');
throw new ValidationError('some_error_code', baseError, {some: 'context'}, {some: 'other_context'});
```
### TypeScript/flowtype support
ErrorJS is tightly integrated with typescript and provides type information.
```javascript
import {errors} from 'errorjs';
errors.NotImplemented // <--- autocomplete here
new errors.NotFoundError( /* autocomplete here */ );
```
### Extendable error factory and base error
Error factory can be extended with new errors classes and base error can be redefined.
```javascript
import {ExtendedErrorFactory} from 'errorjs';
class MyVeryBadErrorFactory extends ExtendedErrorFactory {
BaseError = class BaseError extends this.BaseError {
isVeryBadError = true;
get isUserError() {
return this.context.userID;
}
};
MyError = this.defineErrorClass(
class MyError extends this.BaseError {
meta = {httpCode: 500};
}
);
}
const factory = new MyErrorFactory();
const error = factory.NotFoundError('some_error', {userID: '123-123'});
error.isVeryBadError // <-- extended functionality here
throw new factory.MyError('some_error_code');
```
## Examples
### TypeScript
```javascript
import {ExtendedErrorFactory} from 'errorjs';
class MyErrorFactory extends ExtendedErrorFactory {
BaseError = class MyBaseError extends this.BaseError {
scope = 'global';
get isUserError() {
return this.context.userId;
}
};
TransactionError = this.defineErrorClass(
class TransactionError extends this.ConflictError {
scope = 'transaction';
get userMessage() {
return `{${this.scope}} ${super.userMessage}`;
}
}
);
}
const errorFactory = new MyErrorFactory();
const userErrors = factory.withContext({userId: ''});
throw new userErrors.TransactionError('transaction_not_found', 'transaction was not found', {
id: ''
});
```
### JavaScript
```javascript
const {ExtendedErrorFactory} = require('errorjs');
class MyErrorFactory extends ExtendedErrorFactory {
constructor(...args) {
super(...args);
this.BaseError = class MyBaseError extends this.BaseError {
scope = 'global';
get isUserError() {
return this.context.userId;
}
};
this.TransactionError = this.defineErrorClass(
class TransactionError extends this.ConflictError {
get scope() {
return 'transaction';
}
get userMessage() {
return `{${this.scope}} ${super.userMessage}`;
}
}
);
}
}
const errorFactory = new MyErrorFactory();
const userErrors = factory.withContext({userId: ''});
throw new userErrors.TransactionError('transaction_not_found', 'transaction was not found', {
id: ''
});
```
## Development
- `npm t`: Run test suite
- `npm start`: Runs `npm run build` in watch mode
- `npm run test:watch`: Run test suite in [interactive watch mode](http://facebook.github.io/jest/docs/cli.html#watch)
- `npm run test:prod`: Run linting and generate coverage
- `npm run build`: Generage bundles and typings, create docs
- `npm run lint`: Lints code
- `npm run commit`: Commit using conventional commit style ([husky](https://github.com/typicode/husky) will tell you to use it if you haven't :wink:)
## Credits
Made with :heart: by [@offlinehacker](https://twitter.com/offlinehacker)