svelte-unsafe
v0.2.0
Published
Manage errors through conditionals.
Downloads
278
Readme
Svelte Unsafe
Manage errors through conditionals.
Install with
npm i -D svelte-unsafe
Use ok()
to create a successful result
import { ok } from 'svelte-unsafe'
/**
* @returns {import("./types").Unsafe<string>}
*/
function greeting(){
return ok("hello world")
}
Or error()
to create errors
/**
* @param {string} name
* @returns {import("./types").Unsafe<string>}
*/
function greet(name){
if(name.length < 2){
return error('Name must be at least 2 characters.')
}
return ok('hello world')
}
Then manage your errors through conditionals
const greetAttempt = greet('A')
if(greetAttempt.error){
console.error(greetAttempt.error)
} else {
console.log(greetAttempt.value)
}