Skip to content

fix: handle empty capacities array to prevent app crash#215

Closed
moltboie wants to merge 1 commit intohoiekim:mainfrom
moltboie:fix/empty-capacities-crash-211
Closed

fix: handle empty capacities array to prevent app crash#215
moltboie wants to merge 1 commit intohoiekim:mainfrom
moltboie:fix/empty-capacities-crash-211

Conversation

@moltboie
Copy link
Copy Markdown
Contributor

Summary

Fixes the TypeError: Cannot read properties of undefined (reading 'month') crash that occurs when a budget/section/category has an empty capacities array.

Root Cause

Two budgets in the DB have capacities: []. When getActiveCapacity() is called:

  1. sorted.find(...) returns undefined (empty array)
  2. Fallback sorted[sorted.length - 1] evaluates to sorted[-1]undefined
  3. capacity[interval] (e.g., capacity['month']) throws on undefined

This crashes the entire app — the error boundary catches it but there's no recovery.

Fix

Client (BudgetFamily.ts):

  • getActiveCapacity() now returns a default new Capacity() (month=0) when capacities is empty

Server (budgets.ts):

  • createBudget, createSection, and createCategory now provide a default capacity ({month: 0, capacity_id: ...}) when none is specified, preventing empty arrays from being stored

Testing

  1. Started app with existing empty-capacities budgets in DB
  2. Verified app loads without crash — budgets page shows all budgets including the previously-crashing ones
  3. Verified Dashboard and Accounts pages load correctly
  4. TypeScript compiles clean (tsc --noEmit)

Closes #211

- Add defensive return in getActiveCapacity when capacities is empty
- Server-side: createBudget/Section/Category now provide a default
  capacity with month=0 when none is specified

Closes hoiekim#211
@moltboie moltboie force-pushed the fix/empty-capacities-crash-211 branch from 19ee0b1 to 86d1fb2 Compare March 21, 2026 16:49
@hoiekim hoiekim closed this Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: App crashes when budget has empty capacities array

2 participants