1.1.3 • Published 4 years ago
@alexshelkov/result v1.1.3
Type-safe error handling without exceptions
Flexible and explicit error handling with a small flavor of functional languages.
Result type allows you go from this:
const user = getUser(email);
if (!user) {
throw new Error("Can't get user") // but what happens, why email is invalid?
// and who will catch this error?
}
to this:
const userResult = getUser(email);
if (userResult.isErr()) {
const err = userResult.err();
switch (err.type) {
case 'InvalidEmail':
console.log('User email is invalid'); break;
case 'UserNotExists':
console.log("Can't find the user"); break;
case 'UserBannded':
console.log(`User was bannded: ${err.ban}`); break;
}
}