Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i use exceptionHandler in jquery.terminal

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.

like image 749
Severus Avatar asked Mar 09 '26 18:03

Severus


1 Answers

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.

like image 162
jcubic Avatar answered Mar 11 '26 08:03

jcubic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!