Skip to content

Spec Proposal: Switch to ErrorCodes instead of pre-defined error classes #41

@grunklejp

Description

@grunklejp

In the docs, we're told to check for a provider error based on the Error constructor value like so:

  if (err.constructor === MissingProviderError) {
    message = "Check out https://lightningjoule.com to get a WebLN provider";
  }

I think if we used an int instance property errorCode instead it would de-couple the webln spec from this specific implementation. We could call the property something like webLnErrorCode and have the values be an int.

Benefits to Providers

  • Can more easily write & throw their own error messages
  • It's much more explicit & 'spec-like' so providers will feel more compelled to meet this requirement (looking at you Alby... 😠 )

Benefits to webln supporting Apps

  • ❗️Apps can now more effortlessly know if an error was thrown by the provider
catch(e){
  if (e.webLnErrorCode) {
    // we know that its a webln related  error
  }
}
// Instead of checking against every single webln error class
  • No longer have to import a bunch of Error classes to know if the provider threw an error

Rough mapping

Code Current Error message Description
0 MissingProviderError Thrown when requestProvider doesn't find a provider. This is a good one to catch to direct users to install an extension or browser that supports WebLN, especially if you have a favorite.
1 RejectionError UserRejectionError Thrown by providers when a user indicates that they don't want to complete a request from the application.
2 ConnectionError Thrown by providers when the node the provider use could not be reached for connection reasons (Either the user's network is down, or the node's network is down.)
3 UnsupportedMethodError Providers that only partially implement the WebLN spec can throw this error for methods they don't support.
4 RoutingError Thrown by providers when a node couldn't be routed to. This is a good time to prompt users to add your node as a peer, or open a channel.
6 InvalidDataError Thrown by providers if some data passed by the application is incorrect, such as a malformed BOLT-11 payment request.
7...98 Reserved Reserved for future error codes
99 InternalError A catch-all for errors that happened in the provider that the app can't really do anything about.

The best for last

  • We can do this spec upgrade in a non-breaking way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions