Refactor javascript

Refactor javascript to catch error and pass to ruby
Add error catching

Add the error catching from the javascript layer to the ruby