Handling async errors in JavaScript often turns into a mess of try-catch blocks everywhere. It clutters your code and gets harder to deal with as the project grows.
What if there was a way to handle async errors that's cleaner, easier to scale, and keeps the logic separate from your main functions? That’s exactly the idea behind Lovit — a library designed to help you manage async errors without stuffing try-catch blocks everywhere. It provides a structured, readable way to handle things like 404s and network failures outside your main logic.
Traditional Approach: It Works, But Gets Messy
Let’s say you’re fetching posts from an API:
async function getPosts() { try { const res = await fetch('http://localhost:3001/posts'); if (res.status === 404) { console.warn('⚠️ Posts not found'); return; } const posts = await res.json(); return posts; } catch (error) { console.error('❌ Error fetching posts:', error); } } But as we start adding more logic — like checking for other status codes or doing more than just logging to the console — the function can quickly become harder to read and maintain.
💡 Lovit Style: Clean, Focused, and Separated
Lovit separates what you want to do from how to handle errors if it fails.
import { fetchLovit } from 'lovit'; async function getPosts() { const res = await fetchLovit({ key: 'post.getPosts', url: 'http://localhost:3001/posts', }); const posts = await res.json(); return posts; } Boom — no try, no catch, no if (res.status === 404). Just your business logic.
But where does the error handling go?
Define Error Logic with createProfile
import { createProfile } from 'lovit'; const postProfile = createProfile({ name: 'post', tasks: { getPosts: { notFound: () => console.warn('⚠️ Posts not found'), catch: (err) => console.error('❌ Network error:', err), }, }, }); This puts all your error handling in one place, organized by what part of your app it belongs to
- notFound handles 404s
- catch handles general exceptions like network failures
Lovit automatically triggers these handlers when something goes wrong in getPosts.
📚✨ There’s a lot more this new library can do—check out the docs for a detailed explanation.
Top comments (0)