-
Notifications
You must be signed in to change notification settings - Fork 3
Throughput RERUM Error Responses #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2d89e44
8a8254f
3d56611
4e35b80
3eaf345
2565f70
d33245d
95ef3f5
892f353
6e59876
575b17a
983eaed
dec5ed5
92e8d1e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /** | ||
| * Errors from RERUM are a response code with a text body (except those handled specifically upstream) | ||
| * We want to send the same error code and message through. It is assumed to be RESTful and useful. | ||
| * This will also handle generic (500) app level errors, as well as app level 404 errors. | ||
| * | ||
| * @param rerum_error_res A Fetch API Response object from a fetch() to RERUM that encountered an error. Explanatory text is in .text(). In some cases it is a unhandled generic (500) app level Error. | ||
| * @param req The Express Request object from the request into TinyNode | ||
| * @param res The Express Response object to send out of TinyNode | ||
| * @param next The Express next() operator, unused here but required to support the middleware chain. | ||
| */ | ||
| export async function messenger(rerum_error_res, req, res, next) { | ||
| if (res.headersSent) { | ||
| return | ||
| } | ||
| let error = {} | ||
| let rerum_err_text | ||
| try { | ||
| // Unless already handled upstream the rerum_error_res is an error Response with details as a textual body. | ||
| rerum_err_text = await rerum_error_res.text() | ||
| } | ||
| catch (err) { | ||
| // It is some 500 | ||
| rerum_err_text = undefined | ||
| } | ||
| if (rerum_err_text) error.message = rerum_err_text | ||
| else { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You know I would have put a ternary because I hate "else" but it is kinda an else anyway.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh yeah i might have had more than one line in there at one point |
||
| // Perhaps this is a more generic 500 from the app, perhaps involving RERUM, and there is no good rerum_error_res | ||
| error.message = rerum_error_res.statusMessage ?? rerum_error_res.message ?? `A server error has occured` | ||
| } | ||
| error.status = rerum_error_res.statusCode ?? rerum_error_res.status ?? 500 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is correct and I hate it
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a trip |
||
| console.error(error) | ||
| res.set("Content-Type", "text/plain; charset=utf-8") | ||
| res.status(error.status).send(error.message) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This means messenger can never have a payload...
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now. We didn't respond anywhere with a payload. If we end up wanting to do that we can upgrade the messenger. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,26 +37,31 @@ router.put('/', checkAccessToken, async (req, res, next) => { | |
| } | ||
|
|
||
| const overwriteURL = `${process.env.RERUM_API_ADDR}overwrite` | ||
| let errored = false | ||
| const response = await fetch(overwriteURL, overwriteOptions) | ||
| .then(resp=>{ | ||
| if (!resp.ok) throw resp | ||
| return resp | ||
| }) | ||
| .catch(async err => { | ||
| // Handle 409 conflict error for version mismatch | ||
| if (err.status === 409) { | ||
| const currentVersion = await err.json() | ||
| return res.status(409).json(currentVersion) | ||
| .then(async rerum_res=>{ | ||
| if (rerum_res.ok) return rerum_res.json() | ||
| errored = true | ||
| if (rerum_res.headers.get("Content-Type").includes("json")) { | ||
| // Special handling. This does not go through to error-messenger.js | ||
| if (rerum_res.status === 409) { | ||
| const currentVersion = await rerum_res.json() | ||
| return res.status(409).json(currentVersion) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sends it, it doesn't keep going. |
||
| } | ||
| } | ||
| throw new Error(`Error in overwrite request: ${err.status} ${err.statusText}`) | ||
| return rerum_res | ||
| }) | ||
| .catch(err => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why catch to only throw?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rest of them do it, so I thought maybe we did it for a reason. At least doing it to be consistent. |
||
| throw err | ||
| }) | ||
| if(res.headersSent) return | ||
| const result = await response.json() | ||
| // Send RERUM error responses to error-messenger.js | ||
| if (errored) return next(response) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The order of this makes me a little uneasy, but it works.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes trying to keep separate what errors are here. |
||
| const result = response | ||
| const location = result?.["@id"] ?? result?.id | ||
| if (location) { | ||
| res.setHeader("Location", location) | ||
| } | ||
| res.status(response.status ?? 200) | ||
| res.status(200) | ||
| res.json(result) | ||
| } | ||
| catch (err) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically as middle ware I think the rerum response would come last but I see you