Error handling

Defines

#define assure(BOOL, CODE,...)   irplib_error_assure(BOOL, CODE, (__VA_ARGS__), goto cleanup)
#define assure_nomsg(BOOL, CODE)   irplib_error_assure(BOOL, CODE, (" "), goto cleanup)
#define assure_mem(PTR)
#define ck0(IEXP,...)
#define ck0_nomsg(IEXP)   ck0(IEXP," ")
#define cknull(NULLEXP,...)
#define cknull_nomsg(NULLEXP)   cknull(NULLEXP," ")
#define check(CMD,...)
#define check_nomsg(CMD)   check(CMD, " ")
#define passure(BOOL,...)
#define uves_error_reset()   irplib_error_reset()
#define uves_error_dump()   irplib_error_dump(CPL_MSG_ERROR, CPL_MSG_ERROR)

Detailed Description

Warning: this documentation is outdated. Please refer to the documentation of the error handler in IRPLIB.

This error handling module extends CPL's error handler by adding error tracing and automatic memory deallocation in case of an error. Like in CPL the current error state is indicated by the cpl_error_code (returned by the function cpl_error_get_code() ).

The error tracing makes it possible to see where (source file, function name, line number) an error first occured, as well as the sequence of function calls preceding the error. A typical output looks like:

   An error occured, dumping error trace:
   
   Wavelength calibration did not converge. After 13 iterations the RMS was 
   0.300812 pixels. Try to improve the initial guess solution (The iterative
   process did not converge)
     in [3]uves_wavecal_identify() at uves_wavecal_identify.c :101
    
   Could not calibrate orders
     in [2]uves_wavecal_process_chip() at uves_wavecal.c  :426
     
   Wavelength calibration failed
     in [1]uves_wavecal() at uves_wavecal.c  :679

However, the main motivation of this extension is to simplify the error checking and handling. A single line of source code

   check( dispersion_relation = 
   uves_wavecal_identify(linetable[window-1],
                         line_refer,
             initial_dispersion, 
             WAVECAL_MODE, DEGREE, TOLERANCE, ALPHA, MAXERROR),
           "Could not calibrate orders");

has the same effect as

   if (cpl_error_get_code() != CPL_ERROR_NONE) {
      cpl_msg_error(__func__, "An unexpected error (%s) has occurred in %s() at %-15s :%-3d",
                           cpl_error_get_message(),
                           __func__,
                           __FILE__,
                           __LINE__);
      uves_free_image(&spectrum);
      uves_free_image(&cropped_image);
      uves_free_image(&debug_image);
      uves_free_cpl(&relative_order);
      polynomial_delete(&initial_dispersion);
      polynomial_delete(&dispersion_relation);
      return NULL;
   }

   dispersion_relation = 
   uves_wavecal_identify(linetable[window-1],
                         line_refer,
             initial_dispersion, 
             WAVECAL_MODE, DEGREE, TOLERANCE, ALPHA, MAXERROR);

   if (cpl_error_get_code() != CPL_ERROR_NONE) {
      cpl_msg_error(__func__, "ERROR: Could not calibrate orders "
                              "(%s) in %s() at %-15s :%-3d",
                           cpl_error_get_message(),
                           __func__,
                           __FILE__,
                           __LINE__);
      uves_free_image(&spectrum);
      uves_free_image(&cropped_image);
      uves_free_image(&debug_image);
      uves_free_cpl(&relative_order);
      polynomial_delete(&initial_dispersion);
      polynomial_delete(&dispersion_relation);
      return NULL;
   }

This of course makes the source code more compact and hence easier to read (and maintain) and allows for intensive error checking with minimal effort.

Additionally, editing the check() macro (described below) allows for debugging/tracing information at every function entry and exit.

Usage

New errors are set with the macros assure() and passure(), and sub-functions that might set a cpl_error_code are checked using the macros check() and pcheck() . The function _uves_error_set() should never be called directly. These macros check if an error occured and, if so, jumps to the cleanup label which must be defined at the end of each function. After the cleanup label every pointer used by the function is deallocated and the function returns. Also a string variable named fctid (function identification), must be defined in every function and contain the name of the current function.

At the very end of a recipe the error state should be checked and uves_error_dump() called on error:

   if ( cpl_error_get_code() != CPL_ERROR_NONE )
   {
      uves_error_dump(__func__);
   }

When using this scheme:

Consider the example

   int function_name(...)
   {
      cpl_image * image = NULL;
      cpl_image * another_image;  / *  Wrong: Pointer must be initialized to NULL. On cleanup, 
                                              cpl_image_delete() will try to deallocate whatever
                                              this pointer points to. If the pointer is NULL,
                                              the deallocator function will do nothing.  * /
      :
      :

      {
         cpl_object * object = NULL;   / *  Wrong: Pointer must be declared at 
                                               the beginning of a function.
                                                   This object will not be deallocated, 
                           if the following
                                                   check() fails. * /
     
         object = cpl_object_new();

         :
         :
              
         check( ... );

         :
         :

         cpl_object_delete(object);    / *  Wrong: The pointer must be set to NULL after
                                               deallocation, or
                                                   the following assure() might cause the
                           already deallocated object
                                                   to be deallocated again.  * /
         :
         :
     
         assure( ... );

         return 7;                     / *  Wrong: Only one exit point per function. * /

      }
      
      :
      :

    cleanup:
      cpl_image_delete(image);
      cpl_image_delete(another_image);

      return 7;
   }

