]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/error.ts
Prevent caption listing of private videos
[github/Chocobozzz/PeerTube.git] / server / middlewares / error.ts
CommitLineData
41fb13c3 1import express from 'express'
e030bfb5 2import { ProblemDocument, ProblemDocumentExtension } from 'http-problem-details'
c0e8b12e 3import { HttpStatusCode } from '@shared/models'
e030bfb5
C
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}