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

ToError FromError Protocol Changes #34

Merged
merged 8 commits into from
Oct 9, 2023
Merged

Conversation

amydevs
Copy link
Contributor

@amydevs amydevs commented Oct 9, 2023

Description

FromError/ToError

function toError(errorData: JSONValue): any {}
function fromError(error: any): JSONValue {}

fromError will need to be any -> JSONValue
If it is an error or a serialisable value it must be serialised, otherwise throw a TypeError indicating that it cannot

toError will need to be JSONValue -> any
unknown values will be thrown upwards as is, as the peer is able to throw anything they want that is JSON serializable

if the JSONValue passed into toError is a serialized error, and the error.type cannot be found, it will be deserialised into a generic Error

When fromError throws a TypeError, this will be dispatched on the RPCServer instance, to notify the user that most likely one of their handlers is throwing an non-serialisable value. This is also wrapped in an ErrorRPCRemote and sent to the client so that they know that an internal server error has occured due to an attempt to throw.

Error Object

There have been some changes regarding how errors are mapped to JSON-RPC messages.

A typical JSON-RPC message containing an ErrorRPCRemote would look like the following:

{
  jsonrpc: '2.0',
  error: { 
    code: -32006, // Maps to ErrorRPCRemote 
    message: '...', 
    data: {
      meta: { caller: '...' }, // <- DOES NOT EXIST ON TRANSPORT
      data: {

      },
      cause: {
        type: 'ErrorX...', // Maps to ErrorRPCRemote 
        message: '...', 
        data: {
          data: {

          },
          cause: {
            ... // recurse of parent structure
          },
          timestamp: new Date(),
          stack: "..."
        },
      },
      timestamp: new Date(),
      stack: "..."
    },
  }, 
  id: null
}

This means that all user-thrown application-level errors are now mapped to a type on an ErrorRPCRemote error object, rather than to an error object directly like before. This change was possibly due to a misunderstanding when toError/fromError was initially being speced out. But please correct this if that is not the case.

As of current, the only protocol-level errors that get transmitted between peers are ErrorRPCRemote, which is a wrapper for an application level error, with all other errors only being thrown on the client. However, the aforementioned change may help if we ever decide to introduce protocol-level errors that are transmitted.

Also, the error object.message now carries the error.message rather than the error.description. This has been done for the sake of convenience, it making more sense, and also the fact that the descriptions in all the Errors that extend ErrorRPCProtocol have a static description.

Issues Fixed

Tasks

  • 1. Make ToError/FromError correctly serialize and deserialize error details.
  • 2. Amend ErrorObject data-structure

Final checklist

  • Domain specific tests
  • Full tests
  • Updated inline-comment documentation
  • Lint fixed
  • Squash and rebased
  • Sanity check the final build

Sorry, something went wrong.

@ghost
Copy link

ghost commented Oct 9, 2023

👇 Click on the image for a new way to code review

Review these changes using an interactive CodeSee Map

Legend

CodeSee Map legend

amydevs and others added 6 commits October 9, 2023 20:33

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Import cycles, really confusing if you've never seen it before.

[ci skip]
….description, as all root level errors have static descriptions
@amydevs amydevs force-pushed the feature-toError-fromError branch from aa5cd37 to 298a5ab Compare October 9, 2023 09:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Customizable Error Filtering for js-rpc through User-Defined toError/fromError Functions
2 participants