Skip to content

Fix crash when order total is between 500-1000 credits (no matching discount tier)#8

Draft
kaushik94 wants to merge 1 commit into
mainfrom
fix/auto-fix-crash-when-order-total-is-between-50-1778307877
Draft

Fix crash when order total is between 500-1000 credits (no matching discount tier)#8
kaushik94 wants to merge 1 commit into
mainfrom
fix/auto-fix-crash-when-order-total-is-between-50-1778307877

Conversation

@kaushik94
Copy link
Copy Markdown
Contributor

Root Cause

At line 225 of server.ts, inside the POST /api/orders handler, when rawTotal is between 500 and 1000, the code does DISCOUNT_TIERS.find(t => rawTotal >= t.threshold) which looks for a tier where rawTotal >= threshold. For orders between 500 and 999, rawTotal is less than the first threshold of 1000, so find() returns undefined. The code then accesses tier!.percentage on undefined, causing the crash. The fix is to add a guard: only apply the discount if a matching tier is found.

Reviewer Sign-off

Confidence: 9/10
The fix directly addresses the root cause: when rawTotal is between 500 and 1000, DISCOUNT_TIERS.find(t => rawTotal >= t.threshold) returns undefined because neither tier's threshold (1000, 2000) is met. The original code used tier!.percentage (non-null assertion) on the potentially-undefined result, causing the crash. The fix wraps the discount application in an if (tier) guard, so tier.percentage is only accessed when a matching tier actually exists. When no tier matches, total remains equal to rawTotal — no discount is applied, which is the correct behavior for orders below the lowest threshold. The change is minimal: only lines 225–228 are modified by (1) adding if (tier) { after the find(), (2) removing the ! non-null assertions from tier!.percentage, and (3) closing the block with }. No new abstractions, patterns, or behaviors are introduced. The coding style (variable naming, console.log format, numeric formatting with +(...).toFixed(2)) is consistent with the rest of the file. No other code paths are affected.

Log Sample

Unhandled error: Cannot read properties of undefined (reading 'percentage')
TypeError: Cannot read properties of undefined (reading 'percentage')
    at <anonymous> (/Users/kaushik/Desktop/starwars-shop/backend/server.ts:225:41)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:119:3)
    at patched (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/src/instrumentation.ts:286:27)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:280:10)
    at arguments.<computed> (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/src/instrumentation.ts:281:29)
Unhandled error: Cannot read properties of undefined (reading 'percentage')
TypeError: Cannot read properties of undefined (reading 'percentage')
    at <anonymous> (/Users/kaushik/Desktop/starwars-shop/backend/server.ts:225:41)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:119:3)
    at patched (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/src/instrumentation.ts:286:27)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:280:10)
    at arguments.<computed> (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/src/instrumentation.ts:281:29)
Unhandled error: Cannot read properties of undefined (reading 'percentage')
TypeError: Cannot read properties of undefined (reading 'percentage')
    at <anonymous> (/Users/kaushik/Desktop/starwars-shop/backend/server.ts:225:41)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:119:3)
    at patched (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/src/instrumentation.ts:286:27)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:280:10)
    at arguments.<computed> (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/src/instrumentation.ts:281:29)
Unhandled error: Cannot read properties of undefined (reading 'percentage')
TypeError: Cannot read properties of undefined (reading 'percentage')
    at <anonymous> (/Users/kaushik/Desktop/starwars-shop/backend/server.ts:225:41)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:119:3)
    at patched (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/src/instrumentation.ts:286:27)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:280:10)
    at arguments.<computed> (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/src/instrumentation.ts:281:29)
Unhandled error: Cannot read properties of undefined (reading 'percentage')
TypeError: Cannot read properties of undefined (reading 'percentage')
    at <anonymous> (/Users/kaushik/Desktop/starwars-shop/backend/server.ts:225:41)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:119:3)
    at patched (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/src/instrumentation.ts:286:27)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:280:10)
    at arguments.<computed> (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/src/instrumentation.ts:281:29)
Unhandled error: Cannot read properties of undefined (reading 'percentage')
TypeError: Cannot read properties of undefined (reading 'percentage')
    at <anonymous> (/Users/kaushik/Desktop/starwars-shop/backend/server.ts:225:41)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:119:3)
    at patched (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/src/instrumentation.ts:286:27)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:280:10)
    at arguments.<computed> (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/src/instrumentation.ts:281:29)
Unhandled error: Cannot read properties of undefined (reading 'percentage')
TypeError: Cannot read properties of undefined (reading 'percentage')
    at <anonymous> (/Users/kaushik/Desktop/starwars-shop/backend/server.ts:225:41)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:119:3)
    at patched (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/src/instrumentation.ts:286:27)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:280:10)
    at arguments.<computed> (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/src/instrumentation.ts:281:29)
Unhandled error: Cannot read properties of undefined (reading 'percentage')
TypeError: Cannot read properties of undefined (reading 'percentage')
    at <anonymous> (/Users/kaushik/Desktop/starwars-shop/backend/server.ts:225:41)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:119:3)
    at patched (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/src/instrumentation.ts:286:27)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:280:10)
    at arguments.<computed> (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/src/instrumentation.ts:281:29)
Unhandled error: Cannot read properties of undefined (reading 'percentage')
TypeError: Cannot read properties of undefined (reading 'percentage')
    at <anonymous> (/Users/kaushik/Desktop/starwars-shop/backend/server.ts:225:41)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:119:3)
    at patched (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/src/instrumentation.ts:286:27)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:280:10)
    at arguments.<computed> (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/src/instrumentation.ts:281:29)
Unhandled error: Cannot read properties of undefined (reading 'percentage')
TypeError: Cannot read properties of undefined (reading 'percentage')
    at <anonymous> (/Users/kaushik/Desktop/starwars-shop/backend/server.ts:225:41)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:119:3)
    at patched (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/src/instrumentation.ts:286:27)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:280:10)
    at arguments.<computed> (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/src/instrumentation.ts:281:29)

Opened automatically by the RocketGraph AI-SRE agent.
Reviewer agent signed off before this PR was created.

Fix crash when order total is between 500-1000 credits (no matching discount tier)
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.

1 participant