Skip to content

Commit

Permalink
fix(logger): use correct status code in error log message
Browse files Browse the repository at this point in the history
Fix an issue where status code used in error log message was incorrect

fix #33
  • Loading branch information
PunGrumpy committed Apr 8, 2024
1 parent d9a0da4 commit a3ec49c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Elysia from 'elysia'
import { createLogger, handleHttpError } from './logger'
import startString from './utils/start'
import { Server } from 'bun'
import { Options } from './types'
import { HttpError, Options } from './types'

/**
* Creates a logger.
Expand Down Expand Up @@ -35,7 +35,12 @@ export const logger = (options?: Options): Elysia => {
log.log('INFO', request, { status: 200 }, store as { beforeTime: bigint })
})
.onError({ as: 'global' }, ({ request, error, store }) => {
handleHttpError(request, error, store as { beforeTime: bigint })
handleHttpError(
request,
error as HttpError,
store as { beforeTime: bigint },
options
)
})

return elysia
Expand Down
6 changes: 3 additions & 3 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ export const createLogger = (options?: Options): Logger => ({
* Handles an HTTP error.
*
* @param {RequestInfo} request The request.
* @param {Error} error The error.
* @param {HttpError} error The HTTP error.
* @param {StoreData} store The store data.
* @param {Options} options The options.
*/
export const handleHttpError = (
request: RequestInfo,
error: Error,
error: HttpError,
store: StoreData,
options?: Options
): void => {
const statusCode = error instanceof HttpError ? error.status : 500
const statusCode = error.status || 500
const logMessage = buildLogMessage(
'ERROR',
request,
Expand Down

0 comments on commit a3ec49c

Please sign in to comment.