- Published on
Early return for unhappy path
A pattern I like is to return early for unhappy paths in a function
- Authors
- Name
- Nico Prananta
- Follow me on Bluesky
A PR was made, something like this:
// next.js getServerSideProps
export const getServerSideProps = async (ctx) => {
const { valid, event } = await isValidEvent({
organizationSlug,
projectSlug,
probeSlug,
eventSlug,
userID: ctx.user?.id,
})
const currentEvent = {
id: event?.id,
locationId: event?.locationId,
monika: event?.monikaId,
alert: event?.alertId,
response: parseEventResponse(event?.response),
createdAt: event?.createdAt.toString(),
recoveredAt: event?.recoveredAt?.toString() || '',
location: event?.location,
}
return {
props: {
currentEvent,
},
}
}
The problem with that PR is that it's reading the optional event
too many times. Furthermore, in this case since it's Next.js's getServerSideProps, it's better to check the existence of event
and return notFound when it's null
or undefined
since the page component expects a valid event
data.