Repository navigation
fix: Retain status codes when combining errors - #170
Conversation
| break; | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
I find the algorithm a bit weird (but it seems to work).
There was a problem hiding this comment.
The idea is: if they all have the same status code, return that. If they differ, return 400. If any of them is a normal Error (which is a 500), do that.
There was a problem hiding this comment.
I'd just collect status codes in an array then with a map, and do these operations on that array.
It's not as much nits about the code as it it about the fact that we have to make it this complicated; it might be a sign of something else being wrong (like the "normal Error = 500" bit being implicitly coded here).
There was a problem hiding this comment.
Regarding the code: could also write it like something as follows should that be better
if (errors.some(error => error.statusCode === 500))
throw 500 error;
if (all errors have same status code)
throw that specific error;
throw 400 error;I just wrote it like I did to only have to loop only once through the array but in hindsight the above would probably be clearer.
The normal error = 500 thing is something that we need to have anyway, since we actually never want 500 errors, and they are usually an indication that something somewhere threw an error that we didn't catch (because it's not supposed to throw an error in normal circumstances, e.g. modifyResource being called on the file store). I could explicitly create an HttpError with 500 here if that would be clearer.
I also had #6 in mind when writing this, but this doesn't actually change anything about this issue. Should we change how we handle errors this can easily be changed here as well. But I don't want to let #23 hang because of that since it's seems important to have correct status codes and it only requires a small change to do so.
|
|
||
| throw new UnsupportedHttpError(`No handler supports the given input: [${joined}].`); | ||
| if (last.statusCode === 500) { | ||
| throw new Error(message); |
There was a problem hiding this comment.
Why not HTTP? This components does not know what happens down the line.
There was a problem hiding this comment.
Could do that, would not make a difference though since normal Errors are interpreted as 500 errors at the end (to make sure we always catch all errors that occur).
There was a problem hiding this comment.
See remark above, we might want to do that more explicitly. Also pertains to #6.
|
None of these are major issues BTW, just points for reflection. |
de275dc to
faf5d26
Compare
|
Simplified the code |
|
|
||
| // Find the error range (4xx or 5xx) | ||
| const max = Math.max(...errors.map((error): number => error.statusCode)); | ||
| if (max >= 500) { |
There was a problem hiding this comment.
Nit: we don't need to max; rather errors.any(e => e.statusCode >= 500).
| if (max >= 500) { | ||
| throw new InternalServerError(message); | ||
| } | ||
| throw new UnsupportedHttpError(message); |
There was a problem hiding this comment.
Nit: could use ? : here to make it shorter.
There was a problem hiding this comment.
It's either one change or the other, both would make it too long :P. (Went for the first one).
faf5d26 to
e4cefa4
Compare
Resolves #23 .
This does put some status code logic in the CompositeAsyncHandler but not sure how to do this otherwise since this is quite ingrained.