Skip to content

Commit 0640e64

Browse files
committed
Use httpError helper and payload for errors
Standardize error handling by introducing/using httpError and a `payload` property for error bodies. rest.js now sets error.payload instead of errorBody/body. error-messenger.js was simplified to send rerum_error_res.payload and dropped the ReadableStream/body detection logic. Routes (overwrite, query, update) now import and throw httpError for validation and upstream RERUM errors instead of creating Error objects and manually setting status codes. This centralizes HTTP error construction and response payload handling.
1 parent a85edbc commit 0640e64

5 files changed

Lines changed: 12 additions & 34 deletions

File tree

error-messenger.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,14 @@ export async function messenger(rerum_error_res, req, res, next) {
1414
return
1515
}
1616

17-
const hasReadableStream = typeof globalThis.ReadableStream === "function"
18-
const explicitBody = rerum_error_res.errorBody ?? (
19-
rerum_error_res.body !== undefined
20-
&& (!hasReadableStream || !(rerum_error_res.body instanceof globalThis.ReadableStream))
21-
? rerum_error_res.body
22-
: undefined
23-
)
24-
2517
const error = {
2618
message: rerum_error_res.statusMessage ?? rerum_error_res.message ?? "A server error has occurred",
2719
status: rerum_error_res.statusCode ?? rerum_error_res.status ?? 500,
28-
body: explicitBody
2920
}
3021

31-
if (error.body !== undefined) {
22+
if (rerum_error_res.payload !== undefined) {
3223
console.error(error)
33-
res.status(error.status).json(error.body)
24+
res.status(error.status).json(rerum_error_res.payload)
3425
return
3526
}
3627

rest.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ export function httpError(message, status = 500, body) {
77
const error = new Error(message)
88
error.status = status
99
if (body !== undefined) {
10-
error.errorBody = body
11-
error.body = body
10+
error.payload = body
1211
}
1312
return error
1413
}

routes/overwrite.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express from "express"
22
import checkAccessToken from "../tokens.js"
3-
import { verifyJsonContentType } from "../rest.js"
3+
import { httpError, verifyJsonContentType } from "../rest.js"
44
import { createRerumNetworkError, fetchRerum } from "../rerum.js"
55
const router = express.Router()
66

@@ -12,9 +12,7 @@ router.put('/', verifyJsonContentType, checkAccessToken, async (req, res, next)
1212
const overwriteBody = req.body
1313
// check for @id; any value is valid
1414
if (!overwriteBody || !(overwriteBody['@id'] ?? overwriteBody.id)) {
15-
const err = new Error("No record id to overwrite! (https://store.rerum.io/API.html#overwrite)")
16-
err.status = 400
17-
throw err
15+
throw httpError("No record id to overwrite! (https://store.rerum.io/API.html#overwrite)", 400)
1816
}
1917

2018
const overwriteOptions = {

routes/query.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import express from "express"
2-
import { verifyJsonContentType } from "../rest.js"
2+
import { httpError, verifyJsonContentType } from "../rest.js"
33
import { fetchRerum } from "../rerum.js"
44
const router = express.Router()
55

@@ -14,17 +14,13 @@ router.post('/', verifyJsonContentType, async (req, res, next) => {
1414
// If there is an empty query with [] or {}, we consider that a query for all data,
1515
// which we don't want to allow. We will throw a 400 error.
1616
if (queryBody === '{}' || queryBody === '[]') {
17-
const err = new Error("Empty query is not allowed. Please provide a valid query in the request body.")
18-
err.status = 400
19-
throw err
17+
throw httpError("Empty query is not allowed. Please provide a valid query in the request body.", 400)
2018
}
2119
// check limit and skip for INT
2220
if (Number.isNaN(Number.parseInt(lim, 10) + Number.parseInt(skip, 10))
2321
|| (lim < 0)
2422
|| (skip < 0)) {
25-
const err = new Error("`limit` and `skip` values must be non-negative integers or omitted.")
26-
err.status = 400
27-
throw err
23+
throw httpError("`limit` and `skip` values must be non-negative integers or omitted.", 400)
2824
}
2925

3026
const queryOptions = {
@@ -48,9 +44,7 @@ router.post('/', verifyJsonContentType, async (req, res, next) => {
4844
} catch (e) {
4945
rerumErrorMessage = `500: ${queryURL} - A RERUM error occurred`
5046
}
51-
const err = new Error(rerumErrorMessage)
52-
err.status = 502
53-
throw err
47+
throw httpError(rerumErrorMessage, 502)
5448
})
5549
res.status(200).json(rerumResponse)
5650
}

routes/update.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express from "express"
22
import checkAccessToken from "../tokens.js"
3-
import { verifyJsonContentType } from "../rest.js"
3+
import { httpError, verifyJsonContentType } from "../rest.js"
44
import { createRerumNetworkError, fetchRerum } from "../rerum.js"
55
const router = express.Router()
66

@@ -10,9 +10,7 @@ router.put('/', verifyJsonContentType, checkAccessToken, async (req, res, next)
1010
try {
1111
// check for @id; any value is valid
1212
if (!req.body || !(req.body['@id'] ?? req.body.id)) {
13-
const err = new Error("No record id to update! (https://store.rerum.io/API.html#update)")
14-
err.status = 400
15-
throw err
13+
throw httpError("No record id to update! (https://store.rerum.io/API.html#update)", 400)
1614
}
1715

1816
const updateBody = JSON.stringify(req.body)
@@ -37,9 +35,7 @@ router.put('/', verifyJsonContentType, checkAccessToken, async (req, res, next)
3735
} catch (e) {
3836
rerumErrorMessage = `500: ${updateURL} - A RERUM error occurred`
3937
}
40-
const err = new Error(rerumErrorMessage)
41-
err.status = 502
42-
throw err
38+
throw httpError(rerumErrorMessage, 502)
4339
})
4440
if (!(rerumResponse.id || rerumResponse["@id"])) {
4541
// A 200 with garbled data, call it a fail

0 commit comments

Comments
 (0)