Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Changes between Version 15 and Version 16 of HowDoesErrorHandlingWork


Ignore:
Timestamp:
06/07/06 08:33:31 (13 years ago)
Author:
simon
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • HowDoesErrorHandlingWork

    v15 v16  
    6060 
    6161What if we want to handle invalid baz-es differently than invalid bar-s? Fortunately for us, ''error_handler'' takes a second keyword argument ''rules'', making it possible to influence selection of appropriate error handler (actually a limited interface to ''RuleDispatch''[1]). 
    62 Rules are arbitrary logical Python expressions passed as lists. For example: 
     62Rules are arbitrary logical Python expressions passed as strings. For example: 
    6363{{{ 
    6464@error_handler(baz_eh, "'baz' in tg_errors") 
     
    7777== 1.3 Fail-safe mechanism == 
    7878 
    79 If validation fails for an input, said input also does not get to be converted to designated Python type, which could be rather inconvenient. A fail-safe mechanism is provided for such cases. Decorator ''validate'' can take two additional keyword arguments ''failsafe_scheme'' and ''failsafe_values'' to fine-tune how to failed inputs are handled.  
    80  
    81 Predefined schemas (see source:trunk/turbogears/errorhandling.py): 
    82  
    83  * ''!FailsafeSchema.none'': Default. No conversions are preformed, input remains as is: 
    84  {{{ 
    85  @validate(validators={...}) 
    86  }}} 
    87  or explicitly: 
    88  {{{ 
    89  @validate(validators={...}, failsafe_scheme=errorhandling.FailsafeSchema.none) 
    90  }}} 
    91  ''failsafe_values'' is ignored. 
    92  * ''!FailsafeSchema.values'': If ''failsafe_values'' is a dict, every erroneous input for which exists a key in ''failsafe_values'' is replaced with corresponding value. Otherwise all erroneous inputs are replaced with (same instance of!) ''failsafe_values''. 
    93  {{{ 
    94  @validate(validators={"bar":validators.Int(), "baz":validators.Int()}, failsafe_schema=FailsafeSchema.values, failsafe_values={"bar":1, "baz":2}) 
    95  }}} 
    96  If erroneous, bar is set to (int) 1, while baz to 2. 
    97  {{{ 
    98  @validate(validators={"bar":validators.Int(), "baz":validators.Int()}, failsafe_schema=FailsafeSchema.values, failsafe_values=13) 
    99  }}} 
    100  Both bar and baz are set to 13 when erroneous. 
    101  
    102  * ''!FailsafeSchema.map_errors'': Erroneous inputs are mapped to corresponding exceptions.  
    103  {{{ 
    104  @validate(validators={"bar":validators.Int(), "baz":validators.Int()}, failsafe_schema=FailsafeSchema.map_errors) 
    105  }}} 
    106  ''failsafe_values'' is ignored. 
    107  
    108  * ''FailsafeSchema.defaults'': Erroneous inputs are mapped to method defaults. 
    109  {{{ 
    110  @validate(validators={"bar":validators.Int(), "baz":validators.Int()}, failsafe_schema=FailsafeSchema.defaults) 
    111  def foo(self, baz=1, bar=2): 
    112     ... 
    113  }}} 
    114  If erroneous, bar is set to (int) 1, while baz to 2. 
    115  
    116  ''failsafe_values'' is ignored. 
    117  
    118 User defined schemas can be added by defining new specialisations for generic function ''errorhandling.dispatch_failsafe'' (see source:trunk/turbogears/errorhandling.py) with signature: 
    119 {{{ 
    120 dispatch_failsafe(schema, values, errors, source, kw) 
    121 }}} 
    122  
     79If validation fails for an input, said input also does not get to be converted to designated Python type, which could be rather inconvenient. A fail-safe mechanism is provided for such cases.  
    12380 
    12481= 2 Exceptions =