node.js api接口

by Roger Jin

罗杰·金(Roger Jin)

如何在Node.js API客户端中正常处理故障 (How to gracefully handle failures in a Node.js API client)

There are two facts of life: you breathe air, and errors will occur in your programs.

生活有两个事实:呼吸空气,程序中会发生错误。

We’ve all experienced trouble connecting to Wi-Fi, or had a phone call drop on us abruptly. Intermittent failures across internet works are statistically unusual on the whole, but still bound to occur. This means that for programmers, anything that waits for a response over a network is prone to error.

我们所有人在连接Wi-Fi时都遇到了麻烦,或者突然打了个电话。 从统计上看,跨互联网工作的间歇性故障在总体上是不寻常的,但仍会发生。 这意味着对于程序员来说,任何等待通过网络响应的内容都容易出错。

I’m an architect for a “content management system as a service” called ButterCMS, so I’ll explore reliability using this tool. It consists of:

我是一个名为ButterCMS的“内容管理系统即服务”的架构师 ,因此我将使用此工具探索可靠性。 它包括:

  • a hosted dashboard for content editors内容编辑器的托管仪表板
  • a JSON API for retrieving this content用于检索此内容的JSON API
  • SDK’s for integrating ButterCMS into native code.用于将ButterCMS集成到本机代码中的SDK。

The database, logic, and administrative dashboard is a service through a web API. The question is “What can you do with the inevitable errors in your Node.js client?” Errors over a client API are bound to happen–it is what you do about it that matters most.

数据库,逻辑和管理仪表板是通过Web API提供的服务。 问题是“您如何处理Node.js客户端中不可避免的错误?” 客户端API上的错误注定会发生-最重要的是您所要做的。

In this article I’ll walk through how I added better handling of failures to the ButterCMS JavaScript API client. By the end of this article, you will hopefully have a better understanding of how to deal with failure in your own API clients.

在本文中,我将逐步介绍如何为ButterCMS JavaScript API客户端添加更好的故障处理方法。 到本文结尾,希望您对如何处理自己的API客户端中的故障有更好的了解。

基本异常处理 (Basic Exception Handling)

To begin, let’s look at an example API request for retrieving a blog post from the Butter API:

首先,让我们看一个示例API请求,该请求从Butter API中检索博客文章:

butter.post.retrieve('example-post')  .then(function onSuccess(resp) {    console.log(resp.data);  });

This will work except it leaves you blind to any exceptions the client can throw at you. Note the client API uses promises to get blog data. Keep this in mind as JavaScript takes on a new dimension through promises.

这将起作用,除了它使您对客户端可能向您抛出的任何异常视而不见。 注意,客户端API使用promise获取博客数据。 请记住这一点,因为JavaScript通过Promise开辟了一个新的维度。

To handle exceptions using a promise, slap a catch() at the end.

要使用Promise处理异常,请在最后拍一拍catch()

For example:

例如:

butter.post.retrieve('example-post')  .catch(function onError(error) {    console.log(error);  });

Done! A JavaScript promise handles all errors for you and executes the onError() callback. The error object contains very useful information about what went wrong.

做完了! JavaScript承诺会为您处理所有错误,并执行onError()回调。 error对象包含有关发生问题的非常有用的信息。

If you look under the hood of the ButterCMS client API you’ll see it uses axios. Axios is a promise based HTTP client that works in the browser and Node.js.

如果您查看ButterCMS客户端API的内幕,您会发现它使用axios 。 Axios是基于诺言的HTTP客户端,可在浏览器和Node.js中使用。

Examining the Axios error object you get back through a promise reveals the following error object:

检查通过诺言获得的Axios错误对象将显示以下错误对象:

{data:Object, status:401, statusText:'Unauthorized', headers:Object, config:Object}

The HTTP status code tells me what the error was.

HTTP状态代码告诉我错误是什么。

更好的异常处理 (Better Exception Handling)

The type of errors you get will depend on the API behavior. For example, the ButterCMS API docoumentation lists all possible responses. You can get a 400, 401, or a 404 depending on the request.

您得到的错误类型将取决于API行为。 例如,ButterCMS API文档列出了所有可能的响应 。 您可以根据要求获得400、401或404。

One way to deal with these exceptions is to handle each status in a different way. For example, you could handle errors:

