@@ -11,7 +11,34 @@ var Bubble = mongoose.model('Bubble', {
1111 timestamp : String
1212} ) ;
1313
14+ var resources = {
15+ noConnection : 'MongoDB connection is inactive.' ,
16+ dbError : 'Could not get information from DB.'
17+ } ;
18+
19+ var config = {
20+ statusCodes : {
21+ OK : 200 ,
22+ notFound : 404 ,
23+ error : 500
24+ }
25+ } ;
26+
27+ var connectionErrorHandler = function ( res ) {
28+ res . status ( config . statusCodes . notFound ) . send ( resources . noConnection ) ;
29+ } ;
30+
31+ var DBErrorHandler = function ( res , err ) {
32+ res . status ( config . statusCodes . error ) . send ( resources . dbError ) ;
33+ console . log ( 'Bubble: ' , resources . dbError , err ) ;
34+ } ;
35+
1436var getBubbles = function ( req , res ) {
37+ if ( mongoose . connection . readyState === 0 ) {
38+ connectionErrorHandler ( res ) ;
39+ return ;
40+ }
41+
1542 var specURI = req . query . pathToDataFile ;
1643
1744 var opts = { } ;
@@ -23,13 +50,21 @@ var getBubbles = function(req, res){
2350 }
2451
2552 Bubble . find ( opts , function ( err , data ) {
26- if ( ! err ) {
27- res . jsonp ( data ) ;
53+ if ( err ) {
54+ DBErrorHandler ( res , err ) ;
55+ return ;
2856 }
57+
58+ res . jsonp ( data ) ;
2959 } )
3060} ;
3161
3262var setBubble = function ( req , res ) {
63+ if ( mongoose . connection . readyState === 0 ) {
64+ connectionErrorHandler ( res ) ;
65+ return ;
66+ }
67+
3368 var bubble = new Bubble ( {
3469 specURI : req . query . specURI ,
3570 section : req . query . section ,
@@ -42,40 +77,64 @@ var setBubble = function(req, res){
4277 ) ;
4378
4479 bubble . save ( function ( err , data ) {
45- if ( ! err ) {
46- console . log ( arguments ) ;
47- res . jsonp ( data ) ;
80+ if ( err ) {
81+ DBErrorHandler ( res , err ) ;
82+ return ;
4883 }
84+
85+ res . jsonp ( data ) ;
4986 } ) ;
5087} ;
5188
5289var removeBubble = function ( req , res ) {
90+ if ( mongoose . connection . readyState === 0 ) {
91+ connectionErrorHandler ( res ) ;
92+ return ;
93+ }
94+
5395 var id = req . query . id ;
5496
5597 Bubble . remove ( { _id : id } , function ( err , data ) {
56- if ( ! err ) {
57- res . jsonp ( data ) ;
98+ if ( err ) {
99+ DBErrorHandler ( res , err ) ;
100+ return ;
58101 }
102+
103+ res . jsonp ( data ) ;
59104 } ) ;
60105} ;
61106
62107var countBubbles = function ( req , res ) {
108+ if ( mongoose . connection . readyState === 0 ) {
109+ connectionErrorHandler ( res ) ;
110+ return ;
111+ }
112+
63113 var specURI = req . query . specURI ;
64114
65115 Bubble . count ( { specURI : specURI } , function ( err , data ) {
66- if ( ! err ) {
67- res . jsonp ( data ) ;
68- } else {
69- res . send ( err ) ;
116+ if ( err ) {
117+ DBErrorHandler ( res , err ) ;
118+ return ;
70119 }
120+
121+ res . jsonp ( data ) ;
71122 } ) ;
72123} ;
73124
74125var removeAllBubbles = function ( req , res ) {
126+ if ( mongoose . connection . readyState === 0 ) {
127+ connectionErrorHandler ( res ) ;
128+ return ;
129+ }
130+
75131 Bubble . remove ( function ( err , data ) {
76- if ( ! err ) {
77- res . send ( 'removed all' ) ;
132+ if ( err ) {
133+ DBErrorHandler ( res , err ) ;
134+ return ;
78135 }
136+
137+ res . send ( 'removed all' ) ;
79138 } ) ;
80139} ;
81140
0 commit comments