Repository navigation
refactor(logging): use structured error logs in auth and live-query paths (#294) - #438
ankit-0903 wants to merge 6 commits into
Conversation
omegascorp
left a comment
There was a problem hiding this comment.
Thanks for picking this up! This merges cleanly onto current main, and the auth, live-query and telemetry suites all pass. A few things before it goes in:
Should fix
1. Two server-side console.error calls remain in oauthCommon.ts. #294 lists this file, and they're the last server-side ones in auth:
- ~L210: fallback when the OAuth error redirect can't be built
- ~L877: rejected mobile OAuth
redirectUri
Both put their context into the message string, so they're good candidates for structured fields, e.g.:
logError('[modelence] Rejected mobile OAuth redirectUri', {
redirectUri,
allowed: getAllowedMobileRedirectUrls(),
});(The ones in auth/client/index.ts run in the browser, so leaving them is correct.)
2. Keep the message constant and put the variable in context. A few messages still interpolate a value that's already passed as a field:
`[LiveQuery] Error fetching data for ${method}`/`[LiveQuery] Error in ${method}`(already passmethod)`Error in ${name} hook`(already passeshook)
With the variable in the message, these can't be grouped by message in log tooling. Suggest '[LiveQuery] Error fetching data' + { method }, and so on.
Worth noting
3. These errors can now be silenced. logError only prints when MODELENCE_LOG_LEVEL is debug/info/error. Any other value (e.g. warn, or a typo) now hides OAuth callback failures and email-verification and magic-link errors, which used to print unconditionally. That matches #422, so it's fine, but please mention it in the description.
Nits
4. Only the magic-link hook path has assertions, and it spies on console.error with the exact (message, args) shape, so it depends on how logError prints internally. Mocking @/telemetry's logError (as #422 did with mockLogError) would be more robust and would make it easy to cover the live-query and OAuth sites too.
5. In handleLiveQueryDisconnect, iterating subs.entries() would let the cleanup log include subscriptionId (not just socketId), like the other live-query messages.
6. handlers.ts imports from '../telemetry', while the rest of this PR uses '@/telemetry'.
|
Thanks for the thorough review and actionable guidance, @omegascorp! I have updated the PR to address all feedback and merged latest main: How Review Feedback Was Addressed
Merge Conflict Resolution
Verification:
|
omegascorp
left a comment
There was a problem hiding this comment.
Thanks for the thorough follow-up, all six points are addressed and the auth/live-query/telemetry suites pass on the branch.
Blocking: this now conflicts with main. #456 added src/live-query/handlers.test.ts with two authenticate(..., { persistNewSession: false }) tests. Please merge main and keep both sets of tests in one file (one socket helper, and keep your @/telemetry mock; main's beforeEach rejects runLiveMethod, so without the mock those tests print to the console).
Description: please update it. verification.ts, resetPassword.ts, github.ts and google.ts were already migrated on main and aren't in this diff anymore. Please also add the MODELENCE_LOG_LEVEL note.
Nits:
- In the fetch-error test,
await vi.waitFor(...)would be more reliable thansetTimeout(resolve, 10). - "Error cleaning up existing subscription" and "Error cleaning up after disconnect during setup" have no test yet.
|
Thanks @omegascorp! I've merged
|
Resolves #294
Summary
Replaces raw
console.errorcalls across production authentication and realtime live-query paths with structuredlogError(message, { ...context, error })from the telemetry module.This complements #422 (which migrated server paths) by completing the remaining unmigrated production call sites:
packages/modelence/src/live-query/handlers.ts(subscription initializers, data fetch errors, cleanup errors on unsubscribe, and disconnect handlers)packages/modelence/src/auth/magicLink.tspackages/modelence/src/auth/providers/oauthCommon.tspackages/modelence/src/live-query/handlers.test.tscovering error logging on fetch failure, method rejection, cleanup errors, disconnect during setup, and session preservation.(Note:
verification.ts,resetPassword.ts,github.ts, andgoogle.tswere already migrated onmainand are omitted from this diff).Logging Configuration
Structured logging respects the
MODELENCE_LOG_LEVELenvironment variable (e.g.,MODELENCE_LOG_LEVEL=debugorMODELENCE_LOG_LEVEL=error). When set to a level above the emitted event, structured logs are suppressed accordingly.Verification
npm --prefix packages/modelence run test -- src/live-query/ src/telemetry/ src/auth/(36 files / 653 tests passed)npm --prefix packages/modelence run lint:check(0 errors, 0 warnings)Note
Low Risk
Observability-only refactor in error paths; behavior and client responses are unchanged aside from richer server logs.
Overview
Replaces remaining
console.errorcalls in live-query subscription handlers and OAuth common withlogErrorfrom@/telemetry, attaching structured fields (method,subscriptionId,socketId,redirectUri,siteUrl, etc.) alongside the error object.Live-query paths now log consistently for fetch failures, method setup failures, and cleanup errors on unsubscribe, disconnect, duplicate subscription IDs, and disconnect-during-setup. OAuth rejection of non-allowlisted mobile
redirectUriand failure to build the web OAuth error redirect use the same pattern instead of string-concatenated console output.Tests were extended or tightened: live-query handler coverage for each error log shape; OAuth/mobile tests assert
mockLogErrorpayloads; magic-link tests drop redundantconsole.errorspies where telemetry mocks already assert behavior.Reviewed by Cursor Bugbot for commit 655c7f4. Bugbot is set up for automated code reviews on this repo. Configure here.