-
-
Save DonHuskini/4d4108254c940555bfc68ffae9cb6fe3 to your computer and use it in GitHub Desktop.
[Tutorial] Functional try catch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const asyncTryCatch = async (tryer) => { | |
try { | |
const data = await tryer(); | |
return [data, null]; | |
} catch (error) { | |
return [null, error]; | |
} | |
}; | |
export default async function (req: NextApiRequest, res: NextApiResponse) { | |
const { cartId } = req.query; | |
const [data, error] = await asyncTryCatch(() => api.get(`/checkouts/${cartId}?type=cart`)); | |
if (error) { | |
return res.json({ error: error.response.data }); | |
} | |
res.json({ data }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment