logixlysia 2.2.2 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,16 @@
1
+ # Required, otherwise ggshield considers the file to use the deprecated v1 format
2
+ version: 2
3
+
4
+ # Set to true if the desired exit code for the CLI is always 0, otherwise the
5
+ # exit code will be 1 if incidents are found.
6
+ exit-zero: false
7
+
8
+ verbose: false
9
+
10
+ instance: https://dashboard.gitguardian.com
11
+
12
+ # Maximum commits to scan in a hook.
13
+ max-commits-for-hook: 50
14
+
15
+ # Accept self-signed certificates for the API.
16
+ allow-self-signed: false
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # Changelog
2
2
 
3
+ ## [2.3.0](https://github.com/PunGrumpy/logixlysia/compare/v2.2.3...v2.3.0) (2024-04-06)
4
+
5
+
6
+ ### Features
7
+
8
+ * **logger:** handle HTTP errors and log with appropriate status code ([276b3c4](https://github.com/PunGrumpy/logixlysia/commit/276b3c41334bcf1cefd032ae11f71fb8cda2bc9a)), closes [#24](https://github.com/PunGrumpy/logixlysia/issues/24)
9
+
10
+ ## [2.2.3](https://github.com/PunGrumpy/logixlysia/compare/v2.2.2...v2.2.3) (2024-04-06)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * **error-log:** add global to `onError` ([e217baa](https://github.com/PunGrumpy/logixlysia/commit/e217baad9f72fa3e866f959f371768ed2ed22e33))
16
+ * **ts2488:** that method return an iterator ([35da93f](https://github.com/PunGrumpy/logixlysia/commit/35da93f75e34ff212ddd31ff951425551f31c764))
17
+
3
18
  ## [2.2.2](https://github.com/PunGrumpy/logixlysia/compare/v2.2.1...v2.2.2) (2024-04-06)
4
19
 
5
20
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "logixlysia",
3
- "version": "2.2.2",
3
+ "version": "2.3.0",
4
4
  "description": "🦊 Logixlysia is a logger for Elysia",
5
5
  "type": "module",
6
6
  "module": "src/index.ts",
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import Elysia from 'elysia'
2
- import { createLogger } from './logger'
2
+ import { createLogger, handleHttpError } from './logger'
3
3
  import startString from './utils/start'
4
4
  import { Server } from 'bun'
5
5
  import { Options } from './options'
@@ -50,8 +50,9 @@ export const logger = (options?: Options): Elysia => {
50
50
  store as { beforeTime: bigint }
51
51
  )
52
52
  })
53
- .onError(({ request, error, store }) => {
54
- log.log('ERROR', request, error, store as { beforeTime: bigint })
53
+ .onError({ as: 'global' }, ({ request, error, store }) => {
54
+ // log.log('ERROR', request, error, store as { beforeTime: bigint })
55
+ handleHttpError(request, error, store as { beforeTime: bigint })
55
56
  })
56
57
 
57
58
  return elysia
package/src/logger.ts CHANGED
@@ -7,6 +7,7 @@ import statusString from './utils/status'
7
7
  import { RequestInfo } from './types/RequestInfo'
8
8
  import { LogData, LogLevel, Logger } from './types/Logger'
9
9
  import { StoreData } from './types/StoreData'
10
+ import { HttpError } from './types/HttpError'
10
11
 
11
12
  /**
12
13
  * Asynchronously logs a message constructed from various log components.
@@ -88,3 +89,25 @@ function writeToLogAsync(message: string): Promise<void> {
88
89
  export const createLogger = (): Logger => ({
89
90
  log: (level, request, data, store) => log(level, request, data, store)
90
91
  })
92
+
93
+ /**
94
+ * Handle HTTP errors and log them with the appropriate status code.
95
+ *
96
+ * @param {RequestInfo} request - The request information.
97
+ * @param {Error} error - The error object.
98
+ * @param {StoreData} store - The store data.
99
+ */
100
+ export const handleHttpError = (
101
+ request: RequestInfo,
102
+ error: Error,
103
+ store: StoreData
104
+ ): void => {
105
+ const statusCode = error instanceof HttpError ? error.status : 500
106
+ const logMessage = buildLogMessage(
107
+ 'ERROR',
108
+ request,
109
+ { status: statusCode },
110
+ store
111
+ )
112
+ console.error(logMessage)
113
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Custom Error class for HTTP errors.
3
+ */
4
+ export class HttpError extends Error {
5
+ status: number
6
+
7
+ constructor(status: number, message: string) {
8
+ super(message)
9
+ this.status = status
10
+ }
11
+ }
@@ -28,7 +28,9 @@ describe('Start String', () => {
28
28
  const expectedMessage = `🦊 Elysia is running at http://localhost:3000`
29
29
 
30
30
  // Extract the arguments passed to console.log during the function call
31
- const [[logMessage]] = mockConsoleLog.mock.calls
31
+ const logMessage = mockConsoleLog.mock.calls
32
+ .map((args: any[]) => args.join(' '))
33
+ .join(' ')
32
34
 
33
35
  expect(logMessage).toMatch(expectedMessage)
34
36
  })