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/middlewares/abuses.ts | 6 ++-- server/helpers/middlewares/accounts.ts | 16 +++++------ server/helpers/middlewares/video-blacklists.ts | 8 +++--- server/helpers/middlewares/video-captions.ts | 7 +++-- server/helpers/middlewares/video-channels.ts | 7 +++-- server/helpers/middlewares/video-playlists.ts | 8 +++--- server/helpers/middlewares/videos.ts | 40 +++++++++++++------------- 7 files changed, 48 insertions(+), 44 deletions(-) (limited to 'server/helpers/middlewares') diff --git a/server/helpers/middlewares/abuses.ts b/server/helpers/middlewares/abuses.ts index c53bd9efd..f0b1caba8 100644 --- a/server/helpers/middlewares/abuses.ts +++ b/server/helpers/middlewares/abuses.ts @@ -6,8 +6,10 @@ async function doesAbuseExist (abuseId: number | string, res: Response) { const abuse = await AbuseModel.loadByIdWithReporter(parseInt(abuseId + '', 10)) if (!abuse) { - res.status(HttpStatusCode.NOT_FOUND_404) - .json({ error: 'Abuse not found' }) + res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Abuse not found' + }) return false } diff --git a/server/helpers/middlewares/accounts.ts b/server/helpers/middlewares/accounts.ts index 5addd3e1a..7db79bc48 100644 --- a/server/helpers/middlewares/accounts.ts +++ b/server/helpers/middlewares/accounts.ts @@ -27,15 +27,15 @@ async function doesAccountExist (p: Promise, res: Response, sen if (!account) { if (sendNotFound === true) { - res.status(HttpStatusCode.NOT_FOUND_404) - .json({ error: 'Account not found' }) + res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Account not found' + }) } - return false } res.locals.account = account - return true } @@ -43,14 +43,14 @@ async function doesUserFeedTokenCorrespond (id: number, token: string, res: Resp const user = await UserModel.loadByIdWithChannels(parseInt(id + '', 10)) if (token !== user.feedToken) { - res.status(HttpStatusCode.FORBIDDEN_403) - .json({ error: 'User and token mismatch' }) - + res.fail({ + status: HttpStatusCode.FORBIDDEN_403, + message: 'User and token mismatch' + }) return false } res.locals.user = user - return true } diff --git a/server/helpers/middlewares/video-blacklists.ts b/server/helpers/middlewares/video-blacklists.ts index eda1324d3..3494fd6b0 100644 --- a/server/helpers/middlewares/video-blacklists.ts +++ b/server/helpers/middlewares/video-blacklists.ts @@ -6,10 +6,10 @@ async function doesVideoBlacklistExist (videoId: number, res: Response) { const videoBlacklist = await VideoBlacklistModel.loadByVideoId(videoId) if (videoBlacklist === null) { - res.status(HttpStatusCode.NOT_FOUND_404) - .json({ error: 'Blacklisted video not found' }) - .end() - + res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Blacklisted video not found' + }) return false } diff --git a/server/helpers/middlewares/video-captions.ts b/server/helpers/middlewares/video-captions.ts index 226d3c5f8..2a12c4813 100644 --- a/server/helpers/middlewares/video-captions.ts +++ b/server/helpers/middlewares/video-captions.ts @@ -7,9 +7,10 @@ async function doesVideoCaptionExist (video: MVideoId, language: string, res: Re const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(video.id, language) if (!videoCaption) { - res.status(HttpStatusCode.NOT_FOUND_404) - .json({ error: 'Video caption not found' }) - + res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Video caption not found' + }) return false } diff --git a/server/helpers/middlewares/video-channels.ts b/server/helpers/middlewares/video-channels.ts index 602555921..f5ed5ef0f 100644 --- a/server/helpers/middlewares/video-channels.ts +++ b/server/helpers/middlewares/video-channels.ts @@ -31,9 +31,10 @@ export { function processVideoChannelExist (videoChannel: MChannelBannerAccountDefault, res: express.Response) { if (!videoChannel) { - res.status(HttpStatusCode.NOT_FOUND_404) - .json({ error: 'Video channel not found' }) - + res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Video channel not found' + }) return false } diff --git a/server/helpers/middlewares/video-playlists.ts b/server/helpers/middlewares/video-playlists.ts index d2dd80a35..3faeab677 100644 --- a/server/helpers/middlewares/video-playlists.ts +++ b/server/helpers/middlewares/video-playlists.ts @@ -28,10 +28,10 @@ export { function handleVideoPlaylist (videoPlaylist: MVideoPlaylist, res: express.Response) { if (!videoPlaylist) { - res.status(HttpStatusCode.NOT_FOUND_404) - .json({ error: 'Video playlist not found' }) - .end() - + res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Video playlist not found' + }) return false } diff --git a/server/helpers/middlewares/videos.ts b/server/helpers/middlewares/videos.ts index 403cae092..52b934eb7 100644 --- a/server/helpers/middlewares/videos.ts +++ b/server/helpers/middlewares/videos.ts @@ -21,10 +21,10 @@ async function doesVideoExist (id: number | string, res: Response, fetchType: Vi const video = await fetchVideo(id, fetchType, userId) if (video === null) { - res.status(HttpStatusCode.NOT_FOUND_404) - .json({ error: 'Video not found' }) - .end() - + res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Video not found' + }) return false } @@ -55,10 +55,10 @@ async function doesVideoExist (id: number | string, res: Response, fetchType: Vi async function doesVideoFileOfVideoExist (id: number, videoIdOrUUID: number | string, res: Response) { if (!await VideoFileModel.doesVideoExistForVideoFile(id, videoIdOrUUID)) { - res.status(HttpStatusCode.NOT_FOUND_404) - .json({ error: 'VideoFile matching Video not found' }) - .end() - + res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'VideoFile matching Video not found' + }) return false } @@ -69,9 +69,7 @@ async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAcc const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId) if (videoChannel === null) { - res.status(HttpStatusCode.BAD_REQUEST_400) - .json({ error: 'Unknown video "video channel" for this instance.' }) - + res.fail({ message: 'Unknown video "video channel" for this instance.' }) return false } @@ -82,9 +80,9 @@ async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAcc } if (videoChannel.Account.id !== user.Account.id) { - res.status(HttpStatusCode.BAD_REQUEST_400) - .json({ error: 'Unknown video "video channel" for this account.' }) - + res.fail({ + message: 'Unknown video "video channel" for this account.' + }) return false } @@ -95,9 +93,10 @@ async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAcc function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response, onlyOwned = true) { // Retrieve the user who did the request if (onlyOwned && video.isOwned() === false) { - res.status(HttpStatusCode.FORBIDDEN_403) - .json({ error: 'Cannot manage a video of another server.' }) - .end() + res.fail({ + status: HttpStatusCode.FORBIDDEN_403, + message: 'Cannot manage a video of another server.' + }) return false } @@ -106,9 +105,10 @@ function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: // Or if s/he is the video's account const account = video.VideoChannel.Account if (user.hasRight(right) === false && account.userId !== user.id) { - res.status(HttpStatusCode.FORBIDDEN_403) - .json({ error: 'Cannot manage a video of another user.' }) - .end() + res.fail({ + status: HttpStatusCode.FORBIDDEN_403, + message: 'Cannot manage a video of another user.' + }) return false } -- cgit v1.2.3