Merge pull request #4471 from specklesystems/iain/incorporate-review-comments-withoperationlogging
chore(server/logging): simplify withOperationLogging parameters
This commit is contained in:
@@ -29,10 +29,7 @@ import {
|
||||
OperationStatus,
|
||||
stripeEventId
|
||||
} from '@/observability/domain/fields'
|
||||
import {
|
||||
logErrorThenThrow,
|
||||
withOperationLogging
|
||||
} from '@/observability/domain/businessLogging'
|
||||
import { withOperationLogging } from '@/observability/domain/businessLogging'
|
||||
|
||||
export const getBillingRouter = (): Router => {
|
||||
const router = Router()
|
||||
@@ -121,44 +118,50 @@ export const getBillingRouter = (): Router => {
|
||||
logger.info(OperationStatus.start, '[{operationName} ({operationStatus})] ')
|
||||
|
||||
await withOperationLogging(
|
||||
async () =>
|
||||
await withTransaction(
|
||||
async ({ db }) => {
|
||||
const completeCheckout = completeCheckoutSessionFactory({
|
||||
getCheckoutSession: getCheckoutSessionFactory({ db }),
|
||||
updateCheckoutSessionStatus: updateCheckoutSessionStatusFactory({
|
||||
db
|
||||
}),
|
||||
upsertPaidWorkspacePlan: upsertPaidWorkspacePlanFactory({ db }),
|
||||
upsertWorkspaceSubscription: upsertWorkspaceSubscriptionFactory({
|
||||
db
|
||||
}),
|
||||
getSubscriptionData: getSubscriptionDataFactory({
|
||||
stripe
|
||||
}),
|
||||
emitEvent: getEventBus().emit
|
||||
})
|
||||
async () => {
|
||||
try {
|
||||
await withTransaction(
|
||||
async ({ db }) => {
|
||||
const completeCheckout = completeCheckoutSessionFactory({
|
||||
getCheckoutSession: getCheckoutSessionFactory({ db }),
|
||||
updateCheckoutSessionStatus: updateCheckoutSessionStatusFactory(
|
||||
{
|
||||
db
|
||||
}
|
||||
),
|
||||
upsertPaidWorkspacePlan: upsertPaidWorkspacePlanFactory({ db }),
|
||||
upsertWorkspaceSubscription: upsertWorkspaceSubscriptionFactory(
|
||||
{
|
||||
db
|
||||
}
|
||||
),
|
||||
getSubscriptionData: getSubscriptionDataFactory({
|
||||
stripe
|
||||
}),
|
||||
emitEvent: getEventBus().emit
|
||||
})
|
||||
|
||||
return completeCheckout({
|
||||
sessionId: session.id,
|
||||
subscriptionId
|
||||
})
|
||||
},
|
||||
{ db }
|
||||
),
|
||||
return completeCheckout({
|
||||
sessionId: session.id,
|
||||
subscriptionId
|
||||
})
|
||||
},
|
||||
{ db }
|
||||
)
|
||||
} catch (e) {
|
||||
if (e instanceof WorkspaceAlreadyPaidError) {
|
||||
// ignore the request, this is prob a replay from stripe
|
||||
logger.info('Workspace is already paid, ignoring')
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
logger,
|
||||
operationName: 'completeCheckoutSession',
|
||||
operationDescription:
|
||||
'Payment succeeded or Stripe session completed, and payment was paid',
|
||||
errorHandler: async (err, logger) => {
|
||||
if (err instanceof WorkspaceAlreadyPaidError) {
|
||||
// ignore the request, this is prob a replay from stripe
|
||||
logger.info('Workspace is already paid, ignoring')
|
||||
} else {
|
||||
logErrorThenThrow(err, logger)
|
||||
}
|
||||
}
|
||||
'Payment succeeded or Stripe session completed, and payment was paid'
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -5,12 +5,6 @@ import {
|
||||
OperationStatus
|
||||
} from '@/observability/domain/fields'
|
||||
import { logWithErr } from '@/observability/utils/logLevels'
|
||||
import { MaybeAsync } from '@speckle/shared'
|
||||
|
||||
export const logErrorThenThrow = (err: unknown, logger: Logger) => {
|
||||
logWithErr(logger, err, OperationStatus.failure, OperationLogLinePrefix)
|
||||
throw err
|
||||
}
|
||||
|
||||
/**
|
||||
* @description withOperationLogging is intended to be used for adding observability to high-level 'business' operations
|
||||
@@ -26,11 +20,9 @@ export const withOperationLogging = async <T>(
|
||||
logger: Logger
|
||||
operationName: string
|
||||
operationDescription?: string
|
||||
errorHandler?: (err: unknown, logger: Logger) => MaybeAsync<T>
|
||||
}
|
||||
): Promise<T> => {
|
||||
const { operationName, operationDescription } = params
|
||||
const errorHandler = params.errorHandler || logErrorThenThrow
|
||||
const logger = params.logger.child(OperationName(operationName))
|
||||
|
||||
try {
|
||||
@@ -44,6 +36,7 @@ export const withOperationLogging = async <T>(
|
||||
logger.info(OperationStatus.success, OperationLogLinePrefix)
|
||||
return results
|
||||
} catch (err) {
|
||||
return await errorHandler(err, logger)
|
||||
logWithErr(logger, err, OperationStatus.failure, OperationLogLinePrefix)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user