如何在 jQuery.terminal 中使用 exceptionHandler。

huangapple go评论56阅读模式
英文:

how do i use exceptionHandler in jquery.terminal

问题

我正在使用不同的jQuery终端API进行某些操作,但如果用户出错,错误消息看起来像这样

如何在 jQuery.terminal 中使用 exceptionHandler。

代码:

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');
},

我的问题是如何在可能出现错误时发送一个小消息。

英文:

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

如何在 jQuery.terminal 中使用 exceptionHandler。

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.

答案1

得分: 1

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");
};
英文:

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");
};

答案2

得分: 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."

"异常很好,因为您可以轻松找到错误发生的位置,但如果您想隐藏它,那么您可以仅使用exceptionHandler选项来实现。但请注意,如果您有一个Promise,您需要返回它,否则终端将无法看到该Promise的拒绝。"

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.

"看来标签是错误的,[Command]表示这是一个内部错误,但实际上只是来自被拒绝的Promise的用户错误。"

英文:

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.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

term.exec(&#39;foo&#39;);
term.exec(&#39;bar&#39;);


function async_function() {
  return Promise.resolve();
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;script src=&quot;https://code.jquery.com/jquery-3.3.1.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/jquery.terminal/js/jquery.terminal.min.js&quot;&gt;&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/jquery.terminal/css/jquery.terminal.min.css&quot;/&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

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.

huangapple
  • 本文由 发表于 2023年4月17日 08:23:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030965.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定