Skip to content

chore(auth): remove dead try/catch around isMaster + getCompanyId in attachAuth#162

Merged
CryptoJones merged 1 commit into
masterfrom
chore/auth-attach-remove-dead-try-catch
May 19, 2026
Merged

chore(auth): remove dead try/catch around isMaster + getCompanyId in attachAuth#162
CryptoJones merged 1 commit into
masterfrom
chore/auth-attach-remove-dead-try-catch

Conversation

@CryptoJones
Copy link
Copy Markdown
Owner

Closes #161.

Summary

The outer try { req.isMaster = await isMaster(authKey) } catch { res.status(500) } (and the equivalent wrapper around getCompanyId) in attachAuth was dead code: both inner helpers wrap their Sequelize query in try/catch and return false / -1 sentinels on error rather than throwing. The outer catch had nothing to handle.

Misleading: readers think attachAuth distinguishes "DB down" (500) from "auth fails" (downstream 403). It does not — a DB outage surfaces as 403 "Invalid Authorization Key.", with the actual cause only in operator-side logs.

Strip the dead wrappers. Add a comment explaining the swallow-into-sentinel design + the real-fix path if anyone wants DB-down to surface as 500 (push the throw back up from isMaster / getCompanyId; cascading refactor, separate PR).

Test plan

  • npm run lint — clean (eslint's args: "after-used" default lets res slide since next after it is used)
  • npm test — 614 passed, 15 skipped (unchanged from master; no behaviour shift)

Proudly Made in Nebraska. Go Big Red! 🌽 https://xkcd.com/2347/

…attachAuth

`isMaster` and `getCompanyId` both wrap their DB query in a
try/catch INTERNALLY (see auth.js:97-109 and 111-125): on any
Sequelize / pg error they `log.error` and return `false` or `-1`
respectively. The errors don't propagate.

So the outer `try { req.isMaster = await isMaster(authKey) }
catch { return res.status(500) }` in attachAuth had nothing to
catch — the awaited promise never rejects. Same for the
getCompanyId wrapper below it. Both `500` paths were unreachable
dead code that misled readers into thinking attachAuth
distinguished "auth failed" from "DB down".

Strip the dead try/catch. Add a comment explaining the
swallow-into-sentinel design so the next reader doesn't
re-introduce the outer catches, and call out the real fix path
if anyone DOES want DB-down to surface as 500 (push the throw
back up from isMaster / getCompanyId, then layer a catch here
that's actually reachable). Behaviour unchanged; test suite
unchanged at 614 passing.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@CryptoJones CryptoJones merged commit 15d298c into master May 19, 2026
3 checks passed
@CryptoJones CryptoJones deleted the chore/auth-attach-remove-dead-try-catch branch May 19, 2026 07:54
CryptoJones added a commit that referenced this pull request May 19, 2026
Cleanup follow-up to #162, which removed the outer try/catch in
attachAuth. Since attachAuth no longer has any 500-emitting path (its
internal callees swallow infrastructure errors into sentinels and
always invoke next()), the `attachOk` boolean was guaranteed true
after the awaited Promise resolved.

The dead `if (!attachOk) return;` line and its stale
"attachAuth already sent a 500" comment misled readers into thinking
resolveAuth still had a divergent error path. Drop both. Behavior
unchanged.

All 688 tests still pass.

Co-authored-by: Aaron K. Clark <akclark@thenetwerk.net>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

auth: dead try/catch around isMaster + getCompanyId in attachAuth misleads readers

1 participant