aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-02 18:15:41 +0200
committerChocobozzz <me@florianbigard.com>2021-06-02 18:15:41 +0200
commite030bfb59dd5ee65f20a64686ec9b22ca39f70ae (patch)
treec9a439159ef540291e3c030bcaf958b953442147 /server/middlewares
parent463206948d6a9d46e7e68d55c7b763e601ecc870 (diff)
downloadPeerTube-e030bfb59dd5ee65f20a64686ec9b22ca39f70ae.tar.gz
PeerTube-e030bfb59dd5ee65f20a64686ec9b22ca39f70ae.tar.zst
PeerTube-e030bfb59dd5ee65f20a64686ec9b22ca39f70ae.zip
Refactor server errors handler
Diffstat (limited to 'server/middlewares')
-rw-r--r--server/middlewares/doc.ts13
-rw-r--r--server/middlewares/error.ts39
-rw-r--r--server/middlewares/index.ts2
-rw-r--r--server/middlewares/validators/videos/videos.ts6
4 files changed, 54 insertions, 6 deletions
diff --git a/server/middlewares/doc.ts b/server/middlewares/doc.ts
new file mode 100644
index 000000000..aa852cd77
--- /dev/null
+++ b/server/middlewares/doc.ts
@@ -0,0 +1,13 @@
1import * as express from 'express'
2
3function docMiddleware (docUrl: string) {
4 return (req: express.Request, res: express.Response, next: express.NextFunction) => {
5 res.locals.docUrl = docUrl
6
7 if (next) return next()
8 }
9}
10
11export {
12 docMiddleware
13}
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}
diff --git a/server/middlewares/index.ts b/server/middlewares/index.ts
index 3e280e16f..413653dac 100644
--- a/server/middlewares/index.ts
+++ b/server/middlewares/index.ts
@@ -7,4 +7,6 @@ export * from './servers'
7export * from './sort' 7export * from './sort'
8export * from './user-right' 8export * from './user-right'
9export * from './dnt' 9export * from './dnt'
10export * from './error'
11export * from './doc'
10export * from './csp' 12export * from './csp'
diff --git a/server/middlewares/validators/videos/videos.ts b/server/middlewares/validators/videos/videos.ts
index 64e09234e..b7a9bcbe3 100644
--- a/server/middlewares/validators/videos/videos.ts
+++ b/server/middlewares/validators/videos/videos.ts
@@ -73,7 +73,6 @@ const videosAddLegacyValidator = getCommonVideoEditAttributes().concat([
73 .custom(isIdValid).withMessage('Should have correct video channel id'), 73 .custom(isIdValid).withMessage('Should have correct video channel id'),
74 74
75 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 75 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
76 res.docs = "https://docs.joinpeertube.org/api-rest-reference.html#operation/uploadLegacy"
77 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files }) 76 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
78 77
79 if (areValidationErrors(req, res)) return cleanUpReqFiles(req) 78 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
@@ -108,7 +107,6 @@ const videosAddLegacyValidator = getCommonVideoEditAttributes().concat([
108 */ 107 */
109const videosAddResumableValidator = [ 108const videosAddResumableValidator = [
110 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 109 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
111 res.docs = "https://docs.joinpeertube.org/api-rest-reference.html#operation/uploadResumable"
112 const user = res.locals.oauth.token.User 110 const user = res.locals.oauth.token.User
113 111
114 const body: express.CustomUploadXFile<express.UploadXFileMetadata> = req.body 112 const body: express.CustomUploadXFile<express.UploadXFileMetadata> = req.body
@@ -170,7 +168,6 @@ const videosAddResumableInitValidator = getCommonVideoEditAttributes().concat([
170 .withMessage('Should specify the file mimetype'), 168 .withMessage('Should specify the file mimetype'),
171 169
172 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 170 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
173 res.docs = "https://docs.joinpeertube.org/api-rest-reference.html#operation/uploadResumableInit"
174 const videoFileMetadata = { 171 const videoFileMetadata = {
175 mimetype: req.headers['x-upload-content-type'] as string, 172 mimetype: req.headers['x-upload-content-type'] as string,
176 size: +req.headers['x-upload-content-length'], 173 size: +req.headers['x-upload-content-length'],
@@ -214,7 +211,6 @@ const videosUpdateValidator = getCommonVideoEditAttributes().concat([
214 .custom(isIdValid).withMessage('Should have correct video channel id'), 211 .custom(isIdValid).withMessage('Should have correct video channel id'),
215 212
216 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 213 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
217 res.docs = 'https://docs.joinpeertube.org/api-rest-reference.html#operation/putVideo'
218 logger.debug('Checking videosUpdate parameters', { parameters: req.body }) 214 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
219 215
220 if (areValidationErrors(req, res)) return cleanUpReqFiles(req) 216 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
@@ -268,7 +264,6 @@ const videosCustomGetValidator = (
268 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), 264 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
269 265
270 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 266 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
271 res.docs = 'https://docs.joinpeertube.org/api-rest-reference.html#operation/getVideo'
272 logger.debug('Checking videosGet parameters', { parameters: req.params }) 267 logger.debug('Checking videosGet parameters', { parameters: req.params })
273 268
274 if (areValidationErrors(req, res)) return 269 if (areValidationErrors(req, res)) return
@@ -334,7 +329,6 @@ const videosRemoveValidator = [
334 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), 329 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
335 330
336 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 331 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
337 res.docs = "https://docs.joinpeertube.org/api-rest-reference.html#operation/delVideo"
338 logger.debug('Checking videosRemove parameters', { parameters: req.params }) 332 logger.debug('Checking videosRemove parameters', { parameters: req.params })
339 333
340 if (areValidationErrors(req, res)) return 334 if (areValidationErrors(req, res)) return