Always validate preconditions before performing operations that could throw runtime errors. This includes checking object property existence, verifying context validity, and ensuring required data is available before proceeding.
When validation fails, handle the situation gracefully rather than allowing the operation to throw. Options include:
Example of defensive property access:
// Instead of direct access that could throw:
created_at__after: dayjs().subtract(TIME_PERIOD_MAPPING[period].value, 'day')
// Add validation first:
...((period !== '9' && TIME_PERIOD_MAPPING[period])
? {
created_at__after: dayjs().subtract(TIME_PERIOD_MAPPING[period].value, 'day')
}
: {})
This approach prevents runtime crashes and provides better user experience by handling edge cases proactively rather than reactively.
Enter the URL of a public GitHub repository