correct error codes and backward compat

pull/4158/head
Rigel Kent 2021-06-01 16:07:58 +02:00 committed by Chocobozzz
parent 1cfbdd30d9
commit 3866ea02d4
7 changed files with 35 additions and 29 deletions

View 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

View 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()
}
// ---------------------------------------------------------------------------

View 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()

View 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.'
})
}

View 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
}

View 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 () {

View 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'
}