Skip to content
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

Draft implementation of server.on(handle-error, ...) #79

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

OnkelTem
Copy link

It's a follow-up of this ticket: #78

Now it should be possible to hide errors for example:

  // Hide other errors
  server.on('handle-error', () => {});

Still needs works.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Member

@pimterry pimterry left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! I know you're still working on this, no worries, but a few quick comments that might be helpful on the code so far:

src/mockttp.ts Outdated
* Subscribe to hear about requests that fail before successfully sending their
* initial parameters (the request line & headers). This will fire for requests
* that drop connections early, send invalid or too-long headers, or aren't
* correctly parseable in some form.
Copy link
Member

Choose a reason for hiding this comment

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

This comment needs updating

src/mockttp.ts Outdated
@@ -576,7 +593,8 @@ export type SubscribableEvent =
| 'response'
| 'abort'
| 'tls-client-error'
| 'client-error';
| 'client-error'
| 'handle-error';
Copy link
Member

Choose a reason for hiding this comment

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

I think this should be handler-error - that's slightly clearer (since handle has other meanings, like file handles). This is basically an event you can subscribe to to hear about any request handlers which throw errors at any point during request processing.

// TODO: This is just a copy of the previous client-error section. Needs to be finished.
'handle-error': gql`subscription OnError {
failedRequest {
errorCode
Copy link
Member

Choose a reason for hiding this comment

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

To make this work, you'll need to:

I think that should be it, and then the identical API will work in browser & remote clients automatically too.

@pimterry
Copy link
Member

Oh, can you add tests for this please? I think you can base them on any of the other subscription types in here: https://github.com/httptoolkit/mockttp/tree/main/test/integration/subscriptions. You can probably use your ENOTFOUND dns error as a good test case to reproduce this I think 😄

@OnkelTem
Copy link
Author

OnkelTem commented May 11, 2022

Sure I'll try.

But I have a question. I started to used the ErrorLike type in mockttp-server.ts, but now typedoc is barking:

Warning: ErrorLike, defined at src/util/error.ts:2, is referenced by MockttpServer.on.on.callback.__type.__type.error but not included in the documentation.

I'm not sure where should I add a description. I added it to src/util.ts but it didn't make it happy.

@OnkelTem
Copy link
Author

What do you think about renaming handle-error to server-error?

@pimterry
Copy link
Member

It's not all server errors though - it's only errors during the request handling. There's all sorts of other server errors we can hit in other places, like low-level socket handling or during server startup. I've looked at adding an event for all this before, although I think I remember it being a bit more difficult than it sounds...

Anyway, how about request-handler-error? That makes it very explicit, and that would also differentiate it from websocket-handler-error which we'll probably want in future (with different params) for consistency there.

@pimterry
Copy link
Member

But I have a question. I started to used the ErrorLike type in mockttp-server.ts, but now typedoc is barking

This means you need to export the ErrorLike type from the package root, so that people can use it themselves, like import { ErrorLike } from 'mockttp'.

It's being a bit overly strict really, but it is still useful because it makes sure that all the types you might need are definitely directly accessible outside the package.

To fix that you just need to add an export type { ErrorLike } from './util/error' in main.ts.

src/mockttp.ts Outdated
*
* @category Events
*/
on(event: 'handle-error', callback: (error: ErrorLike) => void): Promise<void>;
Copy link
Member

Choose a reason for hiding this comment

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

Do you think it would be useful to include the request that failed here too as a second parameter, something like (error, req) => { ... }?

That would be as an InitiatedRequest I think, since we don't actually know if it completed. Or maybe we can do InitiatedRequest | CompletedRequest, I'm just not sure if we have an easy way to provide the completed data only if it's ready, without waiting if it's not...

…s RequestHandlerError type; added gql schema and builder; no tests yet
@OnkelTem
Copy link
Author

Updated PR, not finished yet: tests to come.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants