From 090295773d12cae88e81cf2e509168ad5e3fda45 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 14 Jul 2012 01:38:33 +0200 Subject: o Improved error handling when postgres fails. --- routes/index.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/routes/index.js b/routes/index.js index 82bce70..30f0f9b 100644 --- a/routes/index.js +++ b/routes/index.js @@ -64,8 +64,24 @@ function after(res, callback) { return function() { var err = arguments[0]; if(err) { - res.writeHead(500, {'Content-Type' : 'text/plain'}); - return res.end('Error! ' + util.inspect(err)) + // Ideally we would be able to differenciate between + // client and server errors, but I'm lazy. It might be + // possible to map some values from + // http://www.postgresql.org/docs/9.0/static/errcodes-appendix.html + // - trygvis + var body = JSON.stringify({ collection: { + error: { + title: 'Unable to insert employee.', + message: err.message, + code: err.code + } + }}); + var headers = { + 'Content-Type' : 'application/vnd.collection+json', + 'Content-Length' : body.length + } + res.writeHead(400, headers); + return res.end(body); } callback.apply(this, _.rest(arguments)); } -- cgit v1.2.3