如果在没有重写的情况下出现错误,如何重新编写代码部分?(How to re

编程入门 行业动态 更新时间:2024-10-28 20:18:30
如果在没有重写的情况下出现错误,如何重新编写代码部分?(How to re-do a code part if there is an error without rewriting it ?)

在我的代码中,我做了一些API调用,有时其中一个返回错误,但如果我在它工作之后重新执行它(我真的不知道什么是错的,也许我发布的JSON没有完成或者我没有知道...)

所以,为了使我编码:

HTTP.call("POST", "http://localhost:3000/api/dashboards/db", { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': APIKEY, }, data: { dashboard: dataJSON, overwrite: true } }, function(error, result) { if (!error) { console.error("result post dataJSON --------------------OK------------") } else { console.log("error post dataJSON --------------------KO------------") HTTP.call("POST", "http://localhost:3000/api/dashboards/db", { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': APIKEY, }, data: { dashboard: dataJSON, overwrite: true } }, function(error, result) { if (!error) { console.error("result post dataJSON at the 2nd time --------------------OK------------") } else { console.log("error post dataJSON at the 2nd time --------------------KO------------") } }); }

但是制作这样的东西非常难看我认为这是一种在出现错误时回忆代码部分的方法吗?

In my code I make some API calls and sometimes 1 of them return an error but if I re-do it just after it works (I don't really know what's wrong maybe the JSON that I post is not finish or I don't know...)

So to make that I have coded:

HTTP.call("POST", "http://localhost:3000/api/dashboards/db", { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': APIKEY, }, data: { dashboard: dataJSON, overwrite: true } }, function(error, result) { if (!error) { console.error("result post dataJSON --------------------OK------------") } else { console.log("error post dataJSON --------------------KO------------") HTTP.call("POST", "http://localhost:3000/api/dashboards/db", { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': APIKEY, }, data: { dashboard: dataJSON, overwrite: true } }, function(error, result) { if (!error) { console.error("result post dataJSON at the 2nd time --------------------OK------------") } else { console.log("error post dataJSON at the 2nd time --------------------KO------------") } }); }

But it's very ugly to make something like this I think so is it a way to recall the code's part when there is an error ?

最满意答案

好吧,我的第一个建议就是要了解为什么第一次呼叫失败,看看你是否可以解决它。

除此之外,它取决于您计划使用数据等,但是,例如,您可以在函数中隔离HTTP调用,并使用相同的参数调用该函数。

function callAPIwithData(myData) { HTTP.call("POST", "http://localhost:3000/api/dashboards/db", { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': APIKEY, }, data: myData }, function(error, result) { if (!error) { console.error("OK: result post dataJSON") } else { console.log("KO: error post dataJSON") console.log("retrying ...") callAPIWithData(myData); } }); }

当然,这将需要一组检查和平衡,所以它不会卡在重新尝试无限的casa有其他东西被打破,但你明白了。

Well, my first suggestion is to understand why the call fails the first time around and see if you can fix it.

Excluding that, it depends a bit on you plan to use the data and such, but, for example, you could isolate the HTTP call in a function, and recall the function with the same arguments.

function callAPIwithData(myData) { HTTP.call("POST", "http://localhost:3000/api/dashboards/db", { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': APIKEY, }, data: myData }, function(error, result) { if (!error) { console.error("OK: result post dataJSON") } else { console.log("KO: error post dataJSON") console.log("retrying ...") callAPIWithData(myData); } }); }

of course this will need a set of checks and balances so it won't get stuck re-trying to infinity in casa there's something else broken, but you get the idea.

更多推荐

本文发布于:2023-04-28 09:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330947.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   出现错误   情况下   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!