aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/express-utils.ts
diff options
context:
space:
mode:
authorRigel Kent <sendmemail@rigelk.eu>2021-06-01 01:36:53 +0200
committerChocobozzz <chocobozzz@cpy.re>2021-06-02 16:57:07 +0200
commit76148b27f7501bac061992136852be4303370c8d (patch)
treefc0559253e833c9252fa14ebaec5321d88bfb4e8 /server/helpers/express-utils.ts
parent5ed25fb76e920dac364cb9ef46f14ec4bd372949 (diff)
downloadPeerTube-76148b27f7501bac061992136852be4303370c8d.tar.gz
PeerTube-76148b27f7501bac061992136852be4303370c8d.tar.zst
PeerTube-76148b27f7501bac061992136852be4303370c8d.zip
refactor API errors to standard error format
Diffstat (limited to 'server/helpers/express-utils.ts')
-rw-r--r--server/helpers/express-utils.ts32
1 files changed, 31 insertions, 1 deletions
diff --git a/server/helpers/express-utils.ts b/server/helpers/express-utils.ts
index 010c6961a..e3ff93cdd 100644
--- a/server/helpers/express-utils.ts
+++ b/server/helpers/express-utils.ts
@@ -8,6 +8,7 @@ import { isArray } from './custom-validators/misc'
8import { logger } from './logger' 8import { logger } from './logger'
9import { deleteFileAndCatch, generateRandomString } from './utils' 9import { deleteFileAndCatch, generateRandomString } from './utils'
10import { getExtFromMimetype } from './video' 10import { getExtFromMimetype } from './video'
11import { ProblemDocument, ProblemDocumentExtension } from 'http-problem-details'
11 12
12function buildNSFWFilter (res?: express.Response, paramNSFW?: string) { 13function buildNSFWFilter (res?: express.Response, paramNSFW?: string) {
13 if (paramNSFW === 'true') return true 14 if (paramNSFW === 'true') return true
@@ -125,6 +126,34 @@ function getCountVideos (req: express.Request) {
125 return req.query.skipCount !== true 126 return req.query.skipCount !== true
126} 127}
127 128
129// helpers added in server.ts and used in subsequent controllers used
130const apiResponseHelpers = (req, res: express.Response, next = null) => {
131 res.fail = (options) => {
132 const { data, status, message, title, type, docs, instance } = {
133 data: null,
134 status: HttpStatusCode.BAD_REQUEST_400,
135 ...options
136 }
137
138 const extension = new ProblemDocumentExtension({
139 ...data,
140 docs: docs || res.docs
141 })
142
143 res.status(status)
144 res.setHeader('Content-Type', 'application/problem+json')
145 res.json(new ProblemDocument({
146 status,
147 title,
148 instance,
149 type: type && '' + type,
150 detail: message
151 }, extension))
152 }
153
154 if (next !== null) next()
155}
156
128// --------------------------------------------------------------------------- 157// ---------------------------------------------------------------------------
129 158
130export { 159export {
@@ -134,5 +163,6 @@ export {
134 badRequest, 163 badRequest,
135 createReqFiles, 164 createReqFiles,
136 cleanUpReqFiles, 165 cleanUpReqFiles,
137 getCountVideos 166 getCountVideos,
167 apiResponseHelpers
138} 168}