This is easily fixed:

   int function_name(...)
   {
      cpl_image  * image         = NULL;  / *  All pointers are declared at the beginning  * /
      cpl_image  * another_image = NULL;  / *  of the function an initialized to NULL.     * /
      cpl_object * object        = NULL;

      :
      :

      {

         object = cpl_object_new();

         :
         :
              
         check( ... );

         :
         :

         uves_free_object(&object);            / *  The object is deallocated 
                                                and the pointer set to NULL.  * /

         :
         :
     
         assure( ... );

      }
      
      :
      :

    cleanup:
      uves_free_image (&image);                / *  All objects are deallocated here.  * /
      uves_free_image (&another_image);
      uves_free_object(&object);

      return 7;                           / *  This is the only exit point of the function. * /
   }

(Note that uves_free_image() et al. can be used instead of cpl_image_delete() et al. as a way to ensure that a pointer is always set to NULL after deallocation).

Recovering from an error

To recover from an error, call uves_error_reset(), not cpl_error_reset(). Example:

   n = cpl_table_get_nrow(t);
   if (cpl_error_get_code() == CPL_ERROR_NULL_INPUT)  / *  This error code 
                                                           is set if 't' is NULL.  * /
   {
      / *  Recover from this error  * /

      uves_error_reset();
      n = -3;
   }
   else  / *  Also check for unexpected errors  * /
   {
      assure( cpl_error_get_code() == CPL_ERROR_NONE, cpl_error_get_code(), 
              "Error reading table size");
   }

However, error recovery is usually best avoided, and the functionality above is better written as:

   if (t != NULL)
   {
      check( n = cpl_table_get_nrow(t), "Error reading table size");
   }
   else
   {
      n = -3;
   }

Define Documentation

#define assure_nomsg ( BOOL,
CODE   )     irplib_error_assure(BOOL, CODE, (" "), goto cleanup)
#define assure_mem ( PTR   ) 
#define ck0 ( IEXP,
...   ) 
Value:
irplib_error_assure(IEXP == 0, CPL_ERROR_UNSPECIFIED, \
  (__VA_ARGS__), goto cleanup)

Definition at line 187 of file uves_error.h.

#define cknull ( NULLEXP,
...   ) 
Value:
irplib_error_assure((NULLEXP) != NULL, \
  CPL_ERROR_UNSPECIFIED, (__VA_ARGS__), goto cleanup)

Definition at line 193 of file uves_error.h.

#define check ( CMD,
...   ) 
Value:
irplib_error_assure((uves_msg_softer(), (CMD), uves_msg_louder(),      \
              cpl_error_get_code() == CPL_ERROR_NONE),       \
                       cpl_error_get_code(), (__VA_ARGS__), goto cleanup)

cpl_error_code + message

Definition at line 201 of file uves_error.h.

