Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 3 commits
  • 3 files changed
  • 0 comments
  • 1 contributor
Mar 17, 2012
Benjamin Yu CodedUUID is now Timestamp based.
Details:
* Adds ability to analyze a series of requests and responses
  that use our Serf::More::UuidFields by time dimension without needing
  to add any additional timestamp (or DateTime) fields.
f03b9f2
Benjamin Yu Adds Serf::Util::ProtectedCall module.
Details:
* Helper method to execute a block and to catch any possible errors raised.
  The return value is the results of the block, or the raised error.
  We also return a flag to signal if the returned value is an error or
  the results of the block.
c9443f4
Benjamin Yu Enhances ErrorHandling with ProtectedCall and custom errback.
Details:
* ErrorHandling now uses the protected_call module to catch the
  raised exceptions when calling the block.
* ErrorHandling's code to rescue from the error has been pulled out
  into its own method. This method may be overridden by including classes.
* with_error_handlings now accepts an additional parameter to
  explicitly declare a different error handling method to call
  in response to an error. This parameter, may instead, be a Proc
  to be called directly.
f510745
46  lib/serf/util/error_handling.rb
@@ -3,6 +3,7 @@
3 3
 require 'serf/messages/caught_exception_event'
4 4
 require 'serf/util/null_object'
5 5
 require 'serf/util/options_extraction'
  6
+require 'serf/util/protected_call'
6 7
 
7 8
 module Serf
8 9
 module Util
@@ -11,21 +12,34 @@ module Util
11 12
   # Helper module to rescues exceptions from executing blocks of
12 13
   # code, and then logs+pushes the error event.
13 14
   #
  15
+  # Including classes may have the following instance variables
  16
+  # to override the default values:
  17
+  # * @error_event_class - ::Serf::Messages::CaughtExceptionEvent
  18
+  # * @logger - ::Serf::Util::NullObject.new
  19
+  # * @error_channel - ::Serf::Util::NullObject.new
14 20
   module ErrorHandling
15  
-    include ::Serf::Util::OptionsExtraction
  21
+    include Serf::Util::OptionsExtraction
  22
+    include Serf::Util::ProtectedCall
16 23
 
17 24
     ##
18 25
     # A block wrapper to handle errors when executing a block.
19 26
     #
20  
-    # Including classes may have the following instance variables
21  
-    # to override the default values:
22  
-    # * @error_event_class - ::Serf::Messages::CaughtExceptionEvent
23  
-    # * @logger - ::Serf::Util::NullObject.new
24  
-    # * @error_channel - ::Serf::Util::NullObject.new
  27
+    def with_error_handling(context=nil, errback=:handle_error, &block)
  28
+      ok, results = protected_call &block
  29
+      return ok, (ok ?
  30
+        results :
  31
+        ((errback.is_a?(Proc) ?
  32
+          errback.call(results, context) :
  33
+          send(errback, results, context))))
  34
+    end
  35
+
  36
+    ##
  37
+    # Including classes may override this method to do alternate error
  38
+    # handling. By default, this method will create a new caught exception
  39
+    # event and publish it to the error channel. This method will also
  40
+    # log the exception itself to the logger.
25 41
     #
26  
-    def with_error_handling(context=nil)
27  
-      return true, yield
28  
-    rescue => e
  42
+    def handle_error(e, context=nil)
29 43
       eec = opts :error_event_class, ::Serf::Messages::CaughtExceptionEvent
30 44
       logger = opts :logger, ::Serf::Util::NullObject.new
31 45
       error_channel = opts :error_channel, ::Serf::Util::NullObject.new
@@ -35,20 +49,18 @@ def with_error_handling(context=nil)
35 49
         message: e.message,
36 50
         backtrace: e.backtrace.join("\n"))
37 51
 
38  
-      # log the error to our logger, and to our error channel.
39  
-      logger.error error_event
  52
+      # log the error to our logger
  53
+      logger.error e
  54
+
  55
+      # log the error event to our error channel.
40 56
       begin
41 57
         error_channel.push error_event
42 58
       rescue => e1
43  
-        logger.error("
44  
-          Failed pushing to ErrorChannel:
45  
-          #{e1.message}
46  
-          #{e1.backtrace.join('\n')}
47  
-        ")
  59
+        logger.error e1
48 60
       end
49 61
 
50 62
       # We're done, so just return this error.
51  
-      return false, error_event
  63
+      return error_event
52 64
     end
53 65
 
54 66
   end
35  lib/serf/util/protected_call.rb
... ...
@@ -0,0 +1,35 @@
  1
+module Serf
  2
+module Util
  3
+
  4
+  ##
  5
+  # Rescues exceptions raised when calling blocks.
  6
+  #
  7
+  module ProtectedCall
  8
+
  9
+    ##
  10
+    # A block wrapper to catch errors when executing a block. Instead of
  11
+    # raising the error, the error is caught and returned in place of
  12
+    # the block's results.
  13
+    #
  14
+    #   ok, results = protected_call do
  15
+    #     1 + 1
  16
+    #   end
  17
+    #   => [true, 2]
  18
+    #
  19
+    #   ok, results = protected_call do
  20
+    #     raise 'My Error'
  21
+    #   end
  22
+    #   => [false, RuntimeError]
  23
+    #
  24
+    # @return boolean success and the block's (or caught exception) results.
  25
+    #
  26
+    def protected_call
  27
+      return true, yield
  28
+    rescue => e
  29
+      return false, e
  30
+    end
  31
+
  32
+  end
  33
+
  34
+end
  35
+end
6  lib/serf/util/uuidable.rb
@@ -13,7 +13,9 @@ module Util
13 13
   module Uuidable
14 14
 
15 15
     ##
16  
-    # Creates a Random UUID, base64 encoded.
  16
+    # Creates a Timestamp UUID, base64 encoded.
  17
+    #
  18
+    # NOTE: UUIDTools TimeStamp code creates a UTC based timestamp UUID.
17 19
     #
18 20
     def create_coded_uuid
19 21
       # All raw UUIDs are 16 bytes long. Base64 lengthens the string to
@@ -22,7 +24,7 @@ def create_coded_uuid
22 24
       # of an extra 6 bytes over raw UUID, but with the readability
23 25
       # benefit. And saves us 14 bytes of size from the 'standard'
24 26
       # string hex representation of UUIDs.
25  
-      Base64.urlsafe_encode64(UUIDTools::UUID.random_create.raw).chomp('==')
  27
+      Base64.urlsafe_encode64(UUIDTools::UUID.timestamp_create.raw).chomp('==')
26 28
     end
27 29
 
28 30
     ##

No commit comments for this range

Something went wrong with that request. Please try again.