void copy_error_to_zval()

in source/sqlsrv/util.cpp [785:849]


void copy_error_to_zval( _Inout_ zval* error_z, _In_ sqlsrv_error_const* error, _Inout_ zval* reported_chain, _Inout_ zval* ignored_chain, 
                         _In_ bool warning )
{
    array_init(error_z);

    // sqlstate
    zval temp; 
    ZVAL_UNDEF(&temp);
    core::sqlsrv_zval_stringl( &temp, reinterpret_cast<char*>( error->sqlstate ), SQL_SQLSTATE_SIZE );
    Z_TRY_ADDREF_P( &temp );
    if( add_next_index_zval( error_z, &temp ) == FAILURE ) {
        DIE( "Fatal error during error processing" );
    }

    add_assoc_zval(error_z, "SQLSTATE", &temp);

    // native_code
    if( add_next_index_long( error_z,  error->native_code ) == FAILURE ) {
        DIE( "Fatal error during error processing" );
    }

    add_assoc_long(error_z, "code", error->native_code);

    // native_message
    ZVAL_UNDEF(&temp);
    ZVAL_STRING( &temp, reinterpret_cast<char*>( error->native_message ) );
    Z_TRY_ADDREF_P(&temp);
    if( add_next_index_zval( error_z, &temp ) == FAILURE ) {
        DIE( "Fatal error during error processing" );
    }

    add_assoc_zval(error_z, "message", &temp);

    // If it is an error or if warning_return_as_errors is true than
    // add the error or warning to the reported_chain.
    if( !warning || SQLSRV_G( warnings_return_as_errors ) )
    {
        // if the warning is part of the ignored warning list than 
        // add to the ignored chain if the ignored chain is not null.
        if( warning && ignore_warning( reinterpret_cast<char*>(error->sqlstate), error->native_code ) &&
            ignored_chain != NULL ) {
            
            if( add_next_index_zval( ignored_chain, error_z ) == FAILURE ) {
                DIE( "Fatal error during error processing" );
            }          
        }
        else {

            // It is either an error or a warning which should not be ignored. 
            if( add_next_index_zval( reported_chain, error_z ) == FAILURE ) {
                DIE( "Fatal error during error processing" );
            }          
        }
    }
    else
    {
        // It is a warning with warning_return_as_errors as false, so simply add it to the ignored_chain list
        if( ignored_chain != NULL ) {
         
            if( add_next_index_zval( ignored_chain, error_z ) == FAILURE ) {
                DIE( "Fatal error during error processing" );
            }          
        }
    }
}