Might & Fail
Two utility functions that construct an Either and promotes working with the Either type directly.
When to use them
mightFail
and mightFailSync
are alternatives to try
, catch
, finally
blocks. These functions catch the errors and suply a much better interface for working with errors.
However, there may be times when you don't want to throw Errors in your code. I can imagine mjs
or quickjs
might be good use cases for this.
Or maybe you don't want to catch all errors that might be thrown because some of them need to propagate to the top of the call stack for framework reasons.
So this is for when you don't want to throw errors, but you want to have functions that return an Either type.
Syncronous Either
import { Either, Might, Fail } from 'might-fail'
export function foo(): Either<{message: string}> {
// ...
if (condition) {
// notice that we don't have to wrap the string to an Error construct
// because Fail will automatically wraps it in Error for us.
return Fail("Something went wrong")
}
if (condition2) {
return Fail("Something else went wrong")
}
return Might({message: "All good"})
}
import { Either, Might, Fail } from 'might-fail'
export function foo(): Either<{message: string}> {
// ...
if (condition) {
// notice that we don't have to wrap the string to an Error construct
// because Fail will automatically wraps it in Error for us.
return Fail("Something went wrong")
}
if (condition2) {
return Fail("Something else went wrong")
}
return Might({message: "All good"})
}
import { Either, Might, Fail } from 'might-fail'
export function foo(): Either<{message: string}> {
// ...
if (condition) {
// notice that we don't have to wrap the string to an Error construct
// because Fail will automatically wraps it in Error for us.
return Fail("Something went wrong")
}
if (condition2) {
return Fail("Something else went wrong")
}
return Might({message: "All good"})
}
import { Either, Might, Fail } from 'might-fail'
export function foo(): Either<{message: string}> {
// ...
if (condition) {
// notice that we don't have to wrap the string to an Error construct
// because Fail will automatically wraps it in Error for us.
return Fail("Something went wrong")
}
if (condition2) {
return Fail("Something else went wrong")
}
return Might({message: "All good"})
}
Asyncronous Either
import { Either, Might, Fail } from 'might-fail'
export async function foo(): Promise<Either<{message: string}>> {
// ...
if (condition) {
// notice that we don't have to wrap the string to an Error construct
// because Fail will automatically wraps it in Error for us.
return Fail("Something went wrong")
}
if (condition2) {
return Fail("Something else went wrong")
}
return Might({message: "All good"})
}
import { Either, Might, Fail } from 'might-fail'
export async function foo(): Promise<Either<{message: string}>> {
// ...
if (condition) {
// notice that we don't have to wrap the string to an Error construct
// because Fail will automatically wraps it in Error for us.
return Fail("Something went wrong")
}
if (condition2) {
return Fail("Something else went wrong")
}
return Might({message: "All good"})
}
import { Either, Might, Fail } from 'might-fail'
export async function foo(): Promise<Either<{message: string}>> {
// ...
if (condition) {
// notice that we don't have to wrap the string to an Error construct
// because Fail will automatically wraps it in Error for us.
return Fail("Something went wrong")
}
if (condition2) {
return Fail("Something else went wrong")
}
return Might({message: "All good"})
}
import { Either, Might, Fail } from 'might-fail'
export async function foo(): Promise<Either<{message: string}>> {
// ...
if (condition) {
// notice that we don't have to wrap the string to an Error construct
// because Fail will automatically wraps it in Error for us.
return Fail("Something went wrong")
}
if (condition2) {
return Fail("Something else went wrong")
}
return Might({message: "All good"})
}