From 2d53be0267acc49cda46707b885096193a1f4e9c Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Mon, 7 Dec 2020 14:32:36 +0100 Subject: replace numbers with typed http status codes (#3409) --- server/helpers/custom-validators/video-comments.ts | 13 +++++++------ server/helpers/custom-validators/video-imports.ts | 3 ++- server/helpers/custom-validators/video-ownership.ts | 5 +++-- 3 files changed, 12 insertions(+), 9 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 455ff4241..8d3ce580e 100644 --- a/server/helpers/custom-validators/video-comments.ts +++ b/server/helpers/custom-validators/video-comments.ts @@ -3,6 +3,7 @@ import validator from 'validator' import { VideoCommentModel } from '@server/models/video/video-comment' import { CONSTRAINTS_FIELDS } from '../../initializers/constants' import { MVideoId } from '@server/types/models' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' const VIDEO_COMMENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_COMMENTS @@ -15,7 +16,7 @@ async function doesVideoCommentThreadExist (idArg: number | string, video: MVide const videoComment = await VideoCommentModel.loadById(id) if (!videoComment) { - res.status(404) + res.status(HttpStatusCode.NOT_FOUND_404) .json({ error: 'Video comment thread not found' }) .end() @@ -23,7 +24,7 @@ async function doesVideoCommentThreadExist (idArg: number | string, video: MVide } if (videoComment.videoId !== video.id) { - res.status(400) + res.status(HttpStatusCode.BAD_REQUEST_400) .json({ error: 'Video comment is not associated to this video.' }) .end() @@ -31,7 +32,7 @@ async function doesVideoCommentThreadExist (idArg: number | string, video: MVide } if (videoComment.inReplyToCommentId !== null) { - res.status(400) + res.status(HttpStatusCode.BAD_REQUEST_400) .json({ error: 'Video comment is not a thread.' }) .end() @@ -47,7 +48,7 @@ async function doesVideoCommentExist (idArg: number | string, video: MVideoId, r const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id) if (!videoComment) { - res.status(404) + res.status(HttpStatusCode.NOT_FOUND_404) .json({ error: 'Video comment thread not found' }) .end() @@ -55,7 +56,7 @@ async function doesVideoCommentExist (idArg: number | string, video: MVideoId, r } if (videoComment.videoId !== video.id) { - res.status(400) + res.status(HttpStatusCode.BAD_REQUEST_400) .json({ error: 'Video comment is not associated to this video.' }) .end() @@ -71,7 +72,7 @@ async function doesCommentIdExist (idArg: number | string, res: express.Response const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id) if (!videoComment) { - res.status(404) + res.status(HttpStatusCode.NOT_FOUND_404) .json({ error: 'Video comment thread not found' }) return false diff --git a/server/helpers/custom-validators/video-imports.ts b/server/helpers/custom-validators/video-imports.ts index 33a1fa8ab..0063d3337 100644 --- a/server/helpers/custom-validators/video-imports.ts +++ b/server/helpers/custom-validators/video-imports.ts @@ -4,6 +4,7 @@ import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_IMPORT_STATES } from '../../initia import { exists, isFileValid } from './misc' import * as express from 'express' import { VideoImportModel } from '../../models/video/video-import' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function isVideoImportTargetUrlValid (url: string) { const isURLOptions = { @@ -35,7 +36,7 @@ async function doesVideoImportExist (id: number, res: express.Response) { const videoImport = await VideoImportModel.loadAndPopulateVideo(id) if (!videoImport) { - res.status(404) + res.status(HttpStatusCode.NOT_FOUND_404) .json({ error: 'Video import not found' }) .end() diff --git a/server/helpers/custom-validators/video-ownership.ts b/server/helpers/custom-validators/video-ownership.ts index ed5f8cc2f..ee3cebe10 100644 --- a/server/helpers/custom-validators/video-ownership.ts +++ b/server/helpers/custom-validators/video-ownership.ts @@ -2,13 +2,14 @@ import { Response } from 'express' import { VideoChangeOwnershipModel } from '../../models/video/video-change-ownership' import { MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership' import { MUserId } from '@server/types/models' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' export async function doesChangeVideoOwnershipExist (idArg: number | string, res: Response) { const id = parseInt(idArg + '', 10) const videoChangeOwnership = await VideoChangeOwnershipModel.load(id) if (!videoChangeOwnership) { - res.status(404) + res.status(HttpStatusCode.NOT_FOUND_404) .json({ error: 'Video change ownership not found' }) .end() @@ -24,7 +25,7 @@ export function checkUserCanTerminateOwnershipChange (user: MUserId, videoChange return true } - res.status(403) + res.status(HttpStatusCode.FORBIDDEN_403) .json({ error: 'Cannot terminate an ownership change of another user' }) .end() return false -- cgit v1.2.3