function util_add_error_causes(modelH, mExceptionCauseFlat)

%   Copyright 2009-2016 The MathWorks, Inc.

    for idx=1:length(mExceptionCauseFlat)
        
        if strcmp('Simulink:blocks:BusCC_ElementNameMismatchWarning', mExceptionCauseFlat{idx}.identifier)
            [msg,id] = getSLDVBusElementMismatchError(mExceptionCauseFlat{idx});
        elseif Sldv.utils.errMsgHandler.isInvalidParamTuningError(mExceptionCauseFlat{idx})
            [msg,id] = Sldv.utils.errMsgHandler.getInvalidParamTuningError(mExceptionCauseFlat{idx});
        else
            msg = mExceptionCauseFlat{idx}.message;
            id  = mExceptionCauseFlat{idx}.identifier;
        end
        
        if isa(mExceptionCauseFlat{idx},'MSLException') && ...
                ~isempty(mExceptionCauseFlat{idx}.handles) && ...
                ~isempty(mExceptionCauseFlat{idx}.handles{1}) 
            avtcgirunsupcollect('push',filterHandles(mExceptionCauseFlat{idx}.handles{1}),'simulink', ...
                    msg,...
                    id);
        else
            avtcgirunsupcollect('push',modelH,'simulink', ...
                    msg,...
                    id);
        end
    end  
end

function blockH = filterHandles(blockH)
    if length(blockH)>1
        blockH = get_param(bdroot(blockH(1)),'Handle');
    end
end
% LocalWords:  MSL

function [msg,id] = getSLDVBusElementMismatchError(Mex)
    id = 'Sldv:Sldvextract:BusElementNameMismatch';  
    bus_elem_msg = regexp(Mex.message, '\. ', 'split');
    sldv_bus_err_msg = getString(message(id));
    msg = [char(bus_elem_msg(1)) '. ' char(sldv_bus_err_msg)];
end