Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: crash on error in sessionPublicContext #3271

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/servers/graphql-main-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ACCOUNT_USERNAME,
SemanticAttributes,
addAttributesToCurrentSpanAndPropagate,
recordExceptionInCurrentSpan,
} from "@services/tracing"

import { NextFunction, Request, Response } from "express"
Expand Down Expand Up @@ -45,6 +46,15 @@ const setGqlContext = async (
ip,
})

if (gqlContext instanceof Error) {
recordExceptionInCurrentSpan({
error: gqlContext,
fallbackMsg: "error executing sessionPublicContext",
})
next(gqlContext)
return
}

req.gqlContext = gqlContext

return addAttributesToCurrentSpanAndPropagate(
Expand Down
8 changes: 3 additions & 5 deletions src/servers/middlewares/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { Accounts, Transactions } from "@app"
import { recordExceptionInCurrentSpan } from "@services/tracing"
import jsonwebtoken from "jsonwebtoken"

import { mapError } from "@graphql/error-map"

import { maybeExtendSession } from "@app/authentication"
import { checkedToUserId } from "@domain/accounts"
import { ValidationError } from "@domain/shared"
Expand All @@ -18,7 +16,7 @@ export const sessionPublicContext = async ({
}: {
tokenPayload: jsonwebtoken.JwtPayload
ip: IpAddress | undefined
}): Promise<GraphQLPublicContext> => {
}): Promise<GraphQLPublicContext | Error> => {
const logger = baseLogger.child({ tokenPayload })

let domainAccount: Account | undefined
Expand All @@ -35,7 +33,7 @@ export const sessionPublicContext = async ({
const userId = maybeUserId
const account = await Accounts.getAccountFromUserId(userId)
if (account instanceof Error) {
throw mapError(account)
return account
} else {
domainAccount = account
// not awaiting on purpose. just updating metadata
Expand All @@ -51,7 +49,7 @@ export const sessionPublicContext = async ({
}

const userRes = await UsersRepository().findById(account.kratosUserId)
if (userRes instanceof Error) throw mapError(userRes)
if (userRes instanceof Error) return userRes
user = userRes
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/servers/ws-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@ const getContext = async (
sub: kratosCookieRes.kratosUserId,
}

return sessionPublicContext({
const context = await sessionPublicContext({
tokenPayload,
ip,
})

if (context instanceof Error) throw context
return context
}

const kratosToken = authz?.slice(7) as AuthToken
Expand All @@ -106,10 +109,13 @@ const getContext = async (
return false
}

return sessionPublicContext({
const context = await sessionPublicContext({
tokenPayload,
ip,
})

if (context instanceof Error) throw context
return context
},
})()
}
Expand Down
Loading