Skip to content
Browse files

Add ClientError support

  • Loading branch information...
1 parent 13f578e commit ddcfb53c8002cdad17b889372b41172fdb0b7a46 @vpetrov committed Feb 3, 2013
Showing with 64 additions and 19 deletions.
  1. +37 −19 index.js
  2. +27 −0 lib/clienterror.js
View
56 index.js
@@ -6,23 +6,34 @@
* @license New BSD License (see LICENSE file for details).
*/
-var express=require('express');
-var DB=require('./db');
-var sconfig=require("./config");
-var log=require('logule').init(module);
-var path=require('path');
-var ejs=require('ejs');
-var ursa=require('ursa');
-var fs=require('fs');
-var util=require('./util');
-
-ejs.open='{{';
-ejs.close='}}';
-
-global.obj=require('./lib/obj');
-global.arrays=require('./lib/arrays');
-global.ROOT=path.dirname(process.mainModule.filename);
-
+var express = require('express'),
+ DB = require('./db'),
+ sconfig = require("./config"),
+ log = require('logule').init(module),
+ path = require('path'),
+ ejs = require('ejs'),
+ ursa = require('ursa'),
+ fs = require('fs'),
+ util = require('./util'),
+ //constants
+ HTTP_SERVER_ERROR = 500;
+
+ejs.open = '{{';
+ejs.close = '}}';
+
+/* Globals */
+global.obj = require('./lib/obj');
+global.arrays = require('./lib/arrays');
+global.ClientError = require('./lib/clienterror');
+global.ROOT = path.dirname(process.mainModule.filename);
+
+/**
+ * Mount a new Survana module
+ * @param app
+ * @param name
+ * @param mconf
+ * @return {Object}
+ */
function addModule(app,name,mconf)
{
var mname=sconfig.module_prefix+'-'+name;
@@ -52,6 +63,13 @@ function addModule(app,name,mconf)
return module;
}
+/**
+ * Overrides default module config with the global config
+ * TODO: Consider using obj.merge() or obj.override()
+ * @param source
+ * @param config
+ * @return {Object}
+ */
function mergeConfig(source,config)
{
//iterate over each key in config
@@ -136,13 +154,13 @@ function globalErrorHandler(err,req,res,next)
var app=req.app;
var log=app.log; //use app-specific logger
- log.error(err.message,err.stack);
+ log.error(err.message, err.stack);
if (req.header('Content-Type') === 'application/json') {
res.send({
success:0,
message:err.message
- },500);
+ },err.code || HTTP_SERVER_ERROR);
} else {
res.render('error',{
req:req,
View
27 lib/clienterror.js
@@ -0,0 +1,27 @@
+/** lib/clienterror.js
+ *
+ * @author Victor Petrov <victor.petrov@gmail.com>
+ * @copyright (c) 2012, The Neuroinformatics Research Group at Harvard University.
+ * @copyright (c) 2012, The President and Fellows of Harvard College.
+ * @license New BSD License (see LICENSE file for details).
+ */
+
+var HTTP_BAD_REQUEST = 400,
+ HTTP_UNAUTHORIZED = 401,
+ HTTP_SERVER_ERROR = 500;
+
+function ClientError(message, code) {
+ "use strict";
+
+ this.name = "ClientError";
+ this.message = message || "";
+ this.code = code || HTTP_BAD_REQUEST; //this code will attempt to read
+}
+
+ClientError.prototype = new Error();
+ClientError.prototype.constructor = ClientError;
+ClientError.prototype.HTTP_BAD_REQUEST = HTTP_BAD_REQUEST;
+ClientError.prototype.HTTP_UNAUTHORIZED = HTTP_UNAUTHORIZED;
+ClientError.prototype.HTTP_SERVER_ERROR = HTTP_SERVER_ERROR;
+
+module.exports = ClientError;

0 comments on commit ddcfb53

Please sign in to comment.
Something went wrong with that request. Please try again.