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/helpers/middlewares | |
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/helpers/middlewares')
-rw-r--r-- | server/helpers/middlewares/abuses.ts | 6 | ||||
-rw-r--r-- | server/helpers/middlewares/accounts.ts | 16 | ||||
-rw-r--r-- | server/helpers/middlewares/video-blacklists.ts | 8 | ||||
-rw-r--r-- | server/helpers/middlewares/video-captions.ts | 7 | ||||
-rw-r--r-- | server/helpers/middlewares/video-channels.ts | 7 | ||||
-rw-r--r-- | server/helpers/middlewares/video-playlists.ts | 8 | ||||
-rw-r--r-- | server/helpers/middlewares/videos.ts | 40 |
7 files changed, 48 insertions, 44 deletions
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) { | |||
6 | const abuse = await AbuseModel.loadByIdWithReporter(parseInt(abuseId + '', 10)) | 6 | const abuse = await AbuseModel.loadByIdWithReporter(parseInt(abuseId + '', 10)) |
7 | 7 | ||
8 | if (!abuse) { | 8 | if (!abuse) { |
9 | res.status(HttpStatusCode.NOT_FOUND_404) | 9 | res.fail({ |
10 | .json({ error: 'Abuse not found' }) | 10 | status: HttpStatusCode.NOT_FOUND_404, |
11 | message: 'Abuse not found' | ||
12 | }) | ||
11 | 13 | ||
12 | return false | 14 | return false |
13 | } | 15 | } |
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<MAccountDefault>, res: Response, sen | |||
27 | 27 | ||
28 | if (!account) { | 28 | if (!account) { |
29 | if (sendNotFound === true) { | 29 | if (sendNotFound === true) { |
30 | res.status(HttpStatusCode.NOT_FOUND_404) | 30 | res.fail({ |
31 | .json({ error: 'Account not found' }) | 31 | status: HttpStatusCode.NOT_FOUND_404, |
32 | message: 'Account not found' | ||
33 | }) | ||
32 | } | 34 | } |
33 | |||
34 | return false | 35 | return false |
35 | } | 36 | } |
36 | 37 | ||
37 | res.locals.account = account | 38 | res.locals.account = account |
38 | |||
39 | return true | 39 | return true |
40 | } | 40 | } |
41 | 41 | ||
@@ -43,14 +43,14 @@ async function doesUserFeedTokenCorrespond (id: number, token: string, res: Resp | |||
43 | const user = await UserModel.loadByIdWithChannels(parseInt(id + '', 10)) | 43 | const user = await UserModel.loadByIdWithChannels(parseInt(id + '', 10)) |
44 | 44 | ||
45 | if (token !== user.feedToken) { | 45 | if (token !== user.feedToken) { |
46 | res.status(HttpStatusCode.FORBIDDEN_403) | 46 | res.fail({ |
47 | .json({ error: 'User and token mismatch' }) | 47 | status: HttpStatusCode.FORBIDDEN_403, |
48 | 48 | message: 'User and token mismatch' | |
49 | }) | ||
49 | return false | 50 | return false |
50 | } | 51 | } |
51 | 52 | ||
52 | res.locals.user = user | 53 | res.locals.user = user |
53 | |||
54 | return true | 54 | return true |
55 | } | 55 | } |
56 | 56 | ||
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) { | |||
6 | const videoBlacklist = await VideoBlacklistModel.loadByVideoId(videoId) | 6 | const videoBlacklist = await VideoBlacklistModel.loadByVideoId(videoId) |
7 | 7 | ||
8 | if (videoBlacklist === null) { | 8 | if (videoBlacklist === null) { |
9 | res.status(HttpStatusCode.NOT_FOUND_404) | 9 | res.fail({ |
10 | .json({ error: 'Blacklisted video not found' }) | 10 | status: HttpStatusCode.NOT_FOUND_404, |
11 | .end() | 11 | message: 'Blacklisted video not found' |
12 | 12 | }) | |
13 | return false | 13 | return false |
14 | } | 14 | } |
15 | 15 | ||
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 | |||
7 | const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(video.id, language) | 7 | const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(video.id, language) |
8 | 8 | ||
9 | if (!videoCaption) { | 9 | if (!videoCaption) { |
10 | res.status(HttpStatusCode.NOT_FOUND_404) | 10 | res.fail({ |
11 | .json({ error: 'Video caption not found' }) | 11 | status: HttpStatusCode.NOT_FOUND_404, |
12 | 12 | message: 'Video caption not found' | |
13 | }) | ||
13 | return false | 14 | return false |
14 | } | 15 | } |
15 | 16 | ||
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 { | |||
31 | 31 | ||
32 | function processVideoChannelExist (videoChannel: MChannelBannerAccountDefault, res: express.Response) { | 32 | function processVideoChannelExist (videoChannel: MChannelBannerAccountDefault, res: express.Response) { |
33 | if (!videoChannel) { | 33 | if (!videoChannel) { |
34 | res.status(HttpStatusCode.NOT_FOUND_404) | 34 | res.fail({ |
35 | .json({ error: 'Video channel not found' }) | 35 | status: HttpStatusCode.NOT_FOUND_404, |
36 | 36 | message: 'Video channel not found' | |
37 | }) | ||
37 | return false | 38 | return false |
38 | } | 39 | } |
39 | 40 | ||
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 { | |||
28 | 28 | ||
29 | function handleVideoPlaylist (videoPlaylist: MVideoPlaylist, res: express.Response) { | 29 | function handleVideoPlaylist (videoPlaylist: MVideoPlaylist, res: express.Response) { |
30 | if (!videoPlaylist) { | 30 | if (!videoPlaylist) { |
31 | res.status(HttpStatusCode.NOT_FOUND_404) | 31 | res.fail({ |
32 | .json({ error: 'Video playlist not found' }) | 32 | status: HttpStatusCode.NOT_FOUND_404, |
33 | .end() | 33 | message: 'Video playlist not found' |
34 | 34 | }) | |
35 | return false | 35 | return false |
36 | } | 36 | } |
37 | 37 | ||
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 | |||
21 | const video = await fetchVideo(id, fetchType, userId) | 21 | const video = await fetchVideo(id, fetchType, userId) |
22 | 22 | ||
23 | if (video === null) { | 23 | if (video === null) { |
24 | res.status(HttpStatusCode.NOT_FOUND_404) | 24 | res.fail({ |
25 | .json({ error: 'Video not found' }) | 25 | status: HttpStatusCode.NOT_FOUND_404, |
26 | .end() | 26 | message: 'Video not found' |
27 | 27 | }) | |
28 | return false | 28 | return false |
29 | } | 29 | } |
30 | 30 | ||
@@ -55,10 +55,10 @@ async function doesVideoExist (id: number | string, res: Response, fetchType: Vi | |||
55 | 55 | ||
56 | async function doesVideoFileOfVideoExist (id: number, videoIdOrUUID: number | string, res: Response) { | 56 | async function doesVideoFileOfVideoExist (id: number, videoIdOrUUID: number | string, res: Response) { |
57 | if (!await VideoFileModel.doesVideoExistForVideoFile(id, videoIdOrUUID)) { | 57 | if (!await VideoFileModel.doesVideoExistForVideoFile(id, videoIdOrUUID)) { |
58 | res.status(HttpStatusCode.NOT_FOUND_404) | 58 | res.fail({ |
59 | .json({ error: 'VideoFile matching Video not found' }) | 59 | status: HttpStatusCode.NOT_FOUND_404, |
60 | .end() | 60 | message: 'VideoFile matching Video not found' |
61 | 61 | }) | |
62 | return false | 62 | return false |
63 | } | 63 | } |
64 | 64 | ||
@@ -69,9 +69,7 @@ async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAcc | |||
69 | const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId) | 69 | const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId) |
70 | 70 | ||
71 | if (videoChannel === null) { | 71 | if (videoChannel === null) { |
72 | res.status(HttpStatusCode.BAD_REQUEST_400) | 72 | res.fail({ message: 'Unknown video "video channel" for this instance.' }) |
73 | .json({ error: 'Unknown video "video channel" for this instance.' }) | ||
74 | |||
75 | return false | 73 | return false |
76 | } | 74 | } |
77 | 75 | ||
@@ -82,9 +80,9 @@ async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAcc | |||
82 | } | 80 | } |
83 | 81 | ||
84 | if (videoChannel.Account.id !== user.Account.id) { | 82 | if (videoChannel.Account.id !== user.Account.id) { |
85 | res.status(HttpStatusCode.BAD_REQUEST_400) | 83 | res.fail({ |
86 | .json({ error: 'Unknown video "video channel" for this account.' }) | 84 | message: 'Unknown video "video channel" for this account.' |
87 | 85 | }) | |
88 | return false | 86 | return false |
89 | } | 87 | } |
90 | 88 | ||
@@ -95,9 +93,10 @@ async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAcc | |||
95 | function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response, onlyOwned = true) { | 93 | function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response, onlyOwned = true) { |
96 | // Retrieve the user who did the request | 94 | // Retrieve the user who did the request |
97 | if (onlyOwned && video.isOwned() === false) { | 95 | if (onlyOwned && video.isOwned() === false) { |
98 | res.status(HttpStatusCode.FORBIDDEN_403) | 96 | res.fail({ |
99 | .json({ error: 'Cannot manage a video of another server.' }) | 97 | status: HttpStatusCode.FORBIDDEN_403, |
100 | .end() | 98 | message: 'Cannot manage a video of another server.' |
99 | }) | ||
101 | return false | 100 | return false |
102 | } | 101 | } |
103 | 102 | ||
@@ -106,9 +105,10 @@ function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: | |||
106 | // Or if s/he is the video's account | 105 | // Or if s/he is the video's account |
107 | const account = video.VideoChannel.Account | 106 | const account = video.VideoChannel.Account |
108 | if (user.hasRight(right) === false && account.userId !== user.id) { | 107 | if (user.hasRight(right) === false && account.userId !== user.id) { |
109 | res.status(HttpStatusCode.FORBIDDEN_403) | 108 | res.fail({ |
110 | .json({ error: 'Cannot manage a video of another user.' }) | 109 | status: HttpStatusCode.FORBIDDEN_403, |
111 | .end() | 110 | message: 'Cannot manage a video of another user.' |
111 | }) | ||
112 | return false | 112 | return false |
113 | } | 113 | } |
114 | 114 | ||