@@ -89,30 +89,56 @@ def prop_from_arg(self, cl, key, value, itemid=None):
8989
9090 return prop
9191
92- @staticmethod
93- def error_obj (status , msg , source = None ):
94- """Wrap the error data into an object. This function is temporally and
95- will be changed to a decorator later."""
96- result = {
97- 'error' : {
98- 'status' : status ,
99- 'msg' : msg
100- }
101- }
102- if source is not None :
103- result ['error' ]['source' ] = source
104-
105- return result
106-
107- @staticmethod
108- def data_obj (data ):
109- """Wrap the returned data into an object. This function is temporally
110- and will be changed to a decorator later."""
111- result = {
112- 'data' : data
113- }
114- return result
92+ def _data_decorator (func ):
93+ """Wrap the returned data into an object.."""
94+ def format_object (self , * args , ** kwargs ):
95+ try :
96+ code , data = func (self , * args , ** kwargs )
97+ except IndexError , msg :
98+ code = 404
99+ data = msg
100+ except Unauthorised , msg :
101+ code = 403
102+ data = msg
103+ except (hyperdb .DesignatorError , UsageError ), msg :
104+ code = 400
105+ data = msg
106+ except (AttributeError , Reject ), msg :
107+ code = 405
108+ data = msg
109+ except ValueError , msg :
110+ code = 409
111+ data = msg
112+ except NotImplementedError :
113+ code = 402 # nothing to pay, just a mark for debugging purpose
114+ data = 'Method under development'
115+ except :
116+ exc , val , tb = sys .exc_info ()
117+ code = 400
118+ # if self.DEBUG_MODE in roundup_server
119+ # else data = 'An error occurred. Please check...',
120+ data = val
121+
122+ # out to the logfile
123+ print 'EXCEPTION AT' , time .ctime ()
124+ traceback .print_exc ()
125+
126+ self .client .response_code = code
127+ if code >= 400 : # any error require error format
128+ result = {
129+ 'error' : {
130+ 'status' : code ,
131+ 'msg' : data
132+ }
133+ }
134+ else :
135+ result = {
136+ 'data' : data
137+ }
138+ return result
139+ return format_object
115140
141+ @_data_decorator
116142 def get_collection (self , class_name , input ):
117143 """GET resource from class URI.
118144
@@ -146,6 +172,7 @@ def get_collection(self, class_name, input):
146172 self .client .setHeader ("X-Count-Total" , str (len (result )))
147173 return 200 , result
148174
175+ @_data_decorator
149176 def get_element (self , class_name , item_id , input ):
150177 """GET resource from object URI.
151178
@@ -191,6 +218,7 @@ def get_element(self, class_name, item_id, input):
191218
192219 return 200 , result
193220
221+ @_data_decorator
194222 def get_attribute (self , class_name , item_id , attr_name , input ):
195223 """GET resource from attribute URI.
196224
@@ -231,6 +259,7 @@ def get_attribute(self, class_name, item_id, attr_name, input):
231259
232260 return 200 , result
233261
262+ @_data_decorator
234263 def post_collection (self , class_name , input ):
235264 """POST a new object to a class
236265
@@ -290,18 +319,22 @@ def post_collection(self, class_name, input):
290319 }
291320 return 201 , result
292321
322+ @_data_decorator
293323 def post_element (self , class_name , item_id , input ):
294324 """POST to an object of a class is not allowed"""
295325 raise Reject ('POST to an item is not allowed' )
296326
327+ @_data_decorator
297328 def post_attribute (self , class_name , item_id , attr_name , input ):
298329 """POST to an attribute of an object is not allowed"""
299330 raise Reject ('POST to an attribute is not allowed' )
300331
332+ @_data_decorator
301333 def put_collection (self , class_name , input ):
302334 """PUT a class is not allowed"""
303335 raise Reject ('PUT a class is not allowed' )
304336
337+ @_data_decorator
305338 def put_element (self , class_name , item_id , input ):
306339 """PUT a new content to an object
307340
@@ -346,6 +379,7 @@ def put_element(self, class_name, item_id, input):
346379 }
347380 return 200 , result
348381
382+ @_data_decorator
349383 def put_attribute (self , class_name , item_id , attr_name , input ):
350384 """PUT an attribute to an object
351385
@@ -394,6 +428,7 @@ def put_attribute(self, class_name, item_id, attr_name, input):
394428
395429 return 200 , result
396430
431+ @_data_decorator
397432 def delete_collection (self , class_name , input ):
398433 """DELETE all objects in a class
399434
@@ -433,6 +468,7 @@ def delete_collection(self, class_name, input):
433468
434469 return 200 , result
435470
471+ @_data_decorator
436472 def delete_element (self , class_name , item_id , input ):
437473 """DELETE an object in a class
438474
@@ -461,6 +497,7 @@ def delete_element(self, class_name, item_id, input):
461497
462498 return 200 , result
463499
500+ @_data_decorator
464501 def delete_attribute (self , class_name , item_id , attr_name , input ):
465502 """DELETE an attribute in a object by setting it to None or empty
466503
@@ -503,10 +540,12 @@ def delete_attribute(self, class_name, item_id, attr_name, input):
503540
504541 return 200 , result
505542
543+ @_data_decorator
506544 def patch_collection (self , class_name , input ):
507545 """PATCH a class is not allowed"""
508546 raise Reject ('PATCH a class is not allowed' )
509547
548+ @_data_decorator
510549 def patch_element (self , class_name , item_id , input ):
511550 """PATCH an object
512551
@@ -573,6 +612,7 @@ def patch_element(self, class_name, item_id, input):
573612 }
574613 return 200 , result
575614
615+ @_data_decorator
576616 def patch_attribute (self , class_name , item_id , attr_name , input ):
577617 """PATCH an attribute of an object
578618
@@ -645,6 +685,7 @@ def patch_attribute(self, class_name, item_id, attr_name, input):
645685 }
646686 return 200 , result
647687
688+ @_data_decorator
648689 def options_collection (self , class_name , input ):
649690 """OPTION return the HTTP Header for the class uri
650691
@@ -654,6 +695,7 @@ def options_collection(self, class_name, input):
654695 """
655696 return 204 , ""
656697
698+ @_data_decorator
657699 def options_element (self , class_name , item_id , input ):
658700 """OPTION return the HTTP Header for the object uri
659701
@@ -667,6 +709,7 @@ def options_element(self, class_name, item_id, input):
667709 )
668710 return 204 , ""
669711
712+ @_data_decorator
670713 def option_attribute (self , class_name , item_id , attr_name , input ):
671714 """OPTION return the HTTP Header for the attribute uri
672715
@@ -736,53 +779,21 @@ def dispatch(self, method, uri, input):
736779
737780 # Call the appropriate method
738781 output = None
739- try :
740- if resource_uri in self .db .classes :
741- response_code , output = getattr (
742- self , "%s_collection" % method .lower ()
743- )(resource_uri , input )
782+ if resource_uri in self .db .classes :
783+ output = getattr (
784+ self , "%s_collection" % method .lower ()
785+ )(resource_uri , input )
786+ else :
787+ class_name , item_id = hyperdb .splitDesignator (resource_uri )
788+ if len (uri_split ) == 3 :
789+ output = getattr (
790+ self , "%s_attribute" % method .lower ()
791+ )(class_name , item_id , uri_split [2 ], input )
744792 else :
745- class_name , item_id = hyperdb .splitDesignator (resource_uri )
746- if len (uri_split ) == 3 :
747- response_code , output = getattr (
748- self , "%s_attribute" % method .lower ()
749- )(class_name , item_id , uri_split [2 ], input )
750- else :
751- response_code , output = getattr (
752- self , "%s_element" % method .lower ()
753- )(class_name , item_id , input )
754- output = RestfulInstance .data_obj (output )
755- self .client .response_code = response_code
756- except IndexError , msg :
757- output = RestfulInstance .error_obj (404 , msg )
758- self .client .response_code = 404
759- except Unauthorised , msg :
760- output = RestfulInstance .error_obj (403 , msg )
761- self .client .response_code = 403
762- except (hyperdb .DesignatorError , UsageError ), msg :
763- output = RestfulInstance .error_obj (400 , msg )
764- self .client .response_code = 400
765- except (AttributeError , Reject ), msg :
766- output = RestfulInstance .error_obj (405 , msg )
767- self .client .response_code = 405
768- except ValueError , msg :
769- output = RestfulInstance .error_obj (409 , msg )
770- self .client .response_code = 409
771- except NotImplementedError :
772- output = RestfulInstance .error_obj (402 , 'Method under development' )
773- self .client .response_code = 402
774- # nothing to pay, just a mark for debugging purpose
775- except :
776- # if self.DEBUG_MODE in roundup_server
777- # else msg = 'An error occurred. Please check...',
778- exc , val , tb = sys .exc_info ()
779- output = RestfulInstance .error_obj (400 , val )
780- self .client .response_code = 400
781-
782- # out to the logfile, it would be nice if the server do it for me
783- print 'EXCEPTION AT' , time .ctime ()
784- traceback .print_exc ()
785- finally :
793+ output = getattr (
794+ self , "%s_element" % method .lower ()
795+ )(class_name , item_id , input )
796+
786797 if format_output .lower () == "json" :
787798 self .client .setHeader ("Content-Type" , "application/json" )
788799 if pretty_output :
0 commit comments