-
-
Notifications
You must be signed in to change notification settings - Fork 241
Add error
property to "degraded" events
#6188
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
base: main
Are you sure you want to change the base?
Conversation
@metamaskbot publish-previews |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
error
property to "degraded" events
[Data] extends [void] | ||
? (data: AdditionalData) => void | ||
: (data: Data & AdditionalData) => void | ||
? (data: Data extends void ? AdditionalData : Data & AdditionalData) => void |
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.
This was an interesting one to figure out.
The purpose of this utility type is to extend the type for a Cockatiel event with additional payload data. We need to do this because NetworkController:rpcEndpointDegraded
and NetworkController:rpcEndpointAvailable
are based on existing Cockatiel events but with additional properties we add.
However, this type did not properly account for when EventListener
was CockatielEvent<FailureReason<unknown> | void>
(which is the same thing as saying (data: void | { error: Error } | { value: unknown }) => void
).
If we say the following
AddToCockatielEventData<
(data: void | { error: Error } | { value: unknown }) => void,
{ foo: 'bar' }
>
we want this to resolve to:
(data: { foo: 'bar' } | ({ error: Error } & { foo: 'bar' }) | ({ value: unknown } & { foo: 'bar' })) => void
(Essentially, we want the void
to be treated as {}
.)
But instead this was resolving as:
(data: ((void | { error: Error } | { value: unknown }) & { foo: 'bar' }) => void
which distributes to:
(data: (void & { foo: 'bar' }) | ({ error: Error } & { foo: 'bar' }) | ({ value: unknown } & { foo: 'bar' })) => void
This is incorrect, because void & { foo: 'bar' }
doesn't make sense.
What we want is for TypeScript to distribute void | { error: Error } | { value: unknown }
over the condition. So first, despite the comment here, we don't want [Data] extends [void]
, because that prevents TypeScript from applying the distribution.
But the second thing is that even if we say Data extends void
then TypeScript still won't apply the distribution, because the condition is actually in the wrong place. What we want is not a type union of three function types, but a single type with a type union for the arguments.
Even if the condition were in the place we wanted it, TypeScript seems to distribute the type union only in a particular circumstance. Looking a bit deeper at the TypeScript 2.8 release notes where this concept was introduced, it says: "Distributive conditional types are automatically distributed over union types during instantiation." In this case, we also need to place the condition so it becomes a part of the type that we want to "return" from this utility type and is not used to simply determine the return type. This way TypeScript evaluates the condition when that return type is used and not beforehand.
Explanation
We would like to start tracking the HTTP status code in metrics when the
NetworkController:rpcEndpointDegraded
event is published because the maximum number of retries for a request has been exceeded. The HTTP status is stored in the error that Cockatiel captures, so we just need to include it in the event payload.References
Closes #6190.
Checklist