Skip to content

Commit

Permalink
add try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
ignmandagaran committed Jun 15, 2022
1 parent c49b3ff commit 124964b
Showing 1 changed file with 47 additions and 40 deletions.
87 changes: 47 additions & 40 deletions src/pages/api/experiments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,58 @@ export default async function handler(
_req: NextApiRequest,
res: NextApiResponse
) {
const allSlugs = await getAllExperimentSlugs()
try {
const allSlugs = await getAllExperimentSlugs()

const modules = await Promise.all(
allSlugs.map((slug) =>
import(`~/experiments/${slug}`).then((m) => [slug, m.default])
)
)

let experiments = modules
.map((exp) => {
const title: string = exp[1].Title || exp[0]

return {
filename: exp[0],
title,
href: `/experiments/${exp[0]}`,
tags:
(exp[1].Tags as string)
?.split(',')
?.map((tag) => tag.toLowerCase().trim()) || []
}
})
.sort((a, b) =>
a.filename.localeCompare(b.filename, undefined, { numeric: true })
const modules = await Promise.all(
allSlugs.map((slug) =>
import(`~/experiments/${slug}`).then((m) => [slug, m.default])
)
)

experiments = experiments.filter((e) => !e.tags.includes('private'))
let experiments = modules
.map((exp) => {
const title: string = exp[1].Title || exp[0]

return {
filename: exp[0],
title,
href: `/experiments/${exp[0]}`,
tags:
(exp[1].Tags as string)
?.split(',')
?.map((tag) => tag.toLowerCase().trim()) || []
}
})
.sort((a, b) =>
a.filename.localeCompare(b.filename, undefined, { numeric: true })
)

// Numerate experiments
experiments = experiments.map((e, i) => ({
...e,
number: i + 1
}))
experiments = experiments.filter((e) => !e.tags.includes('private'))

// Add contributors
experiments = await Promise.all(
experiments.map(async (e) => {
const contributors = await getFileContributors(getExamplePath(e.filename))
// Numerate experiments
experiments = experiments.map((e, i) => ({
...e,
number: i + 1
}))

return {
...e,
contributors
}
})
)
// Add contributors
experiments = await Promise.all(
experiments.map(async (e) => {
const contributors = await getFileContributors(
getExamplePath(e.filename)
)

return {
...e,
contributors
}
})
)

res.status(200).json(experiments)
res.status(200).json(experiments)
} catch (err) {
console.error(err)
res.status(500).json({ error: err.message })
}
}

0 comments on commit 124964b

Please sign in to comment.