I see that you are having a difficult time, let me help you.
npm install @sidiousware/dontpanic[![npm package][npm-img]][npm-url]
[![Build Status][build-img]][build-url]
[![Code Coverage][codecov-img]][codecov-url]
[![Commitizen Friendly][commitizen-img]][commitizen-url]
[![Semantic Release][semantic-release-img]][semantic-release-url]
Humans are known to panic. This is widely considered bad design.
``ts`
function eat(f: string) {
if (f === '๐ฏ') ...
else throw; // panic
}
Panic is not usefulโข. What's useful is a thorough assessment of the circumstances. dontpanic brings you _blazingly steadfast_ abstractions that help you recover from failure and guide your code to success.
`ts`
DontPanic(eat)('๐ฏ').onSuccess(sleep).onFailure(getPizza); // ๐ Don't panic
Unhandled throws crash your program and send it into an unrecoverable state.
`ts
throw new Error('Invalid input'); // ๐ฏ Crash risk
return Failed('Invalid input'); // ๐ฃ No crash risk
`
99.9% of the time there is no need to panic.
Throwable functions do not encode potential in their type signature.
`ts
const parsed = JSON.stringify(input); // โ ๏ธ Number, may crash
DontPanic(JSON.stringify)(input); // โ
Outcome
`
Explicit is better than implicit.
Try/catch creates new execution scopes.
`ts
try {
const parsed = JSON.parse(input);
try {
const validated = validateInput(parsed);
register(validated);
} catch (e) {
handleValidationError(e);
}
} catch (e) {
handleParsingError(e); // ๐ชน Far away from home
}
DontPanic(JSON.parse)(input) // ๐ฅ
.onFailure(handleParsingError)
.onSuccess(validateInput)
.onFailure(handleValidationError)
.onSuccess(register);
`
Flatten your error handling.
`bash``
npm install @sidiousware/dontpanic
[build-img]: https://github.com/sidiousvic/dontpanic/actions/workflows/release.yml/badge.svg?branch=main
[build-url]: https://github.com/sidiousvic/dontpanic/actions/workflows/release.yml?branch=main
[npm-img]: https://img.shields.io/npm/v/dontpanic
[npm-url]: https://www.npmjs.com/package/dontpanic
[codecov-img]: https://codecov.io/gh/sidiousvic/dontpanic/branch/prod/graph/badge.svg
[codecov-url]: https://codecov.io/gh/sidiousvic/dontpanic
[semantic-release-img]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
[semantic-release-url]: https://github.com/semantic-release/semantic-release
[commitizen-img]: https://img.shields.io/badge/commitizen-friendly-brightgreen.svg
[commitizen-url]: http://commitizen.github.io/cz-cli/