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 BOOL,
CODE   )     irplib_error_assure(BOOL, CODE, (__VA_ARGS__), goto cleanup)
 

Condition + message

Definition at line 111 of file uves_error.h.

Referenced by area_above_line(), calculate_spacing(), calibrate_global(), delete_peak(), derivative_cpl_polynomial(), detect_lines(), estimate_sn(), estimate_threshold(), eval_gauss(), extract_order_simple(), filter_median(), find_centroid(), get_offset(), identify_lines(), opt_extract(), opt_sample_spatial_profile(), parse_history(), repeat_orderdef(), subtract_sky(), test_bad_corr(), test_extract(), test_gaussian_fitting(), test_iterate(), test_qc_name(), update_max(), uves_average_images(), uves_check_version(), uves_chop_eso_prefix(), uves_correct_badpix(), uves_define_noise(), uves_delete_bad_lines(), uves_extract(), uves_filter_image_average(), uves_filter_image_median(), uves_find_property_const(), uves_fit_gaussian_2d_image(), uves_flatfielding(), uves_get_badpix(), uves_get_extract_method(), uves_get_flatfield_method(), uves_get_merge_method(), uves_hough(), uves_locate_orders(), uves_merge_orders(), uves_normalize_spectrum(), uves_ordertable_traces_add(), uves_pfits_get_datancom(), uves_pfits_get_exptime(), uves_pfits_get_gratwlen(), uves_pfits_get_ordpred(), uves_pfits_get_ron_adu(), uves_pfits_get_slitlength_pixels(), uves_pfits_get_wend(), uves_pfits_get_wstart(), uves_pfits_set_wend(), uves_pfits_set_wstart(), uves_physmod_msrawxy(), uves_polynomial_add_2d(), uves_polynomial_collapse(), uves_polynomial_convert_from_plist_midas(), uves_polynomial_convert_from_table(), uves_polynomial_convert_to_table(), uves_polynomial_derivative(), uves_polynomial_derivative_1d(), uves_polynomial_derivative_2d(), uves_polynomial_duplicate(), uves_polynomial_evaluate_1d(), uves_polynomial_evaluate_2d(), uves_polynomial_fit_1d(), uves_polynomial_fit_2d(), uves_polynomial_get_coeff_1d(), uves_polynomial_get_coeff_2d(), uves_polynomial_get_degree(), uves_polynomial_get_dimension(), uves_polynomial_new(), uves_polynomial_new_zero(), uves_polynomial_regression_1d(), uves_polynomial_regression_2d(), uves_polynomial_regression_2d_autodegree(), uves_polynomial_rescale(), uves_polynomial_shift(), uves_polynomial_solve_1d(), uves_polynomial_solve_2d(), uves_print_uves_propertylist(), uves_rebin(), uves_reduce(), uves_remove_string_prefix(), uves_response_efficiency(), uves_spline_cubic(), uves_wavecal_identify(), uves_wavecal_identify_lines_ppm(), uves_wavecal_search(), and xcenter().

#define assure_nomsg BOOL,
CODE   )     irplib_error_assure(BOOL, CODE, (" "), goto cleanup)
 

Condition

Definition at line 115 of file uves_error.h.

Referenced by create_line_table(), create_order_table(), test_iterate(), uves_align(), uves_hough(), uves_locate_orders(), uves_normalize_spectrum(), uves_polynomial_fit_1d(), uves_reduce(), and uves_spline_cubic().

#define assure_mem PTR   ) 
 

Value:

irplib_error_assure((PTR) != NULL, CPL_ERROR_ILLEGAL_OUTPUT, \
  ("Memory allocation failure!"), goto cleanup)
Memory allocation

Definition at line 119 of file uves_error.h.

Referenced by calibrate_global(), filter_median(), identify_lines(), opt_measure_profile(), opt_measure_profile_order(), opt_sample_spatial_profile(), opt_subtract_sky(), revise_noise(), test_bad_corr(), test_gaussian_fitting(), test_process(), tflat_qclog(), uves_create_image(), uves_define_noise(), uves_end(), uves_get_blaze_ratio(), uves_lt_new(), uves_pfits_get_wend(), uves_pfits_get_wstart(), uves_pfits_set_wend(), uves_pfits_set_wstart(), uves_polynomial_collapse(), uves_polynomial_fit_2d(), uves_polynomial_new(), uves_polynomial_new_zero(), and uves_polynomial_regression_2d_autodegree().

#define ck0 IEXP   ) 
 

Value:

