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/helpers/custom-validators/video-comments.ts | 39 +++++++++------------- server/helpers/custom-validators/video-imports.ts | 8 ++--- .../helpers/custom-validators/video-ownership.ts | 15 +++++---- 3 files changed, 27 insertions(+), 35 deletions(-) (limited to 'server/helpers/custom-validators') diff --git a/server/helpers/custom-validators/video-comments.ts b/server/helpers/custom-validators/video-comments.ts index 8d3ce580e..5c88447ad 100644 --- a/server/helpers/custom-validators/video-comments.ts +++ b/server/helpers/custom-validators/video-comments.ts @@ -16,26 +16,20 @@ async function doesVideoCommentThreadExist (idArg: number | string, video: MVide const videoComment = await VideoCommentModel.loadById(id) if (!videoComment) { - res.status(HttpStatusCode.NOT_FOUND_404) - .json({ error: 'Video comment thread not found' }) - .end() - + res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Video comment thread not found' + }) return false } if (videoComment.videoId !== video.id) { - res.status(HttpStatusCode.BAD_REQUEST_400) - .json({ error: 'Video comment is not associated to this video.' }) - .end() - + res.fail({ message: 'Video comment is not associated to this video.' }) return false } if (videoComment.inReplyToCommentId !== null) { - res.status(HttpStatusCode.BAD_REQUEST_400) - .json({ error: 'Video comment is not a thread.' }) - .end() - + res.fail({ message: 'Video comment is not a thread.' }) return false } @@ -48,18 +42,15 @@ async function doesVideoCommentExist (idArg: number | string, video: MVideoId, r const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id) if (!videoComment) { - res.status(HttpStatusCode.NOT_FOUND_404) - .json({ error: 'Video comment thread not found' }) - .end() - + res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Video comment thread not found' + }) return false } if (videoComment.videoId !== video.id) { - res.status(HttpStatusCode.BAD_REQUEST_400) - .json({ error: 'Video comment is not associated to this video.' }) - .end() - + res.fail({ message: 'Video comment is not associated to this video.' }) return false } @@ -72,14 +63,14 @@ async function doesCommentIdExist (idArg: number | string, res: express.Response const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id) if (!videoComment) { - res.status(HttpStatusCode.NOT_FOUND_404) - .json({ error: 'Video comment thread not found' }) - + res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Video comment thread not found' + }) return false } res.locals.videoCommentFull = videoComment - return true } diff --git a/server/helpers/custom-validators/video-imports.ts b/server/helpers/custom-validators/video-imports.ts index 0063d3337..3ad7a4648 100644 --- a/server/helpers/custom-validators/video-imports.ts +++ b/server/helpers/custom-validators/video-imports.ts @@ -36,10 +36,10 @@ async function doesVideoImportExist (id: number, res: express.Response) { const videoImport = await VideoImportModel.loadAndPopulateVideo(id) if (!videoImport) { - res.status(HttpStatusCode.NOT_FOUND_404) - .json({ error: 'Video import not found' }) - .end() - + res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Video import not found' + }) return false } diff --git a/server/helpers/custom-validators/video-ownership.ts b/server/helpers/custom-validators/video-ownership.ts index ee3cebe10..21a6b7203 100644 --- a/server/helpers/custom-validators/video-ownership.ts +++ b/server/helpers/custom-validators/video-ownership.ts @@ -9,10 +9,10 @@ export async function doesChangeVideoOwnershipExist (idArg: number | string, res const videoChangeOwnership = await VideoChangeOwnershipModel.load(id) if (!videoChangeOwnership) { - res.status(HttpStatusCode.NOT_FOUND_404) - .json({ error: 'Video change ownership not found' }) - .end() - + res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Video change ownership not found' + }) return false } @@ -25,8 +25,9 @@ export function checkUserCanTerminateOwnershipChange (user: MUserId, videoChange return true } - res.status(HttpStatusCode.FORBIDDEN_403) - .json({ error: 'Cannot terminate an ownership change of another user' }) - .end() + res.fail({ + status: HttpStatusCode.FORBIDDEN_403, + message: 'Cannot terminate an ownership change of another user' + }) return false } -- cgit v1.2.3