]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
correct error codes and backward compat
authorRigel Kent <sendmemail@rigelk.eu>
Tue, 1 Jun 2021 14:07:58 +0000 (16:07 +0200)
committerChocobozzz <chocobozzz@cpy.re>
Wed, 2 Jun 2021 14:57:07 +0000 (16:57 +0200)
server/controllers/api/videos/import.ts
server/helpers/express-utils.ts
server/middlewares/validators/activitypub/activity.ts
server/middlewares/validators/videos/video-live.ts
server/middlewares/validators/videos/videos.ts
server/tests/api/users/users.ts
shared/models/server/server-error-code.enum.ts

index 6ee109a8f7fac19b9665a3461a9443f7b8238b53..42ca59975aefe6b22448e9ac0d7cc7c45604dbca 100644 (file)
@@ -335,7 +335,7 @@ async function processTorrentOrAbortRequest (req: express.Request, res: express.
     cleanUpReqFiles(req)
 
     res.fail({
-      type: ServerErrorCode.INCORRECT_FILES_IN_TORRENT.toString(),
+      type: ServerErrorCode.INCORRECT_FILES_IN_TORRENT,
       message: 'Torrents with only 1 file are supported.'
     })
     return undefined
index bca59a83c38984223989335f84afcf9550bcf2e0..10a860787cf44bf8ded1b7c8d49d63fa61d66caa 100644 (file)
@@ -129,15 +129,14 @@ function getCountVideos (req: express.Request) {
 // helpers added in server.ts and used in subsequent controllers used
 const apiResponseHelpers = (req, res: express.Response, next = null) => {
   res.fail = (options) => {
-    const { data, status, message, title, type, docs, instance } = {
-      data: null,
-      ...options,
-      status: options.status || HttpStatusCode.BAD_REQUEST_400
-    }
+    const { data, status = HttpStatusCode.BAD_REQUEST_400, message, title, type, docs = res.docs, instance } = options
 
     const extension = new ProblemDocumentExtension({
       ...data,
-      docs: docs || res.docs
+      docs,
+      // fields for <= 3.2 compatibility, deprecated
+      error: message,
+      code: type
     })
 
     res.status(status)
@@ -146,12 +145,13 @@ const apiResponseHelpers = (req, res: express.Response, next = null) => {
       status,
       title,
       instance,
-      type: type && '' + type,
-      detail: message
+      // fields intended to replace 'error' and 'code' respectively
+      detail: message,
+      type: type && 'https://docs.joinpeertube.org/api-rest-reference.html#section/Errors/' + type
     }, extension))
   }
 
-  if (next !== null) next()
+  if (next) next()
 }
 
 // ---------------------------------------------------------------------------
index 59355e855d7d6c32b4c88975777d263f5fbaec20..cc6acd4b1aa401a60b9d518b5aef798c0a4a9829 100644 (file)
@@ -9,16 +9,14 @@ async function activityPubValidator (req: express.Request, res: express.Response
 
   if (!isRootActivityValid(req.body)) {
     logger.warn('Incorrect activity parameters.', { activity: req.body })
-    return res.status(HttpStatusCode.BAD_REQUEST_400)
-              .end()
+    return res.fail({ message: 'Incorrect activity' })
   }
 
   const serverActor = await getServerActor()
   const remoteActor = res.locals.signature.actor
   if (serverActor.id === remoteActor.id || remoteActor.serverId === null) {
     logger.error('Receiving request in INBOX by ourselves!', req.body)
-    return res.status(HttpStatusCode.CONFLICT_409)
-              .end()
+    return res.status(HttpStatusCode.CONFLICT_409).end()
   }
 
   return next()
index 9544fa4f501a804286c297a120ffadc923829c77..0fb864098eff81ce4c445fe06213775add8e7412 100644 (file)
@@ -104,7 +104,7 @@ const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
         return res.fail({
           status: HttpStatusCode.FORBIDDEN_403,
           message: 'Cannot create this live because the max instance lives limit is reached.',
-          type: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED.toString()
+          type: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED
         })
       }
     }
