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(server): log thrown errors #3072

Merged
merged 3 commits into from
Sep 25, 2024
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
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