aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/error.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/middlewares/error.ts')
-rw-r--r--server/middlewares/error.ts39
1 files changed, 39 insertions, 0 deletions
diff --git a/server/middlewares/error.ts b/server/middlewares/error.ts
new file mode 100644
index 000000000..e3eb1c8f5
--- /dev/null
+++ b/server/middlewares/error.ts
@@ -0,0 +1,39 @@
1import * as express from 'express'
2import { ProblemDocument, ProblemDocumentExtension } from 'http-problem-details'
3import { HttpStatusCode } from '@shared/core-utils'
4
5function apiFailMiddleware (req: express.Request, res: express.Response, next: express.NextFunction) {
6 res.fail = options => {
7 const { status = HttpStatusCode.BAD_REQUEST_400, message, title, type, data, instance } = options
8
9 const extension = new ProblemDocumentExtension({
10 ...data,
11
12 docs: res.locals.docUrl,
13 code: type,
14
15 // For <= 3.2 compatibility
16 error: message
17 })
18
19 res.status(status)
20 res.setHeader('Content-Type', 'application/problem+json')
21 res.json(new ProblemDocument({
22 status,
23 title,
24 instance,
25
26 detail: message,
27
28 type: type
29 ? `https://docs.joinpeertube.org/api-rest-reference.html#section/Errors/${type}`
30 : undefined
31 }, extension))
32 }
33
34 if (next) next()
35}
36
37export {
38 apiFailMiddleware
39}