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
// 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)
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()
}
// ---------------------------------------------------------------------------
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()
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
})
}
}
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.'
})
}
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
}
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')
})
})
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 () {
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'
}