httphandler(.ashx)に次のメソッドがあると仮定します。
private void Foo()
{
try
{
throw new Exception("blah");
}
catch(Exception e)
{
HttpContext.Current.Response.Write(
serializer.Serialize(new AjaxError(e)));
}
}
[Serializable]
public class AjaxError
{
public string Message { get; set; }
public string InnerException { get; set; }
public string StackTrace { get; set; }
public AjaxError(Exception e)
{
if (e != null)
{
this.Message = e.Message;
this.StackTrace = e.StackTrace;
this.InnerException = e.InnerException != null ?
e.InnerException.Message : null;
HttpContext.Current.Response.StatusDescription = "CustomError";
}
}
}
メソッドを$.ajax()
呼び出すと、success
バックエンドで問題が発生したかどうかに関係なく、コールバックになり、catch
ブロックになります。
エラー処理を正規化するためにajaxメソッドを少し拡張したので、「jquery」エラー(解析エラーなど)またはカスタムエラーに関係なく、エラーコールバックになります。
さて、私が知りたいのは、次のようなものを追加する必要があるということです
HttpContext.Current.Response.StatusCode = 500;
jQuerysエラーハンドラで終わるか、または私が処理する必要があります
HttpContext.Current.Response.StatusDescription = "CustomError";
jqXHRオブジェクトで、そこにエラーがあると想定しますか?
不明な点がありましたらお知らせください。