overleaf/services/track-changes/app/js/DocstoreManager.js
Eric Mc Sween 98a4742c0a Merge pull request #5997 from overleaf/em-track-changes-docstore-404
Skip inexistent docs when building ShareLaTeX history

GitOrigin-RevId: 5b7c5b77623d77c7d20f852050249211de0c8cb3
2021-12-06 09:03:05 +00:00

55 lines
1.5 KiB
JavaScript

const request = require('request')
const logger = require('logger-sharelatex')
const Settings = require('@overleaf/settings')
const Errors = require('./Errors')
function peekDocument(projectId, docId, callback) {
const url = `${Settings.apis.docstore.url}/project/${projectId}/doc/${docId}/peek`
logger.log(
{ project_id: projectId, doc_id: docId },
'getting doc from docstore'
)
request.get(url, function (error, res, body) {
if (error != null) {
return callback(error)
}
if (res.statusCode >= 200 && res.statusCode < 300) {
try {
body = JSON.parse(body)
} catch (error) {
return callback(error)
}
logger.log(
{ project_id: projectId, doc_id: docId, version: body.version },
'got doc from docstore'
)
return callback(null, body.lines.join('\n'), body.version)
} else if (res.statusCode === 404) {
return callback(
new Errors.NotFoundError('doc not found', { projectId, docId })
)
} else {
return callback(
new Error(
`docstore returned a non-success status code: ${res.statusCode}`
)
)
}
})
}
module.exports = {
promises: {
peekDocument: (projectId, docId) => {
return new Promise((resolve, reject) => {
peekDocument(projectId, docId, (err, content, version) => {
if (err) {
reject(err)
} else {
resolve([content, version])
}
})
})
},
},
}