🚯 dontpanic
Introduction
Humans are known to panic. This is widely considered bad design.
function eat(f: string) {
if (f === '🌯') ...
else throw;
}
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.
DontPanic(eat)('🌯').onSuccess(sleep).onFailure(getPizza);
Principles
🚯 No littering
Unhandled throw
s crash your program and send it into an unrecoverable state.
throw new Error('Invalid input');
return Failed('Invalid input');
99.9% of the time there is no need to panic.
⚠️ Explicit content
Throwable functions do not encode potential in their type signature.
const parsed = JSON.stringify(input);
DontPanic(JSON.stringify)(input);
Explicit is better than implicit.
🥞 Flat as a pancake
Try/catch creates new execution scopes.
try {
const parsed = JSON.parse(input);
try {
const validated = validateInput(parsed);
register(validated);
} catch (e) {
handleValidationError(e);
}
} catch (e) {
handleParsingError(e);
}
DontPanic(JSON.parse)(input)
.onFailure(handleParsingError)
.onSuccess(validateInput)
.onFailure(handleValidationError)
.onSuccess(register);
Flatten your error handling.
Install
npm install @sidiousware/dontpanic