https://github.com/watchdg/node-result-error
class ResultError
https://github.com/watchdg/node-result-error
Last synced: 15 days ago
JSON representation
class ResultError
- Host: GitHub
- URL: https://github.com/watchdg/node-result-error
- Owner: WatchDG
- License: mit
- Created: 2020-12-25T10:02:24.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-04-08T06:34:22.000Z (about 5 years ago)
- Last Synced: 2025-02-14T21:19:33.403Z (over 1 year ago)
- Language: TypeScript
- Size: 7.81 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# node-result-error
wrapper for data via error class ResultError
## install
```shell
npm install node-result-error
# or
yarn add node-result-error
```
## example
```ts
import {ResultOk, ResultFail} from 'node-result';
import {ResultError} from "node-result-error";
function checkParameter(parameters: object, parameterName: string, regExp: RegExp) {
try {
const parameterValue = parameters[parameterName];
if (!regExp.test(parameterValue)) {
return ResultFail(new ResultError({
statusCode: 400,
body: JSON.stringify({
error: 'Unsupported parameter value.',
parameterName,
parameterValue
})
}));
}
return ResultOk(null);
} catch (error) {
return ResultFail(error);
}
}
((ctx) => {
try {
const user = {
firstName: 'Alex',
lastName: '_Smith',
age: '28'
};
checkParameter(user, 'firstName', /^[A-Z][a-z]+$/).unwrap();
checkParameter(user, 'lastName', /^[A-Z][a-z]+$/).unwrap();
checkParameter(user, 'age', /^[1-9][0-9]?$/).unwrap();
ctx.status = 200;
ctx.body = 'ok';
} catch (error) {
if (error instanceof ResultError) {
const data = error.unwrap();
ctx.status = data.statusCode;
ctx.body = data.body;
} else {
console.error(error);
ctx.status = 500;
}
}
})();
```