Skip to content

Commit

Permalink
fix(server): properly log thrown errors (#3072)
Browse files Browse the repository at this point in the history
  • Loading branch information
iainsproat authored Sep 25, 2024
1 parent 8dc505d commit 0c9aba6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
16 changes: 11 additions & 5 deletions packages/server/logging/expressLogging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import pino from 'pino'
import type { SerializedResponse } from 'pino'
import type { GenReqId } from 'pino-http'
import type { IncomingMessage, ServerResponse } from 'http'
import type { Optional } from '@speckle/shared'
import { ensureError, type Optional } from '@speckle/shared'
import { getRequestPath } from '@/modules/core/helpers/server'
import { get } from 'lodash'

Expand Down Expand Up @@ -79,31 +79,37 @@ export const LoggingExpressMiddleware = HttpLogger({

customSuccessObject(req, res, val: Record<string, unknown>) {
const isCompleted = !req.readableAborted && res.writableEnded
const requestStatus = isCompleted ? 'completed' : 'aborted'
const isError = !!req.context?.err
const requestStatus = isCompleted ? (isError ? 'errored' : 'completed') : 'aborted'
const requestPath = getRequestPath(req) || 'unknown'
const country = req.headers['cf-ipcountry'] as Optional<string>

return {
...val,
requestStatus,
requestPath,
country
country,
err: req.context?.err
}
},

customErrorMessage() {
return '{requestPath} request {requestStatus} in {responseTime} ms'
},
customErrorObject(req, _res, _err, val: Record<string, unknown>) {
customErrorObject(req, _res, err, val: Record<string, unknown>) {
const requestStatus = 'failed'
const requestPath = getRequestPath(req) || 'unknown'
const country = req.headers['cf-ipcountry'] as Optional<string>
let e: Error | undefined = undefined
if (err) e = ensureError(err)
if (!err && req.context?.err) e = req.context.err

return {
...val,
requestStatus,
requestPath,
country
country,
err: e
}
},

Expand Down
11 changes: 9 additions & 2 deletions packages/server/modules/core/rest/defaultErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,21 @@ const resolveStatusCode = (e: Error): number => {

const resolveErrorInfo = (e: Error): Record<string, unknown> => {
const cause = getCause(e)
const message = e.message
let info = undefined
if (e instanceof BaseError) {
info = e.info()
}

return {
message: e.message,
message,
code: (e instanceof BaseError ? e.info().code : get(e, 'code')) || e.name,
...(isDevEnv()
? {
stack: e.stack,
...(e instanceof BaseError
? {
info: e.info(),
info,
stack: VError.fullStack(e)
}
: {}),
Expand All @@ -49,6 +54,8 @@ export const defaultErrorHandler: ErrorRequestHandler = (err, req, res, next) =>
}

const e = ensureError(err)
// Add the error to the request context, this allows it to be logged by pino-http
if (!req.context.err) req.context.err = e
res.status(resolveStatusCode(e)).json({
error: resolveErrorInfo(e)
})
Expand Down

0 comments on commit 0c9aba6

Please sign in to comment.