irplib_error_assure(IEXP == 0, CPL_ERROR_UNSPECIFIED, \
  (__VA_ARGS__), goto cleanup)

Definition at line 122 of file uves_error.h.

#define cknull NULLEXP   ) 
 

Value:

irplib_error_assure((NULLEXP) != NULL, \
  CPL_ERROR_UNSPECIFIED, (__VA_ARGS__), goto cleanup)

Definition at line 128 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 136 of file uves_error.h.

Referenced by calculate_spacing(), calibrate_global(), compute_lambda(), delete_peak(), detect_lines(), detect_ripples(), estimate_threshold(), extract_ff_rebin_merge(), extract_order_simple(), fit_order_linear(), get_orderlength(), get_xcenter(), get_ycenter(), main(), opt_define_sky(), opt_extract(), opt_extract_sky(), opt_measure_profile(), opt_measure_profile_order(), opt_reject_outlier(), parse_history(), repeat_orderdef(), subtract_sky(), subtract_sky_row(), test_bad_corr(), test_extract(), test_gaussian_fitting(), test_iterate(), tflat_qclog(), trace_order(), uves_align(), uves_baryvel(), uves_calculate_response(), 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_flames_pfits_get_dit(), uves_flames_pfits_get_ident(), uves_flames_pfits_get_nflats(), uves_flames_pfits_get_object(), uves_flames_pfits_get_plateid(), uves_flames_pfits_set_ccfposmax(), uves_flames_pfits_set_newplateid(), uves_flatfielding(), uves_format_is_new(), 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_pfits_get_airmass_end(), uves_pfits_get_airmass_start(), uves_pfits_get_ambipress(), uves_pfits_get_arcfile(), uves_pfits_get_badpxframe(), uves_pfits_get_binx(), uves_pfits_get_biny(), uves_pfits_get_bitpix(), uves_pfits_get_bunit(), uves_pfits_get_ccdid(), uves_pfits_get_cdelt1(), uves_pfits_get_cdelt2(), uves_pfits_get_chip_name(), uves_pfits_get_chipchoice(), uves_pfits_get_chipid(), uves_pfits_get_conad(), uves_pfits_get_crpix1(), uves_pfits_get_crpix2(), uves_pfits_get_crval1(), uves_pfits_get_crval2(), uves_pfits_get_ctype1(), uves_pfits_get_ctype2(), uves_pfits_get_datancom(), uves_pfits_get_date_obs(), uves_pfits_get_dec(), uves_pfits_get_dpr_catg(), uves_pfits_get_dpr_tech(), uves_pfits_get_dpr_type(), uves_pfits_get_drs_id(), uves_pfits_get_exptime(), uves_pfits_get_gain(), uves_pfits_get_geolat(), uves_pfits_get_geolon(), uves_pfits_get_gratid(), uves_pfits_get_gratname(), uves_pfits_get_gratwlen(), uves_pfits_get_humidity(), uves_pfits_get_insmode(), uves_pfits_get_inspath(), uves_pfits_get_maxfibres(), uves_pfits_get_mjdobs(), uves_pfits_get_naxis(), uves_pfits_get_naxis1(), uves_pfits_get_naxis2(), uves_pfits_get_nx(), uves_pfits_get_ny(), uves_pfits_get_ocs_simcal(), uves_pfits_get_offset(), uves_pfits_get_ordpred(), uves_pfits_get_origfile(), uves_pfits_get_out1nx(), uves_pfits_get_out1ny(), uves_pfits_get_out4nx(), uves_pfits_get_out4ny(), uves_pfits_get_ovrscanx(), uves_pfits_get_ovrscany(), uves_pfits_get_pipefile(), uves_pfits_get_pixelscale(), uves_pfits_get_prescanx(), uves_pfits_get_prescany(), uves_pfits_get_pressure(), uves_pfits_get_pro_catg(), uves_pfits_get_ra(), uves_pfits_get_readspeed(), uves_pfits_get_rec1raw1name(), uves_pfits_get_ron_adu(), uves_pfits_get_slit1_name(), uves_pfits_get_slit3_x1encoder(), uves_pfits_get_slit3_x2encoder(), uves_pfits_get_slitlength(), uves_pfits_get_slitlength_pixels(), uves_pfits_get_slitwidth(), uves_pfits_get_startx(), uves_pfits_get_starty(), uves_pfits_get_targ_name(), uves_pfits_get_tempcam(), uves_pfits_get_templateid(), uves_pfits_get_tpl_start(), uves_pfits_get_uit(), uves_pfits_get_utc(), uves_pfits_get_wend(), uves_pfits_get_wstart(), uves_pfits_set_badpixcorr(), uves_pfits_set_bunit(), uves_pfits_set_cdelt1(), uves_pfits_set_cdelt2(), uves_pfits_set_crpix1(), uves_pfits_set_crpix2(), uves_pfits_set_crval1(), uves_pfits_set_crval2(), uves_pfits_set_ctype1(), uves_pfits_set_ctype2(), uves_pfits_set_data_average(), uves_pfits_set_data_max(), uves_pfits_set_data_median(), uves_pfits_set_data_min(), uves_pfits_set_data_stddev(), uves_pfits_set_dec(), uves_pfits_set_exptime(), uves_pfits_set_history_val(), uves_pfits_set_hs(), uves_pfits_set_object(), uves_pfits_set_ocs_simcal(), uves_pfits_set_ordpred(), uves_pfits_set_ra(), uves_pfits_set_redlevel(), uves_pfits_set_starttime(), uves_pfits_set_status(), uves_pfits_set_stoptime(), uves_pfits_set_wend(), uves_pfits_set_wstart(), 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_plist_midas(), uves_polynomial_convert_from_table(), uves_polynomial_derivative(), uves_polynomial_derivative_1d(), uves_polynomial_duplicate(), uves_polynomial_evaluate_1d(), uves_polynomial_get_coeff_1d(), uves_polynomial_get_coeff_2d(), uves_polynomial_new(), uves_polynomial_regression_1d(), uves_polynomial_regression_2d(), uves_polynomial_solve_1d(), uves_polynomial_solve_2d(), uves_print_cpl_frameset(), uves_print_cpl_property(), uves_print_uves_propertylist(), uves_rebin(), uves_reduce(), uves_reduce_mflat(), uves_reduce_scired(), uves_response_efficiency(), uves_scired_process_chip(), uves_spline_hermite_table(), uves_subtract_bias(), uves_subtract_dark(), uves_wavecal_identify(), uves_wavecal_search(), and verify_calibration().

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