@@ -117,7 +117,7 @@ const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
 
         return res.fail({
           status: HttpStatusCode.FORBIDDEN_403,
-          type: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED.toString(),
+          type: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED,
           message: 'Cannot create this live because the max user lives limit is reached.'
         })
       }
index dfd4724003472f571dee119704748db94fd054d5..fd0e543f1e7cf9d49b9e1f33050beb4f70973709 100644 (file)
@@ -253,7 +253,7 @@ async function checkVideoFollowConstraints (req: express.Request, res: express.R
   return res.fail({
     status: HttpStatusCode.FORBIDDEN_403,
     message: 'Cannot get this video regarding follow constraints.',
-    type: ServerErrorCode.DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS.toString(),
+    type: ServerErrorCode.DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS,
     data: {
       originUrl: video.url
     }
index 363236b6270138f02ab6f176c9307a41e1e63487..464c11d3452247252dfb7117f687423e2e5b5457 100644 (file)
@@ -93,16 +93,20 @@ describe('Test users', function () {
       const client = { id: 'client', secret: server.client.secret }
       const res = await login(server.url, client, server.user, HttpStatusCode.BAD_REQUEST_400)
 
-      expect(res.body.type).to.equal('invalid_client')
-      expect(res.body.detail).to.contain('client is invalid')
+      expect(res.body.code).to.equal('invalid_client')
+      expect(res.body.error).to.contain('client is invalid')
+      expect(res.body.type.startsWith('https://')).to.be.true
+      expect(res.body.type).to.contain('invalid_client')
     })
 
     it('Should not login with an invalid client secret', async function () {
       const client = { id: server.client.id, secret: 'coucou' }
       const res = await login(server.url, client, server.user, HttpStatusCode.BAD_REQUEST_400)
 
-      expect(res.body.type).to.equal('invalid_client')
-      expect(res.body.detail).to.contain('client is invalid')
+      expect(res.body.code).to.equal('invalid_client')
+      expect(res.body.error).to.contain('client is invalid')
+      expect(res.body.type.startsWith('https://')).to.be.true
+      expect(res.body.type).to.contain('invalid_client')
     })
   })
 
@@ -112,16 +116,20 @@ describe('Test users', function () {
       const user = { username: 'captain crochet', password: server.user.password }
       const res = await login(server.url, server.client, user, HttpStatusCode.BAD_REQUEST_400)
 
-      expect(res.body.type).to.equal('invalid_grant')
-      expect(res.body.detail).to.contain('credentials are invalid')
+      expect(res.body.code).to.equal('invalid_grant')
+      expect(res.body.error).to.contain('credentials are invalid')
+      expect(res.body.type.startsWith('https://')).to.be.true
+      expect(res.body.type).to.contain('invalid_grant')
     })
 
     it('Should not login with an invalid password', async function () {
       const user = { username: server.user.username, password: 'mew_three' }
       const res = await login(server.url, server.client, user, HttpStatusCode.BAD_REQUEST_400)
 
-      expect(res.body.type).to.equal('invalid_grant')
-      expect(res.body.detail).to.contain('credentials are invalid')
+      expect(res.body.code).to.equal('invalid_grant')
+      expect(res.body.error).to.contain('credentials are invalid')
+      expect(res.body.type.startsWith('https://')).to.be.true
+      expect(res.body.type).to.contain('invalid_grant')
     })
 
     it('Should not be able to upload a video', async function () {
index d17d958be227f6280f9e9999238f561b8c6b70a1..f9fe478308167205bc7644f431386f88a468a94b 100644 (file)
@@ -1,6 +1,6 @@
 export const enum ServerErrorCode {
-  DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS = 1,
-  MAX_INSTANCE_LIVES_LIMIT_REACHED = 2,
-  MAX_USER_LIVES_LIMIT_REACHED = 3,
-  INCORRECT_FILES_IN_TORRENT = 4
+  DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS = 'DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS',
+  MAX_INSTANCE_LIVES_LIMIT_REACHED = 'MAX_INSTANCE_LIVES_LIMIT_REACHED',
+  MAX_USER_LIVES_LIMIT_REACHED = 'MAX_USER_LIVES_LIMIT_REACHED',
+  INCORRECT_FILES_IN_TORRENT = 'INCORRECT_FILES_IN_TORRENT'
 }