处理这些异常的一种方法是以不同的方式处理每个状态。 例如,您可以处理错误:

butter.post.retrieve('example-post')  .catch(function onError(error) {    if (error.status === 400) {      console.log('Bad request, often due to missing a required parameter.');    } else if (error.status === 401) {      console.log('No valid API key provided.');    } else if (error.status === 404) {      console.log('The requested resource doesn\'t exist.');    }  });

By using the HTTP status as the source of truth, you can interpret the reason for the error however you want.

通过使用HTTP状态作为事实的来源,您可以根据需要解释错误的原因。

Other companies, like the Stripe API client, solve the problem with an error type on the response. The error typestatus code tells you what type of error is coming back in the response.

其他公司,例如Stripe API客户端 ,通过响应中的错误类型解决了该问题。 错误typestatus代码告诉您响应中又返回哪种类型的错误。

With all this, one final question remains. “What happens when the network request times out?”

有了这些,最后一个问题仍然存在。 “当网络请求超时时会发生什么?”

For a client API, any request over a network is very risky. Network connectivity can be a luxury one can’t afford at times.

对于客户端API,通过网络进行的任何请求都是非常危险的。 网络连接有时是一种无法承受的奢侈品。

Let’s examine what error exception you get when it times out. The ButterCMS client API has a default value of 3000 ms or 3 seconds.

让我们检查一下超时时得到的错误异常。 ButterCMS客户端API的默认值为3000毫秒或3秒。

Take a look at this error object when it times out from the exception handler:

看一下此错误对象在异常处理程序中超时的情况:

{code:'ECONNABORTED', message:String, stack:String, timeout:3000}

Like any good error object, it has plenty of good details about the exception. Note this error object is different from the one we saw earlier. One distinct difference is the timeout property. This can be useful in dealing with this kind of exception in a unique way.

像其他任何好的错误对象一样,它具有大量有关异常的详细信息。 请注意,此错误对象与我们之前看到的对象不同。 区别在于timeout属性。 这对于以独特的方式处理此类异常很有用。

The question is, “Is there a graceful way to handle these kinds of exceptions?”

问题是:“是否有一种优雅的方式来处理这类异常?”

处理网络错误 (Handling Network Errors)

One idea is to auto-retry the request after it fails. Anything that waits for a network response can fail. The failure occurs because of circumstances outside your direct control. As developers, it is nice to be in control but life comes with many exceptions.

一种想法是在请求失败后自动重试。 任何等待网络响应的操作都可能失败。 发生故障是由于您无法直接控制的情况。 作为开发人员,受到控制很高兴,但生活中有许多例外。

Polly-js can attempt to retry the action once it detects an error. The polly-js library can handle exceptions through a JavaScript promise. This promise catches the exception in case all the retries fail and executes the catch(). But, we decided not to use polly-js because it is an extra dependency that adds bloat to the client API.

一旦检测到错误, Polly-js可以尝试重试该动作。 polly-js库可以通过JavaScript承诺处理异常。 如果所有重试均失败,则该promise捕获异常,并执行catch() 。 但是,我们决定不使用polly-js,因为它是一个额外的依赖项,给客户端API增加了膨胀。

One design principle at play here is: “A little copy-paste is better than an extra dependency.” The bulk of the retry logic is minimal and has exactly what we need to solve the problem.

在这里发挥作用的一个设计原则是:“复制粘贴胜于附加依赖。” 重试逻辑的大部分是最小的,并且正是我们解决问题所需要的。

The crux of auto retries returns a JavaScript promise:

自动重试的关键在于返回JavaScript承诺:

function executeForPromiseWithDelay(config, cb) {  return new Promise(function (resolve, reject) {    function execute() {      var original = cb();
original.then(function (e) {        resolve(e);      }, function (e) {        var delay = config.delays.shift();
if (delay && config.handleFn(e)) {          setTimeout(execute, delay);        } else {          reject(e);        }      });    }
execute();  });}

The promise has the resolve and reject callbacks encapsulated for automatic retries. The config.handleFn() callback figures out what condition will cause it to retry. The config.delays.shift() will remove the first item from the list and delay the next attempt.

