Skip to content

Commit 87f461f

Browse files
committed
Improve overwrite route error handling and header support
Enhanced the overwrite route to better handle errors, including 409 version conflicts, and to support the If-Overwritten-Version header from both request headers and the request body. Also improved validation for record IDs and refactored response handling for clarity.
1 parent 651f478 commit 87f461f

1 file changed

Lines changed: 42 additions & 12 deletions

File tree

routes/overwrite.js

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,60 @@ import rerumPropertiesWasher from "../preprocessor.js"
77
router.put('/', checkAccessToken, rerumPropertiesWasher, async (req, res, next) => {
88

99
try {
10-
// check for @id in body. Any value is valid. Lack of value is a bad request.
11-
if (!req?.body || !(req.body['@id'] ?? req.body.id)) {
12-
res.status(400).send("No record id to overwrite! (https://store.rerum.io/v1/API.html#overwrite)")
10+
11+
const overwriteBody = req.body
12+
// check for @id; any value is valid
13+
if (!(overwriteBody['@id'] ?? overwriteBody.id)) {
14+
throw Error("No record id to overwrite! (https://store.rerum.io/API.html#overwrite)")
1315
}
14-
// check body for JSON
15-
const body = JSON.stringify(req.body)
16+
1617
const overwriteOptions = {
1718
method: 'PUT',
18-
body,
19+
body: JSON.stringify(overwriteBody),
1920
headers: {
2021
'user-agent': 'Tiny-Things/1.0',
2122
'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`,
2223
'Content-Type' : "application/json;charset=utf-8"
2324
}
2425
}
26+
27+
// Pass through If-Overwritten-Version header if present
28+
const ifOverwrittenVersion = req.headers['if-overwritten-version']
29+
if (ifOverwrittenVersion) {
30+
overwriteOptions.headers['If-Overwritten-Version'] = ifOverwrittenVersion
31+
}
32+
33+
// Check for __rerum.isOverwritten in body and use as If-Overwritten-Version header
34+
const isOverwrittenValue = req.body?.__rerum?.isOverwritten
35+
if (isOverwrittenValue) {
36+
overwriteOptions.headers['If-Overwritten-Version'] = isOverwrittenValue
37+
}
38+
2539
const overwriteURL = `${process.env.RERUM_API_ADDR}overwrite`
26-
const result = await fetch(overwriteURL, overwriteOptions).then(res=>res.json())
27-
.catch(err=>next(err))
28-
res.setHeader("Location", result["@id"] ?? result.id)
29-
res.status(200)
40+
const response = await fetch(overwriteURL, overwriteOptions)
41+
.then(resp=>{
42+
if (!resp.ok) throw resp
43+
return resp
44+
})
45+
.catch(async err => {
46+
// Handle 409 conflict error for version mismatch
47+
if (err.status === 409) {
48+
const currentVersion = await err.json()
49+
return res.status(409).json(currentVersion)
50+
}
51+
throw new Error(`Error in overwrite request: ${err.status} ${err.statusText}`)
52+
})
53+
if(res.headersSent) return
54+
const result = await response.json()
55+
if(response.status === 200) {
56+
res.setHeader("Location", result["@id"] ?? result.id)
57+
res.status(200)
58+
}
3059
res.send(result)
3160
}
32-
catch (err) {
33-
next(err)
61+
catch (err) {
62+
console.log(err)
63+
res.status(500).send("Caught Error:" + err)
3464
}
3565
})
3666

0 commit comments

Comments
 (0)