Permalink
Browse files

Error handling done.

  • Loading branch information...
1 parent a0fd71f commit 178d3e755871689ca3012b4aa9bbfedf6046155c xFontroSF committed Sep 27, 2012
Showing with 35 additions and 32 deletions.
  1. +2 −0 index.js
  2. +8 −1 operacionsAritmetiques.js
  3. +4 −12 router.js
  4. +21 −19 server.js
View
@@ -1,3 +1,5 @@
+require('nodetime').profile({debug: true});
+
var server = require("./server"),
router = require("./router"),
operacionsAritmetiques = require("./operacionsAritmetiques"),
@@ -20,7 +20,14 @@ var dividir = function (a, b, res){
var checkNumbersAndRespond = function (result, res){
if(isNaN(result)){
- res(new Error("Not a number!"));
+ var error = {
+ errorCode: 400,
+ errorContent: {
+ 'Content-Type' : 'text/html'
+ },
+ errorDescription: '400 Bad Request'
+ };
+ res(error);
} else{
res(null, result);
}
View
@@ -1,22 +1,15 @@
-function route(handle, operation, a, b, resultat, err) {
+function route(handle, operation, a, b, resultat) {
if (typeof handle[operation] === 'function') {
handle[operation](
a,
b,
function(error, res){
if(error){
- var error = {
- errorCode: 400,
- errorContent: {
- 'Content-Type' : 'text/html'
- },
- errorDescription: '400 Bad Request'
- };
- err(error);
+ resultat(error);
} else{
//console.log("The result is: "+res);
- resultat({"resultat" : res});
+ resultat(null ,{"resultat" : res});
}
}
);
@@ -28,8 +21,7 @@ function route(handle, operation, a, b, resultat, err) {
},
errorDescription: '404 Not foud'
};
- err(error);
-
+ resultat(error);
}
}
View
@@ -29,15 +29,16 @@ function start(route, handle) {
pathname,
op1,
op2,
- function(resultat){
- response.writeHead(200, {});
- response.write(JSON.stringify(resultat));
- response.end();
- },
- function(err){
- response.writeHead(err.errorCode, err.errorContent);
- response.write(JSON.stringify(err.errorDescription));
- response.end();
+ function(error, resultat){
+ if(error){
+ response.writeHead(error.errorCode, error.errorContent);
+ response.write(JSON.stringify(error.errorDescription));
+ response.end();
+ }else {
+ response.writeHead(200, {});
+ response.write(JSON.stringify(resultat));
+ response.end();
+ }
});
});
@@ -53,22 +54,23 @@ function start(route, handle) {
pathname,
op1,
op2,
- function(resultat){
- response.writeHead(200, {});
- response.write(JSON.stringify(resultat));
- response.end();
- },
- function(err){
- response.writeHead(err.errorCode, err.errorContent);
- response.write(JSON.stringify(err.errorDescription));
- response.end();
+ function(error, resultat){
+ if(error){
+ response.writeHead(error.errorCode, error.errorContent);
+ response.write(JSON.stringify(error.errorDescription));
+ response.end();
+ }else {
+ response.writeHead(200, {});
+ response.write(JSON.stringify(resultat));
+ response.end();
+ }
});
}
}
http.createServer(onRequest).listen(port,ipaddr);
- console.log("Server has started at: "+ipaddr+":"+port);
+ //console.log("Server has started at: "+ipaddr+":"+port);
}

0 comments on commit 178d3e7

Please sign in to comment.