Skip to content

Commit

Permalink
fix: crash on error in sessionPublicContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Burtey committed Sep 27, 2023
1 parent 102643b commit 6c6004b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
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

0 comments on commit 6c6004b

Please sign in to comment.