diff --git a/__tests__/smoke.test.js b/__tests__/smoke.test.js index c43a3c0c..a3cf220c 100644 --- a/__tests__/smoke.test.js +++ b/__tests__/smoke.test.js @@ -57,10 +57,10 @@ async function runSmokeChecks() { if (!(hasHeading || hasWelcome)) throw new Error('Expected TPEN3 Services index HTML not found in response') })) - // Protected endpoint requires authentication (400 when Authorization header is missing) + // Protected endpoint requires authentication (401 when Authorization header is missing) checks.push(await runCheck('Protected endpoint requires authentication', async () => { const res = await request('/my/profile') - if (res.status !== 400) throw new Error(`Expected 400, got ${res.status}`) + if (res.status !== 401) throw new Error(`Expected 401, got ${res.status}`) })) // CORS headers present for allowed origin diff --git a/utilities/shared.js b/utilities/shared.js index b3f1cd05..e2ff2441 100644 --- a/utilities/shared.js +++ b/utilities/shared.js @@ -84,8 +84,29 @@ export const updateLayerAndProject = async (layer, project, userId) => { const pageOverwrites = updatedLayer.pages .filter(page => page.id.startsWith(process.env.RERUMIDPREFIX)) .map(async page => { - const oldPage = await databaseTiny.find({_id: page.id.split("/").pop()}) - if(!oldPage) throw new Error(`Page with ID ${page.id} not found in RERUM`) + const oldPage = await fetch(page.id).then(async (resp) => { + if (resp.ok) return resp.json() + let rerumErrorMessage + try { + rerumErrorMessage = `${resp.status ?? 500}: ${page.id} - ${await resp.text()}` + } catch (e) { + rerumErrorMessage = `500: ${page.id} - A RERUM error occurred` + } + const err = new Error(rerumErrorMessage) + err.status = 502 + throw err + }) + .catch(err => { + if (err.status === 502) throw err + const genericRerumNetworkError = new Error(`500: ${page.id} - A RERUM error occurred`) + genericRerumNetworkError.status = 502 + throw genericRerumNetworkError + }) + if (!(oldPage?.id || oldPage?.["@id"])) { + const err = new Error(`500: ${page.id} - A RERUM error occurred`) + err.status = 502 + throw err + } oldPage.partOf = [{ id: updatedLayer.id, type: "AnnotationCollection" }] return databaseTiny.overwrite(oldPage) })