tangled
alpha
login
or
join now
danabra.mov
/
statusphere-react
forked from
samuel.fm/statusphere-react
0
fork
atom
the statusphere demo reworked into a vite/react app in a monorepo
0
fork
atom
overview
issues
pulls
pipelines
Remove http-statuses (overkill)
Paul Frazee
2 years ago
34a4ccf2
95f92601
+20
-12
3 changed files
expand all
collapse all
unified
split
package.json
src
middleware
errorHandler.ts
requestLogger.ts
-1
package.json
···
25
"dotenv": "^16.4.5",
26
"envalid": "^8.0.0",
27
"express": "^4.19.2",
28
-
"http-status-codes": "^2.3.0",
29
"iron-session": "^8.0.2",
30
"kysely": "^0.27.4",
31
"multiformats": "^9.9.0",
···
25
"dotenv": "^16.4.5",
26
"envalid": "^8.0.0",
27
"express": "^4.19.2",
0
28
"iron-session": "^8.0.2",
29
"kysely": "^0.27.4",
30
"multiformats": "^9.9.0",
+1
-2
src/middleware/errorHandler.ts
···
1
import type { ErrorRequestHandler, RequestHandler } from 'express'
2
-
import { StatusCodes } from 'http-status-codes'
3
4
const unexpectedRequest: RequestHandler = (_req, res) => {
5
-
res.sendStatus(StatusCodes.NOT_FOUND)
6
}
7
8
const addErrorToRequestLog: ErrorRequestHandler = (err, _req, res, next) => {
···
1
import type { ErrorRequestHandler, RequestHandler } from 'express'
0
2
3
const unexpectedRequest: RequestHandler = (_req, res) => {
4
+
res.sendStatus(404)
5
}
6
7
const addErrorToRequestLog: ErrorRequestHandler = (err, _req, res, next) => {
+19
-9
src/middleware/requestLogger.ts
···
1
import { randomUUID } from 'node:crypto'
2
import type { IncomingMessage, ServerResponse } from 'node:http'
3
import type { Request, RequestHandler, Response } from 'express'
4
-
import { StatusCodes, getReasonPhrase } from 'http-status-codes'
5
import type { LevelWithSilent } from 'pino'
6
import { type CustomAttributeKeys, type Options, pinoHttp } from 'pino-http'
7
···
33
customLogLevel,
34
customSuccessMessage,
35
customReceivedMessage: (req) => `request received: ${req.method}`,
36
-
customErrorMessage: (_req, res) => `request errored with status code: ${res.statusCode}`,
0
37
customAttributeKeys,
38
...options,
39
}
···
67
next()
68
}
69
70
-
const customLogLevel = (_req: IncomingMessage, res: ServerResponse<IncomingMessage>, err?: Error): LevelWithSilent => {
71
-
if (err || res.statusCode >= StatusCodes.INTERNAL_SERVER_ERROR) return LogLevel.Error
72
-
if (res.statusCode >= StatusCodes.BAD_REQUEST) return LogLevel.Warn
73
-
if (res.statusCode >= StatusCodes.MULTIPLE_CHOICES) return LogLevel.Silent
0
0
0
0
74
return LogLevel.Info
75
}
76
77
-
const customSuccessMessage = (req: IncomingMessage, res: ServerResponse<IncomingMessage>) => {
78
-
if (res.statusCode === StatusCodes.NOT_FOUND) return getReasonPhrase(StatusCodes.NOT_FOUND)
0
0
0
79
return `${req.method} completed`
80
}
81
82
-
const genReqId = (req: IncomingMessage, res: ServerResponse<IncomingMessage>) => {
0
0
0
83
const existingID = req.id ?? req.headers['x-request-id']
84
if (existingID) return existingID
85
const id = randomUUID()
···
1
import { randomUUID } from 'node:crypto'
2
import type { IncomingMessage, ServerResponse } from 'node:http'
3
import type { Request, RequestHandler, Response } from 'express'
0
4
import type { LevelWithSilent } from 'pino'
5
import { type CustomAttributeKeys, type Options, pinoHttp } from 'pino-http'
6
···
32
customLogLevel,
33
customSuccessMessage,
34
customReceivedMessage: (req) => `request received: ${req.method}`,
35
+
customErrorMessage: (_req, res) =>
36
+
`request errored with status code: ${res.statusCode}`,
37
customAttributeKeys,
38
...options,
39
}
···
67
next()
68
}
69
70
+
const customLogLevel = (
71
+
_req: IncomingMessage,
72
+
res: ServerResponse<IncomingMessage>,
73
+
err?: Error
74
+
): LevelWithSilent => {
75
+
if (err || res.statusCode >= 500) return LogLevel.Error
76
+
if (res.statusCode >= 400) return LogLevel.Warn
77
+
if (res.statusCode >= 300) return LogLevel.Silent
78
return LogLevel.Info
79
}
80
81
+
const customSuccessMessage = (
82
+
req: IncomingMessage,
83
+
res: ServerResponse<IncomingMessage>
84
+
) => {
85
+
if (res.statusCode === 404) return 'Not found'
86
return `${req.method} completed`
87
}
88
89
+
const genReqId = (
90
+
req: IncomingMessage,
91
+
res: ServerResponse<IncomingMessage>
92
+
) => {
93
const existingID = req.id ?? req.headers['x-request-id']
94
if (existingID) return existingID
95
const id = randomUUID()