Fixed the incorrect way used to return error resonses in NBI.

This commit is contained in:
Zaid Abdulla committed 2015-07-01 05:11:30 +03:00
1 parent b91bcb7442
commit a02e3ecf17
3 files changed
+19 -15

No files matched your search

+1 -1
View File
@@ -134,7 +134,7 @@ watchTask = (taskId, timeout, callback) ->
if task if task
timeout -= 500 timeout -= 500
if timeout < 0 if timeout < 0
callback('timeout') callback(new Error('Timeout'))
else else
watchTask(taskId, timeout, callback) watchTask(taskId, timeout, callback)
else else
+17 -13
View File
@@ -61,6 +61,10 @@ DEVICE_PRESET_REGEX = /^\/devices\/([a-zA-Z0-9\-\_\%]+)\/preset\/?$/
DELETE_DEVICE_REGEX = /^\/devices\/([a-zA-Z0-9\-\_\%]+)\/?$/ DELETE_DEVICE_REGEX = /^\/devices\/([a-zA-Z0-9\-\_\%]+)\/?$/
errorToString = (err) ->
"#{err.name}: #{err.message}"
listener = (request, response) -> listener = (request, response) ->
chunks = [] chunks = []
bytes = 0 bytes = 0
@@ -94,7 +98,7 @@ listener = (request, response) ->
) )
if err if err
response.writeHead(500) response.writeHead(500)
response.end(err) response.end(errorToString(err))
return return
response.writeHead(200) response.writeHead(200)
response.end() response.end()
@@ -106,7 +110,7 @@ listener = (request, response) ->
) )
if err if err
response.writeHead(500) response.writeHead(500)
response.end(err) response.end(errorToString(err))
return return
response.writeHead(200) response.writeHead(200)
response.end() response.end()
@@ -126,7 +130,7 @@ listener = (request, response) ->
) )
if err if err
response.writeHead(500) response.writeHead(500)
response.end(err) response.end(errorToString(err))
return return
response.writeHead(200) response.writeHead(200)
response.end() response.end()
@@ -138,7 +142,7 @@ listener = (request, response) ->
) )
if err if err
response.writeHead(500) response.writeHead(500)
response.end(err) response.end(errorToString(err))
return return
response.writeHead(200) response.writeHead(200)
response.end() response.end()
@@ -157,7 +161,7 @@ listener = (request, response) ->
) )
if err if err
response.writeHead(500) response.writeHead(500)
response.end(err) response.end(errorToString(err))
return return
response.writeHead(200) response.writeHead(200)
response.end() response.end()
@@ -169,7 +173,7 @@ listener = (request, response) ->
) )
if err if err
response.writeHead(500) response.writeHead(500)
response.end(err) response.end(errorToString(err))
return return
response.writeHead(200) response.writeHead(200)
response.end() response.end()
@@ -190,7 +194,7 @@ listener = (request, response) ->
) )
if err if err
response.writeHead(500) response.writeHead(500)
response.end(err) response.end(errorToString(err))
return return
if urlParts.query.connection_request? if urlParts.query.connection_request?
@@ -218,7 +222,7 @@ listener = (request, response) ->
apiFunctions.connectionRequest(deviceId, (err) -> apiFunctions.connectionRequest(deviceId, (err) ->
if err if err
response.writeHead 504 response.writeHead 504
response.end() response.end(errorToString(err))
return return
response.writeHead 200 response.writeHead 200
response.end() response.end()
@@ -250,7 +254,7 @@ listener = (request, response) ->
db.tasksCollection.remove({'_id' : taskId}, (err, removedCount) -> db.tasksCollection.remove({'_id' : taskId}, (err, removedCount) ->
if err if err
response.writeHead(500) response.writeHead(500)
response.end(err) response.end(errorToString(err))
return return
response.writeHead(200) response.writeHead(200)
response.end() response.end()
@@ -303,9 +307,9 @@ listener = (request, response) ->
else if PING_REGEX.test(urlParts.pathname) else if PING_REGEX.test(urlParts.pathname)
host = querystring.unescape(PING_REGEX.exec(urlParts.pathname)[1]) host = querystring.unescape(PING_REGEX.exec(urlParts.pathname)[1])
require('child_process').exec("ping -w 1 -i 0.2 -c 3 #{host}", (err, stdout, stderr) -> require('child_process').exec("ping -w 1 -i 0.2 -c 3 #{host}", (err, stdout, stderr) ->
if err? if err
response.writeHead(404, {'Cache-Control' : 'no-cache'}) response.writeHead(404, {'Cache-Control' : 'no-cache'})
response.end() response.end(errorToString(err))
return return
response.writeHead(200, {'Content-Type' : 'text/plain', 'Cache-Control' : 'no-cache'}) response.writeHead(200, {'Content-Type' : 'text/plain', 'Cache-Control' : 'no-cache'})
response.end(stdout) response.end(stdout)
@@ -320,7 +324,7 @@ listener = (request, response) ->
apiFunctions.deleteDevice(deviceId, (err) -> apiFunctions.deleteDevice(deviceId, (err) ->
if err if err
response.writeHead(500) response.writeHead(500)
response.end(err.message) response.end(errorToString(err))
return return
response.writeHead(200) response.writeHead(200)
response.end() response.end()
@@ -343,7 +347,7 @@ listener = (request, response) ->
q = JSON.parse(urlParts.query.query) q = JSON.parse(urlParts.query.query)
catch err catch err
response.writeHead(400) response.writeHead(400)
response.end(err.toString()) response.end(errorToString(err))
return return
else else
q = {} q = {}
+1 -1
View File
@@ -51,7 +51,7 @@ listener = (httpRequest, httpResponse) ->
) )
httpResponse.writeHead(500, {'Connection' : 'close'}) httpResponse.writeHead(500, {'Connection' : 'close'})
httpResponse.end() httpResponse.end("#{err.name}: #{err.message}")
catch err2 catch err2
util.error("#{new Date().toISOString()} - #{err2.stack}\n") util.error("#{new Date().toISOString()} - #{err2.stack}\n")
) )