有什么方法可以在代码级别捕获AWS lambda超时错误? [英] Is there any way to catch AWS lambda timed out error in code level?

查看:101
本文介绍了有什么方法可以在代码级别捕获AWS lambda超时错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以在代码级捕获AWS lambda timed out错误,以便我有机会在退出lambda函数之前处理该错误?

Is there any way to catch AWS lambda timed out error in code-level so that I can have a chance to handle for the error before exiting the lambda function?

推荐答案

虽然lambda环境不会触发定时退出"事件,但您可以自己进行一些琐碎的事情.

While the lambda environment doesn't fire a "timing out" event, you can do this yourself fairly trivially.

每种语言都有context对象公开的功能 来获取剩余时间(以毫秒为单位)

Each language has a function exposed by the context object to get the remaining time in milliseconds.

您可以结合使用任何语言的计时器功能来使用它,以确保在超时之前得到通知.

You can use that, in tandem with the timer functionality of whatever language you're using to ensure you get notified before timeout.

例如(节点):

function handler(event, context, callback) {
  const timer = setTimeout(() => {
    console.log("oh no i'm going to timeout in 3 seconds!");
    // &c.
  }, context.getRemainingTimeInMillis() - 3 * 1000);
  // rest of code...
  clearTimeout(timer);
  callback(null, result);
}

这篇关于有什么方法可以在代码级别捕获AWS lambda超时错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