Error handling

1. Introduction

Almost every function of the library returns a qmckl_exit_code: QMCKL_SUCCESS, which is zero, when the call succeeded, and a non-zero code describing the error otherwise. The exceptions are the functions that return a value or nothing: the context functions, the lock, the provided predicates, the numerical-precision getters, the two qmckl_test_precision helpers and qmckl_string_of_error. An error is reported through the return value alone, and the calling code decides what it means for it.

A return code alone does not say which call failed on which argument. For that the library records, inside the context, the code, the name of the function and a message. This module defines the exit codes and the functions that write, read and decode that record.

The library does not print errors. Assertions catch a misuse in a build that keeps them enabled, and qmckl_check, at the end of this page, is the only helper that writes to stderr.

2. Exit codes

An exit code is a 32-bit integer:

C
typedef int32_t qmckl_exit_code;

The codes fall in two blocks. Codes 1 to 20 are QMCKL_INVALID_ARG_1 to QMCKL_INVALID_ARG_20 and name the offending argument by its position in the signature, the context being the first, so a function that rejects its third argument returns QMCKL_INVALID_ARG_3. Codes 101 to 109 describe the conditions that are not an argument: a failed allocation, a value that was never provided, a set attempted twice. Four of them are defined and decoded but never returned by the library at present, QMCKL_ERRNO, QMCKL_DEALLOCATION_FAILED, QMCKL_OUT_OF_BOUNDS and QMCKL_INVALID_EXIT_CODE.

Here is the complete list of exit codes:

Macro Code Description
QMCKL_SUCCESS 0 'Success'
QMCKL_INVALID_ARG_1 1 'Invalid argument 1'
QMCKL_INVALID_ARG_2 2 'Invalid argument 2'
QMCKL_INVALID_ARG_3 3 'Invalid argument 3'
QMCKL_INVALID_ARG_4 4 'Invalid argument 4'
QMCKL_INVALID_ARG_5 5 'Invalid argument 5'
QMCKL_INVALID_ARG_6 6 'Invalid argument 6'
QMCKL_INVALID_ARG_7 7 'Invalid argument 7'
QMCKL_INVALID_ARG_8 8 'Invalid argument 8'
QMCKL_INVALID_ARG_9 9 'Invalid argument 9'
QMCKL_INVALID_ARG_10 10 'Invalid argument 10'
QMCKL_INVALID_ARG_11 11 'Invalid argument 11'
QMCKL_INVALID_ARG_12 12 'Invalid argument 12'
QMCKL_INVALID_ARG_13 13 'Invalid argument 13'
QMCKL_INVALID_ARG_14 14 'Invalid argument 14'
QMCKL_INVALID_ARG_15 15 'Invalid argument 15'
QMCKL_INVALID_ARG_16 16 'Invalid argument 16'
QMCKL_INVALID_ARG_17 17 'Invalid argument 17'
QMCKL_INVALID_ARG_18 18 'Invalid argument 18'
QMCKL_INVALID_ARG_19 19 'Invalid argument 19'
QMCKL_INVALID_ARG_20 20 'Invalid argument 20'
QMCKL_FAILURE 101 'Failure'
QMCKL_ERRNO 102 strerror(errno)
QMCKL_INVALID_CONTEXT 103 'Invalid context'
QMCKL_ALLOCATION_FAILED 104 'Allocation failed'
QMCKL_DEALLOCATION_FAILED 105 'De-allocation failed'
QMCKL_NOT_PROVIDED 106 'Not provided'
QMCKL_OUT_OF_BOUNDS 107 'Index out of bounds'
QMCKL_ALREADY_SET 108 'Already set'
QMCKL_INVALID_EXIT_CODE 109 'Invalid exit code'

3. Decoding errors

qmckl_string_of_error turns an exit code into a string that an application can print or log. The switch covers every code of the table and returns "Unknown error" for anything else, so the result is never NULL. Only QMCKL_ERRNO gives something else: strerror(errno), the description of the last failing system call, in a buffer that the next strerror call may overwrite. The Fortran interface binds to a private wrapper, qmckl_string_of_error_f, which copies at most 127 characters into the 128-byte buffer it is given and terminates the string.

C
const char*
qmckl_string_of_error (const qmckl_exit_code error);

The strings are generated from the table above by the Python block that also produces the C and Fortran constants, so a code and its text cannot drift apart.

4. Data structure in context

The context holds one qmckl_error_struct, with the code, a QMCKL_MAX_FUN_LEN-byte function name and a QMCKL_MAX_MSG_LEN-byte message. The two strings have a fixed size, so recording an error allocates nothing and cannot fail for lack of memory.

A new context is zeroed by qmckl_context_create: it starts with QMCKL_SUCCESS and two empty strings. qmckl_context_copy copies the structure with the rest of the context, and nothing clears it afterwards, so a successful call leaves the last error in place.

5. Updating errors in the context

qmckl_set_error overwrites the record with a code, a function name and a message, under the context lock. All three are mandatory and the code cannot be QMCKL_SUCCESS. The check that the context is valid is an assertion, not a return value, because a record that has nowhere to go cannot report anything. The function returns QMCKL_SUCCESS, which says that the recording succeeded, not the code it stored. A name longer than 255 characters and a message longer than 1023 are truncated.

C
qmckl_exit_code
qmckl_set_error(qmckl_context context,
                const qmckl_exit_code exit_code,
                const char* function_name,
                const char* message);

6. Get the error

qmckl_get_error reads the record back into three caller-provided variables. It has no size arguments: the two buffers must hold QMCKL_MAX_FUN_LEN and QMCKL_MAX_MSG_LEN bytes, and neither may be NULL. The code is written to exit_code, which is QMCKL_SUCCESS when nothing has failed since the context was created. Like the setter, it asserts that the context is valid and returns QMCKL_SUCCESS.

C
qmckl_exit_code
qmckl_get_error(qmckl_context context,
                qmckl_exit_code *exit_code,
                char* function_name,
                char* message);

7. Failing

qmckl_failwith records the failure and returns the code it was given, so that a kernel can write return qmckl_failwith(...). The code, the function name and a message are the arguments; a NULL message is replaced by the text qmckl_string_of_error gives for that code. It asserts that the name and the message fit in 255 and 1023 characters. When the context is not valid, nothing can be recorded and the function returns QMCKL_INVALID_CONTEXT.

C
qmckl_exit_code
qmckl_failwith(qmckl_context context,
               const qmckl_exit_code exit_code,
               const char* function,
               const char* message) ;

For example, this function can be used as

8. Last error

qmckl_last_error formats the record into a caller-provided buffer of size_max bytes: Error -- <code> -- in <function> on one line, the message on the next. The text is truncated to fit and is always terminated. A NULL buffer and a non-positive size return QMCKL_INVALID_ARG_2 and QMCKL_INVALID_ARG_3. With an invalid context the function cannot read the record, writes "Null context" and returns QMCKL_FAILURE.

C
qmckl_exit_code
qmckl_last_error(qmckl_context context,
                 char* buffer,
                 const int64_t size_max);

9. Helper functions for debugging

qmckl_check prints to stderr the code it was given and the record of the context when that code is not QMCKL_SUCCESS. What it returns is the code it reads back from the context, not its argument: a matching context gives the same value, an empty one gives QMCKL_SUCCESS, and the assertion that usually follows would then pass. The context must be valid; qmckl_get_error asserts it.

C
qmckl_exit_code
qmckl_check(qmckl_context context, qmckl_exit_code rc);

It should be used as:

C
rc = qmckl_check(context,
                 qmckl_...(context, ...)
                 );
assert (rc == QMCKL_SUCCESS);