@naturalcycles/backend-lib 9.77.0 → 9.77.1
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.
|
@@ -53,7 +53,8 @@ export function respondWithError(req, res, err) {
|
|
|
53
53
|
const httpError = _errorLikeToErrorObject(originalError);
|
|
54
54
|
if (!includeErrorStack)
|
|
55
55
|
delete httpError.stack;
|
|
56
|
-
|
|
56
|
+
// http-errors (express itself, its body parsers) carry the status they should be answered with
|
|
57
|
+
httpError.data.backendResponseStatusCode ||= getHttpErrorStatusCode(originalError) || 500; // default to 500
|
|
57
58
|
_objectAssign(httpError.data, {
|
|
58
59
|
errorId,
|
|
59
60
|
headersSent: headersSent || undefined,
|
|
@@ -63,3 +64,18 @@ export function respondWithError(req, res, err) {
|
|
|
63
64
|
error: httpError,
|
|
64
65
|
});
|
|
65
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* Errors created by the `http-errors` package (express itself, body-parser, raw-body, send)
|
|
69
|
+
* carry the HTTP status they are meant to be answered with: `status` and `statusCode` (always equal),
|
|
70
|
+
* plus `expose` (whether the message is safe to show to the client).
|
|
71
|
+
* All three together are the marker: other libraries also put a `statusCode` on their errors
|
|
72
|
+
* (e.g a gateway's response status), which says nothing about how we should respond.
|
|
73
|
+
*/
|
|
74
|
+
function getHttpErrorStatusCode(err) {
|
|
75
|
+
const { status, statusCode, expose } = err;
|
|
76
|
+
if (typeof expose !== 'boolean' || typeof statusCode !== 'number' || status !== statusCode)
|
|
77
|
+
return;
|
|
78
|
+
if (!Number.isInteger(statusCode) || statusCode < 400 || statusCode > 599)
|
|
79
|
+
return;
|
|
80
|
+
return statusCode;
|
|
81
|
+
}
|
|
@@ -43,6 +43,6 @@ function buildRequestEndpoint(req, stripPrefix, sanitize) {
|
|
|
43
43
|
// the exact bytes are not worth preserving here, `req.url` still has them.
|
|
44
44
|
const UNSAFE_PATH_CHAR = /[^\w\-./:@%!$*+,;=~]/gu;
|
|
45
45
|
function sanitizePath(path) {
|
|
46
|
-
return path.
|
|
46
|
+
return path.replaceAll(UNSAFE_PATH_CHAR, '_');
|
|
47
47
|
}
|
|
48
48
|
const identity = (path) => path;
|
package/package.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { BackendErrorResponseObject, ErrorObject } from '@naturalcycles/js-lib/error'
|
|
2
2
|
import { _anyToError, _errorLikeToErrorObject } from '@naturalcycles/js-lib/error'
|
|
3
|
+
import type { HttpStatusCode } from '@naturalcycles/js-lib/http'
|
|
3
4
|
import { _objectAssign } from '@naturalcycles/js-lib/types'
|
|
4
5
|
import type { BackendErrorRequestHandler, BackendRequest, BackendResponse } from './server.model.js'
|
|
5
6
|
|
|
@@ -89,7 +90,8 @@ export function respondWithError(req: BackendRequest, res: BackendResponse, err:
|
|
|
89
90
|
const httpError = _errorLikeToErrorObject(originalError)
|
|
90
91
|
if (!includeErrorStack) delete httpError.stack
|
|
91
92
|
|
|
92
|
-
|
|
93
|
+
// http-errors (express itself, its body parsers) carry the status they should be answered with
|
|
94
|
+
httpError.data.backendResponseStatusCode ||= getHttpErrorStatusCode(originalError) || 500 // default to 500
|
|
93
95
|
_objectAssign(httpError.data, {
|
|
94
96
|
errorId,
|
|
95
97
|
headersSent: headersSent || undefined,
|
|
@@ -101,3 +103,23 @@ export function respondWithError(req: BackendRequest, res: BackendResponse, err:
|
|
|
101
103
|
error: httpError,
|
|
102
104
|
} satisfies BackendErrorResponseObject)
|
|
103
105
|
}
|
|
106
|
+
|
|
107
|
+
interface HttpErrorLike {
|
|
108
|
+
status: number
|
|
109
|
+
statusCode: number
|
|
110
|
+
expose: boolean
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Errors created by the `http-errors` package (express itself, body-parser, raw-body, send)
|
|
115
|
+
* carry the HTTP status they are meant to be answered with: `status` and `statusCode` (always equal),
|
|
116
|
+
* plus `expose` (whether the message is safe to show to the client).
|
|
117
|
+
* All three together are the marker: other libraries also put a `statusCode` on their errors
|
|
118
|
+
* (e.g a gateway's response status), which says nothing about how we should respond.
|
|
119
|
+
*/
|
|
120
|
+
function getHttpErrorStatusCode(err: Error): HttpStatusCode | undefined {
|
|
121
|
+
const { status, statusCode, expose } = err as Partial<HttpErrorLike>
|
|
122
|
+
if (typeof expose !== 'boolean' || typeof statusCode !== 'number' || status !== statusCode) return
|
|
123
|
+
if (!Number.isInteger(statusCode) || statusCode < 400 || statusCode > 599) return
|
|
124
|
+
return statusCode
|
|
125
|
+
}
|
|
@@ -55,7 +55,7 @@ function buildRequestEndpoint(
|
|
|
55
55
|
const UNSAFE_PATH_CHAR = /[^\w\-./:@%!$*+,;=~]/gu
|
|
56
56
|
|
|
57
57
|
function sanitizePath(path: string): string {
|
|
58
|
-
return path.
|
|
58
|
+
return path.replaceAll(UNSAFE_PATH_CHAR, '_')
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
const identity = (path: string): string => path
|