From 76148b27f7501bac061992136852be4303370c8d Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Tue, 1 Jun 2021 01:36:53 +0200 Subject: refactor API errors to standard error format --- server/middlewares/validators/redundancy.ts | 57 +++++++++++++++++++---------- 1 file changed, 38 insertions(+), 19 deletions(-) (limited to 'server/middlewares/validators/redundancy.ts') 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 = [ return f.resolution === paramResolution && (!req.params.fps || paramFPS) }) - if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video file not found.' }) + if (!videoFile) { + return res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Video file not found.' + }) + } res.locals.videoFile = videoFile const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id) - if (!videoRedundancy) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video redundancy not found.' }) + if (!videoRedundancy) { + return res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Video redundancy not found.' + }) + } res.locals.videoRedundancy = videoRedundancy return next() @@ -65,11 +75,21 @@ const videoPlaylistRedundancyGetValidator = [ const paramPlaylistType = req.params.streamingPlaylistType as unknown as number // We casted to int above const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p.type === paramPlaylistType) - if (!videoStreamingPlaylist) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video playlist not found.' }) + if (!videoStreamingPlaylist) { + return res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Video playlist not found.' + }) + } res.locals.videoStreamingPlaylist = videoStreamingPlaylist const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id) - if (!videoRedundancy) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video redundancy not found.' }) + if (!videoRedundancy) { + return res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Video redundancy not found.' + }) + } res.locals.videoRedundancy = videoRedundancy return next() @@ -90,12 +110,10 @@ const updateServerRedundancyValidator = [ const server = await ServerModel.loadByHost(req.params.host) if (!server) { - return res - .status(HttpStatusCode.NOT_FOUND_404) - .json({ - error: `Server ${req.params.host} not found.` - }) - .end() + return res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: `Server ${req.params.host} not found.` + }) } res.locals.server = server @@ -129,19 +147,19 @@ const addVideoRedundancyValidator = [ if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return if (res.locals.onlyVideo.remote === false) { - return res.status(HttpStatusCode.BAD_REQUEST_400) - .json({ error: 'Cannot create a redundancy on a local video' }) + return res.fail({ message: 'Cannot create a redundancy on a local video' }) } if (res.locals.onlyVideo.isLive) { - return res.status(HttpStatusCode.BAD_REQUEST_400) - .json({ error: 'Cannot create a redundancy of a live video' }) + return res.fail({ message: 'Cannot create a redundancy of a live video' }) } const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid) if (alreadyExists) { - return res.status(HttpStatusCode.CONFLICT_409) - .json({ error: 'This video is already duplicated by your instance.' }) + return res.fail({ + status: HttpStatusCode.CONFLICT_409, + message: 'This video is already duplicated by your instance.' + }) } return next() @@ -160,9 +178,10 @@ const removeVideoRedundancyValidator = [ const redundancy = await VideoRedundancyModel.loadByIdWithVideo(parseInt(req.params.redundancyId, 10)) if (!redundancy) { - return res.status(HttpStatusCode.NOT_FOUND_404) - .json({ error: 'Video redundancy not found' }) - .end() + return res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Video redundancy not found' + }) } res.locals.videoRedundancy = redundancy -- cgit v1.2.3