Skip to content

fix: Retain status codes when combining errors - #170

Merged
jvherweg merged 1 commit into
masterfrom
composite-errors
Oct 2, 2020
Merged

jvherweg merged 1 commit into
masterfrom
composite-errors

Conversation

@jvherweg

Copy link
Copy Markdown
Member

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.

@jvherweg jvherweg added the 🐛 bug Something isn't working label Sep 30, 2020
Comment thread src/util/CompositeAsyncHandler.ts Outdated
break;
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the algorithm a bit weird (but it seems to work).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/util/CompositeAsyncHandler.ts Outdated

throw new UnsupportedHttpError(`No handler supports the given input: [${joined}].`);
if (last.statusCode === 500) {
throw new Error(message);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not HTTP? This components does not know what happens down the line.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See remark above, we might want to do that more explicitly. Also pertains to #6.

@RubenVerborgh

Copy link
Copy Markdown
Member

None of these are major issues BTW, just points for reflection.

@jvherweg

jvherweg commented Oct 1, 2020

Copy link
Copy Markdown
Member Author

Simplified the code

@jvherweg
jvherweg requested a review from RubenVerborgh October 1, 2020 11:22

@RubenVerborgh RubenVerborgh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍

Comment thread src/util/CompositeAsyncHandler.ts Outdated

// Find the error range (4xx or 5xx)
const max = Math.max(...errors.map((error): number => error.statusCode));
if (max >= 500) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: could use ? : here to make it shorter.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's either one change or the other, both would make it too long :P. (Went for the first one).

@jvherweg
jvherweg merged commit 10723bb into master Oct 2, 2020
@jvherweg
jvherweg deleted the composite-errors branch October 2, 2020 07:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐛 bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Don't lose status codes in CompositeAsyncHandler

2 participants