1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-03 17:59:37 +02:00

Refactor server errors handler

This commit is contained in:
Chocobozzz 2021-06-02 18:15:41 +02:00
parent 463206948d
commit e030bfb59d
No known key found for this signature in database
GPG key ID: 583A612D890159BE
20 changed files with 236 additions and 60 deletions

View file

@ -8,7 +8,6 @@ import { isArray } from './custom-validators/misc'
import { logger } from './logger'
import { deleteFileAndCatch, generateRandomString } from './utils'
import { getExtFromMimetype } from './video'
import { ProblemDocument, ProblemDocumentExtension } from 'http-problem-details'
function buildNSFWFilter (res?: express.Response, paramNSFW?: string) {
if (paramNSFW === 'true') return true
@ -126,34 +125,6 @@ function getCountVideos (req: express.Request) {
return req.query.skipCount !== true
}
// helpers added in server.ts and used in subsequent controllers used
const apiResponseHelpers = (req, res: express.Response, next = null) => {
res.fail = (options) => {
const { data, status = HttpStatusCode.BAD_REQUEST_400, message, title, type, docs = res.docs, instance } = options
const extension = new ProblemDocumentExtension({
...data,
docs,
// fields for <= 3.2 compatibility, deprecated
error: message,
code: type
})
res.status(status)
res.setHeader('Content-Type', 'application/problem+json')
res.json(new ProblemDocument({
status,
title,
instance,
// fields intended to replace 'error' and 'code' respectively
detail: message,
type: type && 'https://docs.joinpeertube.org/api-rest-reference.html#section/Errors/' + type
}, extension))
}
if (next) next()
}
// ---------------------------------------------------------------------------
export {
@ -163,6 +134,5 @@ export {
badRequest,
createReqFiles,
cleanUpReqFiles,
getCountVideos,
apiResponseHelpers
getCountVideos
}