better error handling for C API