处理coffeescript中的Http错误 [英] Handling Http errors in coffeescript

查看:71
本文介绍了处理coffeescript中的Http错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理coffeescript中的http请求,但万一服务器停机,应用程序会死于以下错误,而我找不到正确的解决方案。

I am trying to handle a http request in coffeescript, but in case the server is down the app just dies with error below, and I can't find the right solution.

代码:

 http.get "http://localhost:8080/health", (res) ->
        status =  res.statusCode
        value = if status == 200 then 1 else 0
        console.log value
        server.push_metric metricPrefix , value
        res.on 'error', () ->
          colsone.log "Tomcat Disconected"

错误:

events.js:71
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: connect ECONNREFUSED
    at errnoException (net.js:770:11)
    at Object.afterConnect [as oncomplete] (net.js:761:19)


推荐答案

我认为您必须在单独的事件处理程序中积极侦听错误。目前,您正在将事件处理程序附加到响应( res ),但需要将其附加到请求对象本身。参见文档

I think you have to actively listen for the error in a separate event handler. Right now, you're attaching an event handler to the response (res), but it needs to be attached to the request object itself. See the docs.

req = http.get "http://localhost:8080/health", (res) ->
  status = res.statusCode
  value = if status == 200 then 1 else 0
  console.log value
  server.push_metric metricPrefix , value

req.on 'error', ->
  console.log "Tomcat Disconected"

此外,您在当前错误中有错字处理程序: colsone.log

Also, you have a typo in your current error handler: colsone.log

这篇关于处理coffeescript中的Http错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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