UVES Pipeline Reference Manual  5.4.6
Macros
Error handling

Macros

#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)
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 * /
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;
}

Macro Definition 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 184 of file uves_error.h.

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

Definition at line 190 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)
#define uves_msg_softer()
Decrease message volume.
Definition: uves_msg.h:126
#define uves_msg_louder()
Increase message volume.
Definition: uves_msg.h:133

cpl_error_code + message

Definition at line 198 of file uves_error.h.

Referenced by calculate_spacing(), calibrate_global(), compute_lambda(), create_descr(), delete_peak(), detect_lines(), detect_ripples(), extract_ff_rebin_merge(), extract_order_simple(), 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(), 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(), uves_align(), uves_baryvel(), uves_calculate_response(), uves_ccd_is_new(), 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_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_flat_create_normalized_master(), uves_flat_create_normalized_master2(), uves_flatfielding(), uves_format_is_new(), 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_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_bscale(), 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_cunit1(), uves_pfits_get_cunit2(), 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_tel_alt_start(), 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_wlen1(), uves_pfits_get_wstart(), uves_pfits_set_badpixcorr(), uves_pfits_set_bscale(), uves_pfits_set_bunit(), uves_pfits_set_bunit_no(), uves_pfits_set_cd1(), uves_pfits_set_cd11(), uves_pfits_set_cd12(), uves_pfits_set_cd21(), uves_pfits_set_cd22(), 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_cunit1(), uves_pfits_set_cunit2(), 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_dpr_catg(), uves_pfits_set_dpr_tech(), uves_pfits_set_dpr_type(), uves_pfits_set_exptime(), uves_pfits_set_extname(), 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_tunit_no(), 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_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 204 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_pfits_get_slitlength_pixels(), 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 219 of file uves_error.h.