@@ -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 += `
5256Your 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 += `
6064The body of your request was invalid. Please make sure it is a valid content-type and that the body matches that type.
6165If 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 += `
6771The token provided is Unauthorized. Please check that it is your token and that it is not expired.
6872Token: ${ token } `
6973 }
7074 else {
71- err . message += `
75+ error . message += `
7276The request does not contain an "Authorization" header and so is Unauthorized. Please include a token with your requests
7377like "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 += `
8084You are Forbidden from performing this action. Check your privileges.
8185Token: ${ token } `
8286 }
@@ -87,24 +91,28 @@ You are Forbidden from performing this action. The request does not contain an "
8791Make sure you have registered at ${ process . env . RERUM_PREFIX } . `
8892 }
8993 case 404 :
90- err . message += `
94+ error . message += `
9195The 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 += `
103109RERUM experienced a server issue while performing this action.
104110It 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
110118export default { checkPatchOverrideSupport, messenger }
0 commit comments