3

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.

jcubic
  • 61,973
  • 54
  • 229
  • 402
Severus
  • 33
  • 3

2 Answers2

1

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");
};
perona chan
  • 101
  • 1
  • 8
1

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.

jcubic
  • 61,973
  • 54
  • 229
  • 402
  • if the user does not make an error in the first code, but does in the second, the error is ignoring and the 1st data is displaying like this (https://files.catbox.moe/7pyrz2.jpeg) – Severus Apr 18 '23 at 08:17
  • @Severus which second? You have only one code in your question. My demo has two examples and both work. I've explicitly written that you need to return a promise from The interpreter if you don't, the library will not know that there is rejected promise. It's like throwing an error outside of the terminal. Can you show your code? Without seeing your code nothing I can help with. – jcubic Apr 19 '23 at 10:35