Skip to content

Commit ae76332

Browse files
committed
Error redo
1 parent 4e0afaa commit ae76332

3 files changed

Lines changed: 33 additions & 29 deletions

File tree

db-controller.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,9 +1578,9 @@ async function newTreePrime(obj) {
15781578
* @param {Error} originalError `source` for tracing this Error
15791579
* @returns Error for use in Express.next(err)
15801580
*/
1581-
function createExpressError(update, originalError = {}) {
1582-
let err = Error("detected error", { cause: originalError })
1583-
if (update.code) {
1581+
function createExpressError(err) {
1582+
let error = {}
1583+
if (err.code) {
15841584
/**
15851585
* Detection that createExpressError(error) passed in a mongo client error.
15861586
* IMPORTANT! If you try to write to 'update' when it comes in as a mongo error...
@@ -1591,24 +1591,20 @@ function createExpressError(update, originalError = {}) {
15911591
* If you do update.statusMessage or update.statusCode YOU WILL CAUSE THIS ERROR.
15921592
* Make sure you write to err instead. Object.assign() will have the same result.
15931593
*/
1594-
switch (update.code) {
1594+
switch (err.code) {
15951595
case 11000:
15961596
//Duplicate _id key error, specific to SLUG support. This is a Conflict.
1597-
err.statusMessage = `The id provided in the Slug header already exists. Please use a different Slug.`
1598-
err.statusCode = 409
1597+
error.statusMessage = `The id provided already exists. Please use a different _id or Slug.`
1598+
error.statusCode = 409
15991599
break
16001600
default:
1601-
err.statusMessage = "There was a mongo error that prevented this request from completing successfully."
1602-
err.statusCode = 500
1601+
error.statusMessage = "There was a mongo error that prevented this request from completing successfully."
1602+
error.statusCode = 500
16031603
}
16041604
}
1605-
else {
1606-
//Warning! If 'update' is considered sent, this will cause a 500. See notes above.
1607-
update.statusMessage = update.message
1608-
update.statusCode = update.status
1609-
}
1610-
Object.assign(err, update)
1611-
return err
1605+
if(!err.statusCode) error.statusCode === "500"
1606+
if(!err.statusMessage) error.statusMessage === "Detected Error"
1607+
return error
16121608
}
16131609

16141610
/**

rest.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,47 +36,51 @@ const messenger = function (err, req, res, next) {
3636
next(err)
3737
return
3838
}
39-
err.message = err.message ?? res.message ?? ``
40-
if (err.statusCode === 401) {
39+
console.log("Error into messenger")
40+
console.log(err)
41+
let error = {}
42+
error.message = err.statusMessage ?? err.message ?? res.statusMessage ?? res.message ?? ``
43+
error.status = err.statusCode ?? res.statusCode ?? 500
44+
if (error.status === 401) {
4145
//Special handler for token errors from the oauth module
4246
//Token errors come through with a message that we want. That message is in the error's WWW-Authenticate header
4347
//Other 401s from our app come through with a status message. They may not have headers.
4448
if (err.headers?.["WWW-Authenticate"]) {
45-
err.message += err.headers["WWW-Authenticate"]
49+
error.message += err.headers["WWW-Authenticate"]
4650
}
4751
}
4852
let genericMessage = ""
4953
let token = req.header("Authorization")
5054
if(token && !token.startsWith("Bearer ")){
51-
err.message +=`
55+
error.message +=`
5256
Your token is not in the correct format. It should be a Bearer token formatted like: "Bearer <token>"`
5357
next(err)
5458
return
5559
}
56-
switch (err.statusCode) {
60+
switch (error.status) {
5761
case 400:
5862
//"Bad Request", most likely because the body and Content-Type are not aligned. Could be bad JSON.
59-
err.message += `
63+
error.message += `
6064
The body of your request was invalid. Please make sure it is a valid content-type and that the body matches that type.
6165
If the body is JSON, make sure it is valid JSON.`
6266
break
6367
case 401:
6468
//The requesting agent is known from the request. That agent does not match __rerum.generatedBy. Unauthorized.
6569
if (token) {
66-
err.message += `
70+
error.message += `
6771
The token provided is Unauthorized. Please check that it is your token and that it is not expired.
6872
Token: ${token} `
6973
}
7074
else {
71-
err.message += `
75+
error.message += `
7276
The request does not contain an "Authorization" header and so is Unauthorized. Please include a token with your requests
7377
like "Authorization: Bearer <token>". Make sure you have registered at ${process.env.RERUM_PREFIX}.`
7478
}
7579
break
7680
case 403:
7781
//Forbidden to use this. The provided Bearer does not have the required privileges.
7882
if (token) {
79-
err.message += `
83+
error.message += `
8084
You are Forbidden from performing this action. Check your privileges.
8185
Token: ${token}`
8286
}
@@ -87,24 +91,28 @@ You are Forbidden from performing this action. The request does not contain an "
8791
Make sure you have registered at ${process.env.RERUM_PREFIX}. `
8892
}
8993
case 404:
90-
err.message += `
94+
error.message += `
9195
The requested web page or resource could not be found.`
9296
break
9397
case 405:
9498
// These are all handled in api-routes.js already.
9599
break
100+
case 409:
101+
break
96102
case 503:
97103
//RERUM is down or readonly. Handled upstream.
98104
break
99105
case 500:
100106
default:
101107
//Really bad, probably not specifically caught.
102-
err.message += `
108+
error.message += `
103109
RERUM experienced a server issue while performing this action.
104110
It may not have completed at all, and most likely did not complete successfully.`
105111
}
106-
// res.status(statusCode).send(err.statusMessage)
107-
next(err)
112+
console.log("Error out with res")
113+
console.log(error)
114+
res.set("Content-Type", "text/plain; charset=utf-8")
115+
res.status(error.status).send(error.message).end()
108116
}
109117

110118
export default { checkPatchOverrideSupport, messenger }

routes/api-routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ router.use('/history', historyRouter)
8080
*/
8181
router.use((req, res, next) => {
8282
const code = res?.statusCode
83-
const handledCodes = [401, 403, 405, 500, 501]
83+
const handledCodes = [401, 403, 405, 409, 500, 501]
8484
let msg = res.statusMessage ?? `${code} Route Error`
8585
if(code && handledCodes.includes(code)) {
8686
// It was handled upstream in a route file. It is already the right error and message so send it out.

0 commit comments

Comments
 (0)