Skip to content

Commit

Permalink
Merge pull request #1172 from yashkohli88/yk/fix-origins-pypi-error-h…
Browse files Browse the repository at this point in the history
…andling

Fixed origins api for pypi components throwing 500 error when invalid package is provided
  • Loading branch information
elrayle authored Aug 28, 2024
2 parents 9a1cb7a + 151813d commit c008d00
Show file tree
Hide file tree
Showing 3 changed files with 40,676 additions and 8 deletions.
19 changes: 13 additions & 6 deletions routes/originPyPi.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ router.get(
'/:name/revisions',
asyncMiddleware(async (request, response) => {
const { name } = request.params
const url = `https://pypi.python.org/pypi/${name}/json`
const answer = await requestPromise({ url, method: 'GET', json: true })
const answer = await getPypiData(name)
const result = answer && answer.releases ? Object.keys(answer.releases) : []
result.reverse()
return response.status(200).send(uniq(result))
Expand All @@ -23,14 +22,22 @@ router.get(
'/:name',
asyncMiddleware(async (request, response) => {
const { name } = request.params
const url = `https://pypi.python.org/pypi/${name}/json`
const answer = await requestPromise({ url, method: 'GET', json: true })
const answer = await getPypiData(name)
const result = answer && answer.info ? [{ id: answer.info.name }] : []
return response.status(200).send(result)
})
)

function setup() {
async function getPypiData(name) {
const url = `https://pypi.python.org/pypi/${encodeURIComponent(name)}/json`
try {
return await requestPromise({ url, method: 'GET', json: true })
} catch (error) {
if (error.statusCode === 404) return {}
throw error
}
}
function setup(testflag = false) {
if (testflag) router._getPypiData = getPypiData
return router
}

Expand Down
Loading

0 comments on commit c008d00

Please sign in to comment.