How To: Access Full Error Code Descriptions When Using SteemConnect v.2 in your dApps To Interact With the Steem Blockchain.

in steemdev •  2 months ago  (edited)

If you have ever attempted to use SteemConnect to interact with the Steem blockchain, you may have found that there is no support for accessing/processing proper error codes from Steem - this code lets you fix that.

I have recently been adding Steem support to my social network at ureka.org and found that the information returned by SteemConnect's server when errors occur is lacking any meaningful information that I could relay to the user of my site. We need to know when our attempt to interact with the blockchain fails and importantly why it has failed. Currently, SteemConnect does not relay the error_description text to the app that is using SteemConnect, so the best we can easily do is give a generic error to the user. This isn't really good enough for me, so I have put together some Javascript that resolves this.

The following code works because the actual error_description text IS returned by the SteemConnect server, but isn't accessed/used by the current version of the SteemConnect SDK. As I understand from @fabien, the SteemConnect team are aware of this issue and it will be fixed in SteemConnect v.3, but not in the current version 2. My code is a vanilla Javascript function that you can add to your own javascript file that:

  • Intercepts the HTTP Request objects that get sent between the browser and other servers.
  • Listens for messages received from the SteemConnect api server.
  • Extracts the error_description text if it exists and produces a browser alert.

The browser alert is obviously only here to show you, the developer, the errors that are being sent and you would replace the alert command with something more useful for your app and end users. You can also grab other useful data about the response and error from the result_codes variable.

I haven't found anything about this issue online that provides any help, so I hope this is read by anyone wanting to use SteemConnect and saves them some headaches. Note: This took me over 4 hours to find a solution for, so upvotes and resteems are welcome! :)
(Also note that Utopian won't accept this as a submission because it isn't going into a whitelisted repo as SteemConnect 'won't fix').

(function(open) {
  XMLHttpRequest.prototype.open = function(method, url, async, user, pass)
  {
    if (url == 'https://steemconnect.com/api/broadcast')
    {
      this.addEventListener("readystatechange", function() 
      {
        if (this.readyState == 4)
        {
          var reader = new FileReader();
          reader.addEventListener("loadend", function() 
          {
            var result_codes = (JSON.parse(reader.result));
            if (result_codes.error_description)
              alert(result_codes.error_description);
          });
          reader.readAsBinaryString(this.response);
        }
      }, false);
    }
  open.call(this, method, url, async, user, pass);
  };
})(XMLHttpRequest.prototype.open);

Wishing you well,
Ura Soul


You Can Vote For Me As A Steem Witness!

Click the big black button below:


vote ura-soul for witness

View My Witness Application Here

View Some of My Witness Related Posts


(Witnesses are the computer servers that run the Steem Blockchain.
Without witnesses there is no Steem, Steemit, DTube, Utopian or
Busy... You can really help Steem by making your 30 witness votes count!)



steem ocean - diving deep into the blockchain

Find out your voter rank position at steemocean.com!



tribesteemup-orange-banner.png



ureka.org

I run a social network too!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
  ·  2 months ago (edited)

Looks like it could help me out hehe. I can just import this anywhere in my react node project and it will act as a "middleware" to the default XMLHttpRequest by replacing the open method/function? If not, please elaborate on how to use it exactly ;) Thanks!

·

Great! I don't use REACT, so I don't know for sure, but in a 'normal' Javascript file (or JQuery, which is what I am using) you can just drop it in anywhere and it will become active, yes.

It is effectively a hook into the XMLHttpRequest object.

Loading...

Hi @ura-soul!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your UA account score is currently 6.977 which ranks you at #92 across all Steem accounts.
Your rank has not changed in the last three days.

In our last Algorithmic Curation Round, consisting of 218 contributions, your post is ranked at #3. Congratulations!

Evaluation of your UA score:
  • You've built up a nice network.
  • The readers appreciate your great work!
  • Great user engagement! You rock!

Feel free to join our @steem-ua Discord server