2016-01-15 49 views
0

安全我有我的app.js一個方法,可處理一個GET請求,使MySQL查詢。當一切順利時,這是一個快樂的結局,但如果出現任何問題,我會得到類似的信息。處理在節點MySQL錯誤

/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/mysql/lib/protocol/Parser.js:78 
    throw err; 
    ^

Error: ER_BAD_FIELD_ERROR: Unknown column 'undefined' in 'where clause' 
at Query.Sequence._packetToError (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/mysql/lib/protocol/sequences/Sequence.js:48:14) 
at Query.ErrorPacket (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/mysql/lib/protocol/sequences/Query.js:83:18) 
at Protocol._parsePacket (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/mysql/lib/protocol/Protocol.js:280:23) 
at Parser.write (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/mysql/lib/protocol/Parser.js:73:12) 
at Protocol.write (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/mysql/lib/protocol/Protocol.js:39:16) 
at Socket.<anonymous> (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/mysql/lib/Connection.js:96:28) 
at emitOne (events.js:77:13) 
at Socket.emit (events.js:169:7) 
at readableAddChunk (_stream_readable.js:146:16) 
at Socket.Readable.push (_stream_readable.js:110:10) 
-------------------- 
at Protocol._enqueue (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/mysql/lib/protocol/Protocol.js:141:48) 
at Connection.query (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/mysql/lib/Connection.js:201:25) 
at /Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/routes/routes.js:27:22 
at Layer.handle [as handle_request] (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/express/lib/router/layer.js:95:5) 
at next (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/express/lib/router/route.js:131:13) 
at Route.dispatch (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/express/lib/router/route.js:112:3) 
at Layer.handle [as handle_request] (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/express/lib/router/layer.js:95:5) 
at /Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/express/lib/router/index.js:277:22 
at Function.process_params (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/express/lib/router/index.js:330:12) 
at next (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/express/lib/router/index.js:271:10) 

當然我知道爲什麼會出現這個錯誤,但我想要安全地處理它。目前,我的代碼是

app.get('/deleteInventoryItem', function(req, res){ 
console.log('requested for deleting inventory item with id: ' + req.query.item_id); 

if(req.query.item_id){ 
    app.connection.query('UPDATE ITEMS.ITEMS_TABLE SET item_isdeleted=1 WHERE id=' + req.query.item_id, 
    function(err, rows){ 
     if(err){ 
     console.log('error occured in deleting: code - ' + err.code + " ,isFatal - " + err.fatal); 
     next(err); 
     } 
     res.send({"status": "success", 
     "message": "Item deleted with id - " + req.query.item_id }); 
    }); 
} else{ 
    res.send({"status": "error", "message": "missing a parameter"}); 
} 
}); 

這裏是我寫的下一個

app.use(function(err, req, res, next) { 
res.status(500).send({ error: 'Something failed!' }); 
}); 

的代碼,我怎麼能處理錯誤的部分,這樣我的node.js服務器犯規崩潰? 如何發送響應貼切而不是發送整個錯誤堆棧跟蹤

+0

我建議使用參數化查詢和 – nada

+0

是否使用Express.js爲您的服務器驗證模塊? – xaviert

+0

我會建議不要拋出錯誤。這是從[文檔的NodeJS(https://nodejs.org/api/errors.html)「任何使用JavaScript投機制將提高必須使用try/catch語句或Node.js的過程中會處理的異常立即退出。「 –

回答

2

基於我你使用Express.js注意到堆棧跟蹤的。這給你一些選項來處理內部錯誤。

首先我們建議定義一些備用error handler middleware這樣的:

app.use(function(error, request, response, next) { 
    console.log("Error handler: ", error); 

    // Send an error message to the user. 
    response.status(500).json({error:error.message}); 

    // Optionally log the request options so you can analyze it later. 
}); 

每當你遇到一個錯誤處理您的請求,您可以通過使用沿着你的中間件通過了next參數轉發此錯誤,例如:

app.get("/throw", function(request, response, next) { 
    var error = new Error("Express.js will delegate this error to the error handler."); 
    next(error); 
}); 
+0

我遵循了相同的規則,但現在發回客戶端的回覆很奇怪。我得到 「錯誤:ER_BAD_FIELD_ERROR:未知列'民主基金'在' where子句'
    <<太長,包含堆棧跟蹤>>」 我已經使用res.status(500); res.render('error',{error:err}); – Mani

+0

發回給客戶的是你決定的。如果這是一個公共API,您將永遠不會想要發回原始錯誤,而是發送更一般的迴應。錯誤處理程序將所有的邏輯放在一箇中心位置。例如,在我們的API中,我們將錯誤與請求數據,會話信息等以及唯一ID一起記錄到磁盤。用戶只能看到帶有錯誤ID的「內部錯誤」消息。實際的堆棧跟蹤從不發送給客戶端。 – xaviert

+0

是的。我認爲這會發生,因爲我發送一個自定義的JSON字符串。但是整個堆棧跟蹤被髮送。你知道爲什麼嗎? – Mani

1

在平原的NodeJS,而不是throw荷蘭國際集團的錯誤,你可以這樣做:

app.connection.query('UPDATE ITEMS.ITEMS_TABLE SET item_isdeleted=1 WHERE id=' + req.query.item_id, 
    function(err, rows){ 
    if(err){ 
     console.log('error occured in deleting: code - ' + err.code + " ,isFatal - " + err.fatal); 

     // Send error message with status as "failure" or smth explanatory 
     res.send({"status": "failure", "message": 'error occured in deleting: code - ' + err.code + " ,isFatal - " + err.fatal }); 
    } else { 
     res.send({"status": "success", "message": "Item deleted with id - " + req.query.item_id }); 
    } 
}); 
+0

如果我發送響應,它將再次崩潰,說不能設置響應頭。 – Mani