Referenced by calculate_spacing(), calibrate_global(), compute_lambda(), create_descr(), delete_peak(), detect_lines(), detect_ripples(), estimate_threshold(), extract_ff_rebin_merge(), extract_order_simple(), fit_order_linear(), flames_midas_sccfnd(), flames_midas_sccget(), flames_midas_scddel(), flames_midas_scdfnd(), flames_midas_scdprs(), flames_midas_scdrdi(), flames_midas_scfget(), flames_midas_scfinf(), flames_midas_scfput(), flames_midas_tccini(), flames_midas_tccser(), flames_midas_tciget(), flames_midas_tcsget(), flames_midas_tcsput(), frame_close(), get_descr_info(), get_orderlength(), get_xcenter(), get_ycenter(), load_frame(), load_frame_header(), load_header(), main(), opt_define_sky(), opt_extract(), opt_extract_sky(), opt_measure_profile(), opt_measure_profile_order(), opt_reject_outlier(), parse_history(), repeat_orderdef(), revise_noise(), scdcop(), scdrd(), scdwr(), subtract_sky(), subtract_sky_row(), table_colname_from_number(), test_bad_corr(), test_extract(), test_gaussian_fitting(), test_iterate(), test_load_3dtable(), test_process(), tflat_qclog(), trace_order(), uves_align(), uves_baryvel(), uves_calculate_response(), uves_combine_flats(), uves_correct_badpix(), uves_correct_badpix_all(), uves_define_noise(), uves_delete_bad_lines(), uves_draw_lines(), uves_draw_orders(), uves_extract(), uves_filter_image_median(), uves_fit_gaussian_2d_image(), uves_flat_create_normalized_master(), uves_flat_create_normalized_master2(), uves_flatfielding(), uves_get_blaze_ratio(), uves_get_extract_method(), uves_get_flatfield_method(), uves_get_merge_method(), uves_hough(), uves_initialize(), uves_initialize_image_header(), uves_locate_orders(), uves_merge_orders(), uves_mflat_at_ypos(), uves_mflat_exe_body(), uves_mflat_process_chip(), uves_msflats(), uves_normalize_spectrum(), uves_ordertable_traces_add(), uves_ordertable_traces_new(), uves_physmod_calmap(), uves_physmod_center_gauss(), uves_physmod_chop_otab(), uves_physmod_create_table(), uves_physmod_msrawxy(), uves_physmod_plotmod(), uves_physmod_qc1pmtbl(), uves_physmod_regress_echelle(), uves_physmod_stability_check(), uves_polynomial_convert_from_table(), uves_polynomial_derivative(), uves_polynomial_derivative_1d(), uves_polynomial_duplicate(), uves_polynomial_evaluate_1d(), uves_polynomial_fit_1d(), uves_polynomial_get_coeff_1d(), uves_polynomial_get_coeff_2d(), uves_polynomial_new(), uves_polynomial_regression_1d(), uves_polynomial_regression_2d(), uves_polynomial_regression_2d_autodegree(), uves_polynomial_solve_1d(), uves_polynomial_solve_2d(), uves_print_cpl_frameset(), uves_print_cpl_property(), uves_print_uves_propertylist(), uves_qclog_add_sci(), uves_rebin(), uves_reduce(), uves_reduce_mflat(), uves_reduce_mflat_combine(), uves_reduce_scired(), uves_response_efficiency(), uves_scired_process_chip(), uves_spline_hermite_table(), uves_subtract_bias(), uves_subtract_dark(), uves_utl_ima_arith(), uves_utl_physmod(), uves_utl_rcosmic(), uves_utl_remove_crh_single(), uves_wavecal_identify(), uves_wavecal_search(), and verify_calibration().

#define check_nomsg ( CMD   )     check(CMD, " ")

cpl_error_code

Definition at line 207 of file uves_error.h.

Referenced by calibrate_global(), convert_midas_array(), convert_to_history(), create_line_table(), create_order_table(), create_spectrum(), detect_lines(), extract_ff_rebin_merge(), extract_order_simple(), flames_align_table_column(), flames_midas_sccget(), flames_midas_tcbget(), flames_midas_tccini(), flames_midas_tcfget(), flames_midas_tclget(), flames_midas_tcuget(), flames_utl_unpack(), main(), opt_extract(), opt_measure_profile(), opt_measure_profile_order(), parse_midas_poly(), scdcop(), scired_qclog(), set_column_format_unit_tnull(), table_erase_selected(), tcerd(), tcewr(), test_3dtable(), test_extract(), test_iterate(), test_load_linetable(), test_polynomial(), test_polynomial_fit_2d(), tflat_qclog(), uves_average_reject(), uves_combine_flats(), uves_cosrout(), uves_delete_bad_lines(), uves_end(), uves_extract(), uves_flat_create_normalized_master(), uves_flat_create_normalized_master2(), uves_flatfielding(), uves_get_blaze_ratio(), uves_get_wave_map(), uves_hough(), uves_image_mflat_detect_blemishes(), uves_imagelist_get_clean_mean_levels(), uves_imagelist_subtract_values(), uves_ksigma_stack(), uves_ksigma_vector(), uves_locate_orders(), uves_mflat_at_ypos(), uves_mflat_combine_exe_body(), uves_mflat_define_parameters_body(), uves_mflat_one(), uves_mflat_process_chip(), uves_mflat_qclog(), uves_physmod_align_tables(), uves_physmod_center_gauss(), uves_physmod_chop_otab(), uves_physmod_create_table(), uves_physmod_msrawxy(), uves_physmod_qc1pmtbl(), uves_physmod_stability_check(), uves_polynomial_regression_2d_autodegree(), uves_qclog_add_common_wave(), uves_qclog_add_sci(), uves_qclog_dump_common(), uves_qclog_dump_common_wave(), uves_qclog_init(), uves_rcosmic(), uves_rebin(), uves_reduce_mflat(), uves_reduce_mflat_combine(), uves_reduce_scired(), uves_remove_crh_single(), uves_tablename_remove_units(), uves_tablenames_unify_units(), uves_utl_ima_arith(), uves_utl_physmod(), uves_utl_rcosmic(), uves_utl_remove_crh_single(), and uves_wavecal_identify_lines_ppm().

#define passure ( BOOL,
...   ) 
 
#define uves_error_reset (  )     irplib_error_reset()
 
#define uves_error_dump (  )     irplib_error_dump(CPL_MSG_ERROR, CPL_MSG_ERROR)

dump

Definition at line 222 of file uves_error.h.

Referenced by main().


Generated on 8 Mar 2011 for UVES Pipeline Reference Manual by  doxygen 1.6.1