Promise封装了用于自动重试的resolvereject回调。 config.handleFn()回调指出什么条件将导致其重试。 config.delays.shift()将从列表中删除第一项,并延迟下一次尝试。

The good news is it can meet a specific condition before there are any retries. The library has a handle() function to set the callback that evaluates the condition. You tell it how many retries, give the condition, and final exception handling.

好消息是,在重试之前它可以满足特定条件。 该库具有一个handle()函数来设置评估条件的回调。 您可以告诉它重试多少次,给出条件以及最终的异常处理。

The ButterCMS client API has retry capabilities out of the box. To enable auto retries you need this:

ButterCMS客户端API具有开箱即用的retry功能。 要启用自动重试,您需要这样做:

butter.post.retrieve('example-post')  .handle(function onError(error) {    // Only retry on time out    return error.timeout;  })  .executeWithAutoRetry(3)  .then(function onSuccess(resp) {    console.log(resp.data);  })  .catch(function onTimeoutError(error) {    if (error.timeout) {      console.log('The network request has timed out.');    }  });

The executeWithAutoRetry() staggers subsequent requests and retries if there is a failure. For example, the first attempt will fail then wait 100ms before the second attempt. The second attempt, if it fails, will wait 200ms before the third. The third attempt will wait 400ms before the fourth and final attempt.

executeWithAutoRetry()交错后续请求,如果失败,则重试。 例如,第一次尝试将失败,然后在第二次尝试之前等待100ms。 如果第二次尝试失败,它将在第三次尝试之前等待200毫秒。 第三次尝试将在第四次也是最后一次尝试之前等待400毫秒。

With the ButterCMS API client, you now have a nice way of handling promise based exceptions. All you need to do is configure it to your liking.

使用ButterCMS API客户端,您现在有了处理基于承诺的异常的好方法。 您需要做的就是根据自己的喜好对其进行配置。

结论 (Conclusion)

When it comes to errors, you can either bury your head in the sand or handle the unexpected with grace and elegance. Any client API that waits for a response through a connection is prone to exceptions. The choice is yours on what to do when erratic behavior happens.

当涉及到错误时,您既可以将头埋在沙子里,也可以用优雅和优雅来应对意外情况。 任何等待通过连接响应的客户端API都容易出现异常。 选择由您决定何时出现不稳定行为。

Consider an exception as unpredictable behavior. Except, because it is unpredictable does not mean you can’t prepare in advance. When handling exceptions, focus on anticipating what went wrong, not application logic.

将异常视为不可预测的行为。 除外,因为它不可预测并不意味着您无法提前准备。 处理异常时,应着重于预测出了什么问题,而不是应用程序逻辑。

Network connectivity is one of the worst culprits of failures. Be sure to prepare in advance, to give requests a second change in case of a failed connection.

网络连接是故障的最严重原因之一。 如果连接失败,请确保事先做好准备,以便再次更改请求。

This article was originally published on our blog.

本文最初发布在我们的博客上

For more content like this, follow ButterCMS on Twitter.

有关更多内容,请在Twitter上关注ButterCMS。

翻译自: https://www.freecodecamp.org/news/how-to-gracefully-handle-failures-in-a-node-js-api-client-605673cb72ab/

node.js api接口

node.js api接口_如何在Node.js API客户端中正常处理故障相关推荐

  1. react js 添加样式_如何在React JS Application中添加图像?

    react js 添加样式 Hello! In this article, we will learn how to add images in React JS? I remember when I ...

  2. 个人项目api接口_5个免费有趣的API,可用于学习个人项目等

    个人项目api接口 Public APIs are awesome! 公共API很棒! There are over 50 pieces covering APIs on just the Towar ...

  3. 1688店铺所有商品API接口(整店商品查询API接口)

    可以通过1688店铺所有商品API接口采集店铺所有商品详情页各项数据,包含商品标题,SKU信息.价格.优惠价,收藏数.销量.SKU图.标题.详情页图片等店铺内页面上有的数据均可以拿到,大家都知道,16 ...

  4. 拼多多商品详情页 API接口、拼多多商品SKU数据接口 API接口、拼多多关键词搜索接口 API接口 API接口、拼多多关键词采集 API接口、拼多多采集接口 API接口、拼多多详情 API接口

    拼多多API接口,提供商品解析,商品采集等. 拼多多采购项目.店群项目等都可以对接 下方点击注册就可以对接了 点击注册 拼多多商品详情页 API接口.拼多多商品SKU数据接口 API接口.拼多多关键词 ...

  5. 淘宝商品评价api接口,淘宝评论视频API接口,淘宝评论API接口(app、h5端)

    淘宝商品评价api接口,淘宝评论视频API接口,淘宝评论API接口(app.h5端)可通过商品id,获取商品评价信息.评价内容.买家秀图片.评论浏览量.评价视频.评价追评等页面上展示的数据均可以拿到, ...

  6. 获取淘宝购买到的商品订单物流API,买家订单物流API接口,淘宝买家订单API接口

    一.获取淘宝购买到的商品订单物流API,买家订单物流API接口,淘宝买家订单API接口代码对接如下: 1.公共参数: 名称 类型 必须 描述 key String 是 调用key(必须以GET方式拼接 ...

  7. 微信API接口、微信二次开发API调用

    微信API接口.微信二次开发API调用 微信协议接口调用-加微信好友及通过好友请求 加微信好友 /**       * 微信自动添加好友      * @author wechatno:tangjin ...

  8. 拼多多商品详情页API接口、拼多多商品销量API接口、拼多多商品列表API接口、拼多多APP详情API接口、拼多多详情API接口

    通过商品ID提取商品详情页各项数据,包含商品标题,skuid.价格.优惠价,收藏数.拼单人数.月销售量.主图.标题.详情页图片等页面上有的数据均可以拿到,大家都知道,拼多多的反爬虫机制十分严,而很多时 ...

  9. 淘宝商品评价api接口(item_review-获得淘宝商品评论API接口),天猫商品评论API接口

    淘宝商品评价api接口(item_review-获得淘宝商品评论API接口),天猫商品评论API接口可通过商品id,获取商品评价信息.评价内容.买家秀图片.评论浏览量.评价视频.评价追评等页面上的数据 ...

最新文章

  1. 本周推荐10款免费的网站模板设计
  2. 【wpf WebBrowser 清空网站的CookieSession 清空用户登录状态】
  3. 函数返回对象写法的失误
  4. 确认!别再相信Python了! 程序员:就你敢说...
  5. oracle apex 发布应用,基于Oracle APEX的快速Web应用开发
  6. [导入]DotText源码阅读(2)-工程、数据库表结构
  7. c语言 malloc前的括号,20. 有效的括号(LeetCode)——C语言及JS实现
  8. lol2月26日更新后一直提示服务器维护,《lol》2月26日更新了什么 2月26日更新内容一览...
  9. python 小程序搜索排名-用python2.7.9 写个小程序搜索某个目录下行有某关键字
  10. 2015年度总结-面对真实的自己
  11. linux ubuntu apache php 网站 'page not found'
  12. 探索在Android中使用Emoji Font的方法
  13. 山东省第八届acm大赛 G题 (SDUT 3899)
  14. Ubuntu18.04 下载与安装
  15. 农户在集市上卖西瓜,他总共有1020个西瓜,第一天卖掉一半多两个,第二天卖掉剩下的一半多两个, 问照此规律实下去,该农户几天能将所有的西瓜卖完。C语言
  16. 使用python爬虫获取大乐透和双色球历史开奖数据储存到excel表中
  17. win10安装序列号
  18. (转)做好产品需求文档的10步
  19. java.util.concurrent.TimeoutException: Waited 3000 milliseconds (plus 2 milliseconds, 351400 nanosec
  20. 第十五周项目二----用哈希法组织关键字之链地址法

热门文章

  1. python-面向对向-实例方法的继承
  2. MySQL查询冗余索引和未使用过的索引
  3. MySQL ALTER TABLE报错Duplicate entry 'xxx' for key 'PRIMARY'解决方法
  4. 悉尼一船只引擎爆炸男子被迫跳水:严重烧伤被送医
  5. linux中MySQL密码的恢复方法
  6. .NET快速信息化系统开发框架 V3.2-amp;gt;WinForm版本新增新的角色授权管理界面效率更高、更规范...
  7. 大势所趋与各具特色 超融合市场玩家大盘点
  8. GSK携手学术机构共建健康大数据系统
  9. JQuery如何与数据库交互
  10. HTML textarea 标签