Skip to content

fix: print and capture auth handler errors - #454

Merged
omegascorp merged 2 commits into
mainfrom
fix/334-auth-telemetry
Nov 10, 2026
Merged

omegascorp merged 2 commits into
mainfrom
fix/334-auth-telemetry

Conversation

@omegascorp

@omegascorp omegascorp commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #334. Supersedes #388 by @flcrom, whose commit credit is kept via Co-authored-by.

Problem

Auth landing handlers (magic link, password reset, email verification), the GitHub/Google OAuth callbacks, and auth hooks catch their own errors and return a 302 or a fallback response. The route handler's captureError never sees these errors, so they reached stdout only and never APM.

#388 switched these sites to captureError alone. With telemetry on (deployed apps), that sends to APM and prints nothing, so the errors would have dropped off the Logs page (stdout/stderr only since #343).

Change

  • Each call site now calls logError(message, { error }) for stdout and captureError(error) for APM. This matches routes/handler.ts.
  • captureError accepts unknown and wraps non-Error values, so call sites don't each need an instanceof check. Existing callers are unaffected.
  • Covered sites: magic link landing, magic link token restore, fireAuthHook, password reset landing, email verification, GitHub/Google OAuth callbacks, OAuth error-redirect build failure, errorComponent failure, and safelyCallHook.
  • Left as is: the rejected mobile redirectUri log in oauthCommon.ts. It is a rejected input, not a caught exception.
  • User-facing redirects and responses are unchanged.

Test plan

  • Tests assert both logError and captureError for each covered handler. All 9 new assertions fail on main and pass on this branch.
  • vitest run: 1545/1545 pass
  • tsc --noEmit, eslint, and npm run build are clean
  • After deploy, trigger a bad magic link and confirm the error appears in both the Logs page and APM

🤖 Generated with Claude Code


Note

Low Risk
Observability-only changes in catch blocks; auth behavior and client-facing responses stay the same.

Overview
Auth flows that catch errors internally (magic link landing and token restore, password reset landing, email verification, GitHub/Google OAuth callbacks, OAuth error rendering/redirect fallbacks, and auth/OAuth hooks) no longer rely on console.error alone. Each site now calls logError so failures still show on the Logs page and captureError so telemetry-enabled deployments report them to APM, matching the route handler pattern.

captureError now accepts unknown, normalizes non-Error throws into an Error (with safe handling when stringification fails), and tests cover those edge cases. User-facing redirects and responses are unchanged; tests were updated to mock @/telemetry and assert both logging and capture at each handler.

Reviewed by Cursor Bugbot for commit c2c802c. Bugbot is set up for automated code reviews on this repo. Configure here.

Auth landing handlers, OAuth callbacks and auth hooks catch their own
errors and return a redirect or a fallback response, so the route
handler's captureError never saw them and they reached stdout only.

Each call site now prints the error with logError (stdout, so it stays
on the Logs page) and sends it to APM with captureError. captureError
accepts unknown and wraps non-Error values.

Based on the approach in #388.

Co-authored-by: vardhman bengani <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>

@eduard-piliposyan eduard-piliposyan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. This fixes #334, and redirects and responses are unchanged. Widening captureError to accept unknown doesn't break existing callers of the public modelence/telemetry export. The auth and telemetry suites pass locally (610/610).

Some notes, none of them blocking:

  • Log level now applies. logError checks MODELENCE_LOG_LEVEL, but the old console.error always printed. If telemetry is on and the level is set to something outside debug/info/error (say warn), these errors only go to APM. The route handler already behaves this way, so this is fine. Just calling it out.
  • Errors print twice locally. With telemetry off, logError prints the message and error, and then captureError prints the error again.
  • captureError can throw on unusual values. new Error(String(error)) throws if the value can't be turned into a string, for example Object.create(null). Since it runs inside catch blocks, in fireAuthHook it would hide the real error, and in the landing handlers it would turn the 302 into a 500. handler.ts and methods/index.ts already use the same pattern, so this isn't new. A try { String(error) } catch { 'Unknown error' } would close it if you want.
  • Not changed by this PR: in verification.ts, handleVerifyEmail still logs nothing when the thrown value isn't an Error, because of the instanceof check that was already there.

captureError built its message with String(error), which throws for
values such as Object.create(null). It runs inside catch blocks, so that
second throw hid the original error, or turned an auth landing redirect
into a 500. It now falls back to 'Unknown error'.

handleVerifyEmail only logged and captured failures that were Error
instances. It now does so for every failure; only the
onEmailVerificationError hook, which expects an Error, stays behind the
instanceof check.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@omegascorp
omegascorp merged commit f4e04ab into main Nov 10, 2026
5 checks passed
@omegascorp
omegascorp deleted the fix/334-auth-telemetry branch November 10, 2026 07:57
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.

Telemetry: Replace console.error with captureError in resetPassword landing handler

2 participants