[json-rpc-engine] Improve handling for unexpected middleware error types, align error types to use `JsonRpcEngineCallbackError`
Motivation
Currently, JsonRpcEngine handles non-JSON-RPC errors thrown from middleware by passing them into end callbacks without validation or serialization.
Fixing this is necessary to take full advantage of the provider alignment technical proposals, as JsonRpcEngine is the call site for the provider methods. A more accurately typed JsonRpcEngine would enable us to make potential typing improvements to the providers as well.
Explanation
The code for handling these errors suppresses valid errors by using type assertions (as JsonRpcEngineCallbackError), despite their being no guarantee that the thrown errors will be of that type. These assertions (currently marked by TODO comments) should be removed and replaced with type-safe code.
We should align error types used throughout middleware the chain with the JsonRpcEngineCallbackError type. This may involve coming up with a scheme for lossless serialization of errors that explicitly preserves information that is expected by JsonRpcEngine.
References
- Follows from https://github.com/MetaMask/core/issues/3720
- Left 3 TODOs in the
JsonRpcEngineclass
- Left 3 TODOs in the
- See previous effort https://github.com/MetaMask/json-rpc-engine/pull/84
@MajorLift is this effectively resolved by #6176?