1

Not sure the bext approach to display exceptions like db exceptions to the client (ajax calls)? Should I place a try/catch in the controller bellow? Currently, I get a generic exception to my ajax call?

Controller:

// GET: Agency Grants 
    public JsonResult GetAgencyGrants(int agencyID)
    {
        AuditDAL ad = new AuditDAL();
        var grants = ad.GetAgencyGrants(agencyID); //exception here
        return this.Json(grants.ToList());
    } 

Ajax call:

function GetAuditTypes(container) {
    $.ajax({
        url: '/AMS/Audit/GetAuditTypes',
        type: 'post',
        success: function (result) {
            $.each(result, function (i, item) {
                container.append($('<option/>').text(result[i].Audit_Type_Desc).attr('value', result[i].Audit_Type_ID));
            });
        },
        error: function (xhr, err) {
            alert("GetAuditTypes not returned: " + formatErrorMessage(xhr, err)); 
        }
    });
}

formatting function:

// formats errors returned
function formatErrorMessage(jqXHR, exception) {

    if (jqXHR.status === 0) {
        return ('Not connected.\nPlease verify your network connection.');
    } else if (jqXHR.status == 404) {
        return ('The requested page not found. [404]');
    } else if (jqXHR.status == 500) {
        return ('Internal Server Error [500].');
    } else if (exception === 'parsererror') {
        return ('Requested JSON parse failed.');
    } else if (exception === 'timeout') {
        return ('Time out error.');
    } else if (exception === 'abort') {
        return ('Ajax request aborted.');
    } else {
        return ('Uncaught Error.\n' + jqXHR.responseText);
    }
}

I keep getting Internal Server error instead of the stored proc is missing (the real exception).

4

1 に答える 1

0

ajaxerrorコールバック関数はhttpエラーを処理します (特定のサーバー エラーではありません)。したがって、特殊なエラー メッセージは表示されません。コードを次のように変更します。

  public JsonResult GetAgencyGrants(int agencyID)
    {
        AuditDAL ad = new AuditDAL();
        try
        {
           var grants = ad.GetAgencyGrants(agencyID); //exception here
           return this.Json(grants.ToList());
        }
        catch(Exception e){
            return this.Json(e.Message); 
        }             
    } 

JavaScriptコードを次のように変更します。

$.ajax({
        url: '/AMS/Audit/GetAuditTypes',
        type: 'post',
        success: function (result) {
            if (result instanceof Array)
            {
            $.each(result, function (i, item) {
                container.append($('<option/>').text(result[i].Audit_Type_Desc).attr('value', result[i].Audit_Type_ID));
            });
             }
             else{
                // Exception Occurred 
                // do somethings with result(contains error message)
             }
        }           
    });
于 2013-06-21T14:45:53.433 に答える