diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2021-06-01 01:36:53 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2021-06-02 16:57:07 +0200 |
commit | 76148b27f7501bac061992136852be4303370c8d (patch) | |
tree | fc0559253e833c9252fa14ebaec5321d88bfb4e8 /server/middlewares/validators/redundancy.ts | |
parent | 5ed25fb76e920dac364cb9ef46f14ec4bd372949 (diff) | |
download | PeerTube-76148b27f7501bac061992136852be4303370c8d.tar.gz PeerTube-76148b27f7501bac061992136852be4303370c8d.tar.zst PeerTube-76148b27f7501bac061992136852be4303370c8d.zip |
refactor API errors to standard error format
Diffstat (limited to 'server/middlewares/validators/redundancy.ts')
-rw-r--r-- | server/middlewares/validators/redundancy.ts | 57 |
1 files changed, 38 insertions, 19 deletions
diff --git a/server/middlewares/validators/redundancy.ts b/server/middlewares/validators/redundancy.ts index c379aebe4..3d557048a 100644 --- a/server/middlewares/validators/redundancy.ts +++ b/server/middlewares/validators/redundancy.ts | |||
@@ -35,11 +35,21 @@ const videoFileRedundancyGetValidator = [ | |||
35 | return f.resolution === paramResolution && (!req.params.fps || paramFPS) | 35 | return f.resolution === paramResolution && (!req.params.fps || paramFPS) |
36 | }) | 36 | }) |
37 | 37 | ||
38 | if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video file not found.' }) | 38 | if (!videoFile) { |
39 | return res.fail({ | ||
40 | status: HttpStatusCode.NOT_FOUND_404, | ||
41 | message: 'Video file not found.' | ||
42 | }) | ||
43 | } | ||
39 | res.locals.videoFile = videoFile | 44 | res.locals.videoFile = videoFile |
40 | 45 | ||
41 | const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id) | 46 | const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id) |
42 | if (!videoRedundancy) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video redundancy not found.' }) | 47 | if (!videoRedundancy) { |
48 | return res.fail({ | ||
49 | status: HttpStatusCode.NOT_FOUND_404, | ||
50 | message: 'Video redundancy not found.' | ||
51 | }) | ||
52 | } | ||
43 | res.locals.videoRedundancy = videoRedundancy | 53 | res.locals.videoRedundancy = videoRedundancy |
44 | 54 | ||
45 | return next() | 55 | return next() |
@@ -65,11 +75,21 @@ const videoPlaylistRedundancyGetValidator = [ | |||
65 | const paramPlaylistType = req.params.streamingPlaylistType as unknown as number // We casted to int above | 75 | const paramPlaylistType = req.params.streamingPlaylistType as unknown as number // We casted to int above |
66 | const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p.type === paramPlaylistType) | 76 | const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p.type === paramPlaylistType) |
67 | 77 | ||
68 | if (!videoStreamingPlaylist) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video playlist not found.' }) | 78 | if (!videoStreamingPlaylist) { |
79 | return res.fail({ | ||
80 | status: HttpStatusCode.NOT_FOUND_404, | ||
81 | message: 'Video playlist not found.' | ||
82 | }) | ||
83 | } | ||
69 | res.locals.videoStreamingPlaylist = videoStreamingPlaylist | 84 | res.locals.videoStreamingPlaylist = videoStreamingPlaylist |
70 | 85 | ||
71 | const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id) | 86 | const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id) |
72 | if (!videoRedundancy) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video redundancy not found.' }) | 87 | if (!videoRedundancy) { |
88 | return res.fail({ | ||
89 | status: HttpStatusCode.NOT_FOUND_404, | ||
90 | message: 'Video redundancy not found.' | ||
91 | }) | ||
92 | } | ||
73 | res.locals.videoRedundancy = videoRedundancy | 93 | res.locals.videoRedundancy = videoRedundancy |
74 | 94 | ||
75 | return next() | 95 | return next() |
@@ -90,12 +110,10 @@ const updateServerRedundancyValidator = [ | |||
90 | const server = await ServerModel.loadByHost(req.params.host) | 110 | const server = await ServerModel.loadByHost(req.params.host) |
91 | 111 | ||
92 | if (!server) { | 112 | if (!server) { |
93 | return res | 113 | return res.fail({ |
94 | .status(HttpStatusCode.NOT_FOUND_404) | 114 | status: HttpStatusCode.NOT_FOUND_404, |
95 | .json({ | 115 | message: `Server ${req.params.host} not found.` |
96 | error: `Server ${req.params.host} not found.` | 116 | }) |
97 | }) | ||
98 | .end() | ||
99 | } | 117 | } |
100 | 118 | ||
101 | res.locals.server = server | 119 | res.locals.server = server |
@@ -129,19 +147,19 @@ const addVideoRedundancyValidator = [ | |||
129 | if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return | 147 | if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return |
130 | 148 | ||
131 | if (res.locals.onlyVideo.remote === false) { | 149 | if (res.locals.onlyVideo.remote === false) { |
132 | return res.status(HttpStatusCode.BAD_REQUEST_400) | 150 | return res.fail({ message: 'Cannot create a redundancy on a local video' }) |
133 | .json({ error: 'Cannot create a redundancy on a local video' }) | ||
134 | } | 151 | } |
135 | 152 | ||
136 | if (res.locals.onlyVideo.isLive) { | 153 | if (res.locals.onlyVideo.isLive) { |
137 | return res.status(HttpStatusCode.BAD_REQUEST_400) | 154 | return res.fail({ message: 'Cannot create a redundancy of a live video' }) |
138 | .json({ error: 'Cannot create a redundancy of a live video' }) | ||
139 | } | 155 | } |
140 | 156 | ||
141 | const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid) | 157 | const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid) |
142 | if (alreadyExists) { | 158 | if (alreadyExists) { |
143 | return res.status(HttpStatusCode.CONFLICT_409) | 159 | return res.fail({ |
144 | .json({ error: 'This video is already duplicated by your instance.' }) | 160 | status: HttpStatusCode.CONFLICT_409, |
161 | message: 'This video is already duplicated by your instance.' | ||
162 | }) | ||
145 | } | 163 | } |
146 | 164 | ||
147 | return next() | 165 | return next() |
@@ -160,9 +178,10 @@ const removeVideoRedundancyValidator = [ | |||
160 | 178 | ||
161 | const redundancy = await VideoRedundancyModel.loadByIdWithVideo(parseInt(req.params.redundancyId, 10)) | 179 | const redundancy = await VideoRedundancyModel.loadByIdWithVideo(parseInt(req.params.redundancyId, 10)) |
162 | if (!redundancy) { | 180 | if (!redundancy) { |
163 | return res.status(HttpStatusCode.NOT_FOUND_404) | 181 | return res.fail({ |
164 | .json({ error: 'Video redundancy not found' }) | 182 | status: HttpStatusCode.NOT_FOUND_404, |
165 | .end() | 183 | message: 'Video redundancy not found' |
184 | }) | ||
166 | } | 185 | } |
167 | 186 | ||
168 | res.locals.videoRedundancy = redundancy | 187 | res.locals.videoRedundancy = redundancy |