在coffeescript中处理Http错误 [英] Handling Http erorrs in coffeescript

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

问题描述

p>代码:

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

错误:

  events.js:71 
throw arguments [1]; //未处理的错误事件
^
错误:连接ECONNREFUSED
at errnoException(net.js:770:11)
在Object.afterConnect [as oncomplete] js:761:19)


解决方案

在单独的事件处理程序中主动侦听错误。现在,您正在为响应( res )附加事件处理程序,但它需要附加到请求对象本身。请参见文档

  req = http.gethttp:// 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.logTomcat Disconected

此外, handler: colsone.log


Guys 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 cant find the right solution

Code :

 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"

error :

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)

解决方案

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"

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

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

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