cpl_error_code

Definition at line 142 of file uves_error.h.

Referenced by calibrate_global(), create_order_table(), main(), scired_qclog(), test_extract(), test_iterate(), test_load_linetable(), test_polynomial(), test_polynomial_fit_2d(), tflat_qclog(), uves_average_reject(), uves_delete_bad_lines(), uves_end(), uves_extract(), uves_hough(), uves_mflat_at_ypos(), uves_mflat_one(), uves_mflat_qclog(), uves_pfits_get_slitlength_pixels(), uves_physmod_center_gauss(), uves_physmod_create_table(), uves_physmod_msrawxy(), 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_reduce_scired(), and uves_wavecal_identify_lines_ppm().

#define passure BOOL   ) 
 

Value:

irplib_error_assure(BOOL, CPL_ERROR_UNSPECIFIED,                       \
                     ("Internal error. Please report to "                \
                      PACKAGE_BUGREPORT " " __VA_ARGS__), goto cleanup)
assertion + message

Definition at line 145 of file uves_error.h.

Referenced by calibrate_global(), compute_lambda(), count_orders(), detect_lines(), estimate_sn(), estimate_threshold(), extract_ff_rebin_merge(), extract_order_simple(), find_centroid(), fit_order_linear(), get_offset(), identify_lines(), opt_get_order_width(), opt_measure_profile(), opt_measure_profile_order(), subtract_sky(), uves_draw_lines(), uves_draw_orders(), uves_extract(), uves_flatfielding(), uves_get_blaze_ratio(), uves_locate_orders(), uves_merge_orders(), uves_rebin(), uves_reduce(), uves_subtract_bias(), uves_subtract_dark(), uves_table_is_sorted_double(), uves_wavecal_identify(), uves_wavecal_search(), and xcenter().

 
#define uves_error_reset  )     irplib_error_reset()
 

reset

Definition at line 153 of file uves_error.h.

Referenced by find_centroid(), opt_get_order_width(), repeat_orderdef(), uves_draw_lines(), uves_polynomial_fit_2d_test(), and xcenter().

 
#define uves_error_dump  )     irplib_error_dump(CPL_MSG_ERROR, CPL_MSG_ERROR)
 

dump

Definition at line 157 of file uves_error.h.

Referenced by main().


Generated on Tue Jun 19 14:39:21 2007 for UVES Pipeline Reference Manual by  doxygen 1.4.6