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/middlewares/validators/oembed.ts | |
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/middlewares/validators/oembed.ts')
-rw-r--r-- | server/middlewares/validators/oembed.ts | 45 |
1 files changed, 31 insertions, 14 deletions
diff --git a/server/middlewares/validators/oembed.ts b/server/middlewares/validators/oembed.ts index 165eda6d5..b1d763fbe 100644 --- a/server/middlewares/validators/oembed.ts +++ b/server/middlewares/validators/oembed.ts | |||
@@ -51,8 +51,13 @@ const oembedValidator = [ | |||
51 | if (areValidationErrors(req, res)) return | 51 | if (areValidationErrors(req, res)) return |
52 | 52 | ||
53 | if (req.query.format !== undefined && req.query.format !== 'json') { | 53 | if (req.query.format !== undefined && req.query.format !== 'json') { |
54 | return res.status(HttpStatusCode.NOT_IMPLEMENTED_501) | 54 | return res.fail({ |
55 | .json({ error: 'Requested format is not implemented on server.' }) | 55 | status: HttpStatusCode.NOT_IMPLEMENTED_501, |
56 | message: 'Requested format is not implemented on server.', | ||
57 | data: { | ||
58 | format: req.query.format | ||
59 | } | ||
60 | }) | ||
56 | } | 61 | } |
57 | 62 | ||
58 | const url = req.query.url as string | 63 | const url = req.query.url as string |
@@ -65,27 +70,35 @@ const oembedValidator = [ | |||
65 | const matches = watchRegex.exec(url) | 70 | const matches = watchRegex.exec(url) |
66 | 71 | ||
67 | if (startIsOk === false || matches === null) { | 72 | if (startIsOk === false || matches === null) { |
68 | return res.status(HttpStatusCode.BAD_REQUEST_400) | 73 | return res.fail({ |
69 | .json({ error: 'Invalid url.' }) | 74 | status: HttpStatusCode.BAD_REQUEST_400, |
75 | message: 'Invalid url.', | ||
76 | data: { | ||
77 | url | ||
78 | } | ||
79 | }) | ||
70 | } | 80 | } |
71 | 81 | ||
72 | const elementId = matches[1] | 82 | const elementId = matches[1] |
73 | if (isIdOrUUIDValid(elementId) === false) { | 83 | if (isIdOrUUIDValid(elementId) === false) { |
74 | return res.status(HttpStatusCode.BAD_REQUEST_400) | 84 | return res.fail({ message: 'Invalid video or playlist id.' }) |
75 | .json({ error: 'Invalid video or playlist id.' }) | ||
76 | } | 85 | } |
77 | 86 | ||
78 | if (isVideo) { | 87 | if (isVideo) { |
79 | const video = await fetchVideo(elementId, 'all') | 88 | const video = await fetchVideo(elementId, 'all') |
80 | 89 | ||
81 | if (!video) { | 90 | if (!video) { |
82 | return res.status(HttpStatusCode.NOT_FOUND_404) | 91 | return res.fail({ |
83 | .json({ error: 'Video not found' }) | 92 | status: HttpStatusCode.NOT_FOUND_404, |
93 | message: 'Video not found' | ||
94 | }) | ||
84 | } | 95 | } |
85 | 96 | ||
86 | if (video.privacy !== VideoPrivacy.PUBLIC) { | 97 | if (video.privacy !== VideoPrivacy.PUBLIC) { |
87 | return res.status(HttpStatusCode.FORBIDDEN_403) | 98 | return res.fail({ |
88 | .json({ error: 'Video is not public' }) | 99 | status: HttpStatusCode.FORBIDDEN_403, |
100 | message: 'Video is not public' | ||
101 | }) | ||
89 | } | 102 | } |
90 | 103 | ||
91 | res.locals.videoAll = video | 104 | res.locals.videoAll = video |
@@ -96,13 +109,17 @@ const oembedValidator = [ | |||
96 | 109 | ||
97 | const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannelSummary(elementId, undefined) | 110 | const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannelSummary(elementId, undefined) |
98 | if (!videoPlaylist) { | 111 | if (!videoPlaylist) { |
99 | return res.status(HttpStatusCode.NOT_FOUND_404) | 112 | return res.fail({ |
100 | .json({ error: 'Video playlist not found' }) | 113 | status: HttpStatusCode.NOT_FOUND_404, |
114 | message: 'Video playlist not found' | ||
115 | }) | ||
101 | } | 116 | } |
102 | 117 | ||
103 | if (videoPlaylist.privacy !== VideoPlaylistPrivacy.PUBLIC) { | 118 | if (videoPlaylist.privacy !== VideoPlaylistPrivacy.PUBLIC) { |
104 | return res.status(HttpStatusCode.FORBIDDEN_403) | 119 | return res.fail({ |
105 | .json({ error: 'Playlist is not public' }) | 120 | status: HttpStatusCode.FORBIDDEN_403, |
121 | message: 'Playlist is not public' | ||
122 | }) | ||
106 | } | 123 | } |
107 | 124 | ||
108 | res.locals.videoPlaylistSummary = videoPlaylist | 125 | res.locals.videoPlaylistSummary = videoPlaylist |