how do i use exceptionHandler in jquery.terminal

72 views Asked by At

I'm doing something using different apis in jquery terminal but, If the user makes a mistake, the error message looks like this

my code which uses github api

code:

gemini: function(a){
  $.ajaxSetup({async: false});
  $.get('https://api.github.com/repos/'+a, function(x){
    b = x.name;
    c = x.id;
    d = x.license.name;
    e = x.svn_url;
  });
  this.echo('name: '+b);
  this.echo('id: '+c);
  this.echo('license: '+d)
  this.echo('.zip: '+e+'/archive/master.zip');
},

My question is how can I send a small message in a possible error.

2

There are 2 answers

2
jcubic On BEST ANSWER

The exception is nice because you can easily find where the error happens, but if you want to hide it then you have exceptionHandler option that you can use just for that. But note that if you have a Promise you need to return it otherwise the terminal will not see the rejection of that promise.

const term = $('body').terminal(function(command) {
   if (command === 'foo') {
     this.echo(x);
   } else if (command === 'bar') {
     return async_function().then(() => {
        this.echo(x);
     });
   }
}, {
  exceptionHandler: function(e) {
    this.error(e.message);
  }
});

term.exec('foo');
term.exec('bar');


function async_function() {
  return Promise.resolve();
}
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery.terminal/js/jquery.terminal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery.terminal/css/jquery.terminal.min.css"/>
</head>
<body>
</body>
</html>

And it seems that the label is wrong, [Command] means that it was an internal error, but it was a user error only from a rejected promise.

0
perona chan On

javascript still needs the async await code to wait for the result of the data request. fetch code is available in browser to replace jquery

var gemini = async (a) => {
  var x = await fetch("https://api.github.com/repos/" + a);
  var b = x.name;
  var c = x.id;
  var d = x.license.name;
  var e = x.svn_url;
  this.echo("name: " + b);
  this.echo("id: " + c);
  this.echo("license: " + d);
  this.echo(".zip: " + e + "/archive/master.zip");
};