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) cleanUpReqFiles(req)
res.fail({ res.fail({
type: ServerErrorCode.INCORRECT_FILES_IN_TORRENT.toString(), type: ServerErrorCode.INCORRECT_FILES_IN_TORRENT,
message: 'Torrents with only 1 file are supported.' message: 'Torrents with only 1 file are supported.'
}) })
return undefined return undefined

View File

@ -129,15 +129,14 @@ function getCountVideos (req: express.Request) {
// helpers added in server.ts and used in subsequent controllers used // helpers added in server.ts and used in subsequent controllers used
const apiResponseHelpers = (req, res: express.Response, next = null) => { const apiResponseHelpers = (req, res: express.Response, next = null) => {
res.fail = (options) => { res.fail = (options) => {
const { data, status, message, title, type, docs, instance } = { const { data, status = HttpStatusCode.BAD_REQUEST_400, message, title, type, docs = res.docs, instance } = options
data: null,
...options,
status: options.status || HttpStatusCode.BAD_REQUEST_400
}
const extension = new ProblemDocumentExtension({ const extension = new ProblemDocumentExtension({
...data, ...data,
docs: docs || res.docs docs,
// fields for <= 3.2 compatibility, deprecated
error: message,
code: type
}) })
res.status(status) res.status(status)
@ -146,12 +145,13 @@ const apiResponseHelpers = (req, res: express.Response, next = null) => {
status, status,
title, title,
instance, instance,
type: type && '' + type, // fields intended to replace 'error' and 'code' respectively
detail: message detail: message,
type: type && 'https://docs.joinpeertube.org/api-rest-reference.html#section/Errors/' + type
}, extension)) }, 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)) { if (!isRootActivityValid(req.body)) {
logger.warn('Incorrect activity parameters.', { activity: req.body }) logger.warn('Incorrect activity parameters.', { activity: req.body })
return res.status(HttpStatusCode.BAD_REQUEST_400) return res.fail({ message: 'Incorrect activity' })
.end()
} }
const serverActor = await getServerActor() const serverActor = await getServerActor()
const remoteActor = res.locals.signature.actor const remoteActor = res.locals.signature.actor
if (serverActor.id === remoteActor.id || remoteActor.serverId === null) { if (serverActor.id === remoteActor.id || remoteActor.serverId === null) {
logger.error('Receiving request in INBOX by ourselves!', req.body) logger.error('Receiving request in INBOX by ourselves!', req.body)
return res.status(HttpStatusCode.CONFLICT_409) return res.status(HttpStatusCode.CONFLICT_409).end()
.end()
} }
return next() return next()

View File

@ -104,7 +104,7 @@ const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
return res.fail({ return res.fail({
status: HttpStatusCode.FORBIDDEN_403, status: HttpStatusCode.FORBIDDEN_403,
message: 'Cannot create this live because the max instance lives limit is reached.', 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({ return res.fail({
status: HttpStatusCode.FORBIDDEN_403, 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.' 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({ return res.fail({
status: HttpStatusCode.FORBIDDEN_403, status: HttpStatusCode.FORBIDDEN_403,
message: 'Cannot get this video regarding follow constraints.', message: 'Cannot get this video regarding follow constraints.',
type: ServerErrorCode.DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS.toString(), type: ServerErrorCode.DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS,
data: { data: {
originUrl: video.url originUrl: video.url
} }

View File

@ -93,16 +93,20 @@ describe('Test users', function () {
const client = { id: 'client', secret: server.client.secret } const client = { id: 'client', secret: server.client.secret }
const res = await login(server.url, client, server.user, HttpStatusCode.BAD_REQUEST_400) const res = await login(server.url, client, server.user, HttpStatusCode.BAD_REQUEST_400)
expect(res.body.type).to.equal('invalid_client') expect(res.body.code).to.equal('invalid_client')
expect(res.body.detail).to.contain('client is invalid') 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 () { it('Should not login with an invalid client secret', async function () {
const client = { id: server.client.id, secret: 'coucou' } const client = { id: server.client.id, secret: 'coucou' }
const res = await login(server.url, client, server.user, HttpStatusCode.BAD_REQUEST_400) const res = await login(server.url, client, server.user, HttpStatusCode.BAD_REQUEST_400)
expect(res.body.type).to.equal('invalid_client') expect(res.body.code).to.equal('invalid_client')
expect(res.body.detail).to.contain('client is invalid') 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 user = { username: 'captain crochet', password: server.user.password }
const res = await login(server.url, server.client, user, HttpStatusCode.BAD_REQUEST_400) const res = await login(server.url, server.client, user, HttpStatusCode.BAD_REQUEST_400)
expect(res.body.type).to.equal('invalid_grant') expect(res.body.code).to.equal('invalid_grant')
expect(res.body.detail).to.contain('credentials are invalid') 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 () { it('Should not login with an invalid password', async function () {
const user = { username: server.user.username, password: 'mew_three' } const user = { username: server.user.username, password: 'mew_three' }
const res = await login(server.url, server.client, user, HttpStatusCode.BAD_REQUEST_400) const res = await login(server.url, server.client, user, HttpStatusCode.BAD_REQUEST_400)
expect(res.body.type).to.equal('invalid_grant') expect(res.body.code).to.equal('invalid_grant')
expect(res.body.detail).to.contain('credentials are invalid') 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 () { it('Should not be able to upload a video', async function () {

View File

@ -1,6 +1,6 @@
export const enum ServerErrorCode { export const enum ServerErrorCode {
DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS = 1, DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS = 'DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS',
MAX_INSTANCE_LIVES_LIMIT_REACHED = 2, MAX_INSTANCE_LIVES_LIMIT_REACHED = 'MAX_INSTANCE_LIVES_LIMIT_REACHED',
MAX_USER_LIVES_LIMIT_REACHED = 3, MAX_USER_LIVES_LIMIT_REACHED = 'MAX_USER_LIVES_LIMIT_REACHED',
INCORRECT_FILES_IN_TORRENT = 4 INCORRECT_FILES_IN_TORRENT = 'INCORRECT_FILES_IN_TORRENT'
} }