英文:
how do i use exceptionHandler in jquery.terminal
问题
我正在使用不同的jQuery终端API进行某些操作,但如果用户出错,错误消息看起来像这样
代码:
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
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 = $('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();
}
<!-- language: lang-html -->
<!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>
<!-- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论