Skip to content

Commit 3e04771

Browse files
authored
Replace Bad Instance of databaseTiny.find() (#474)
* Fix smoke test for development. Fix issue with bad usage of databaseTiny.find(). * Changes during review
1 parent 86bb137 commit 3e04771

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

__tests__/smoke.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ async function runSmokeChecks() {
5757
if (!(hasHeading || hasWelcome)) throw new Error('Expected TPEN3 Services index HTML not found in response')
5858
}))
5959

60-
// Protected endpoint requires authentication (400 when Authorization header is missing)
60+
// Protected endpoint requires authentication (401 when Authorization header is missing)
6161
checks.push(await runCheck('Protected endpoint requires authentication', async () => {
6262
const res = await request('/my/profile')
63-
if (res.status !== 400) throw new Error(`Expected 400, got ${res.status}`)
63+
if (res.status !== 401) throw new Error(`Expected 401, got ${res.status}`)
6464
}))
6565

6666
// CORS headers present for allowed origin

utilities/shared.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,29 @@ export const updateLayerAndProject = async (layer, project, userId) => {
8484
const pageOverwrites = updatedLayer.pages
8585
.filter(page => page.id.startsWith(process.env.RERUMIDPREFIX))
8686
.map(async page => {
87-
const oldPage = await databaseTiny.find({_id: page.id.split("/").pop()})
88-
if(!oldPage) throw new Error(`Page with ID ${page.id} not found in RERUM`)
87+
const oldPage = await fetch(page.id).then(async (resp) => {
88+
if (resp.ok) return resp.json()
89+
let rerumErrorMessage
90+
try {
91+
rerumErrorMessage = `${resp.status ?? 500}: ${page.id} - ${await resp.text()}`
92+
} catch (e) {
93+
rerumErrorMessage = `500: ${page.id} - A RERUM error occurred`
94+
}
95+
const err = new Error(rerumErrorMessage)
96+
err.status = 502
97+
throw err
98+
})
99+
.catch(err => {
100+
if (err.status === 502) throw err
101+
const genericRerumNetworkError = new Error(`500: ${page.id} - A RERUM error occurred`)
102+
genericRerumNetworkError.status = 502
103+
throw genericRerumNetworkError
104+
})
105+
if (!(oldPage?.id || oldPage?.["@id"])) {
106+
const err = new Error(`500: ${page.id} - A RERUM error occurred`)
107+
err.status = 502
108+
throw err
109+
}
89110
oldPage.partOf = [{ id: updatedLayer.id, type: "AnnotationCollection" }]
90111
return databaseTiny.overwrite(oldPage)
91112
})

0 commit comments

Comments
 (0)