そのため、最近、Api エラーを除くすべてのエラーを処理するExceptionFilterを構築しました。ExceptionFilterは次のようになります。
public class ExceptionAttribute : IExceptionFilter
{
/// <summary>
/// Handles any exception
/// </summary>
/// <param name="filterContext">The current context</param>
public void OnException(ExceptionContext filterContext)
{
// If our exception has been handled, exit the function
if (filterContext.ExceptionHandled)
return;
// If our exception is not an ApiException
if (!(filterContext.Exception is ApiException))
{
// Set our base status code
var statusCode = (int)HttpStatusCode.InternalServerError;
// If our exception is an http exception
if (filterContext.Exception is HttpException)
{
// Cast our exception as an HttpException
var exception = (HttpException)filterContext.Exception;
// Get our real status code
statusCode = exception.GetHttpCode();
}
// Set our context result
var result = CreateActionResult(filterContext, statusCode);
// Set our handled property to true
filterContext.ExceptionHandled = true;
}
}
/// <summary>
/// Creats an action result from the status code
/// </summary>
/// <param name="filterContext">The current context</param>
/// <param name="statusCode">The status code of the error</param>
/// <returns></returns>
protected virtual ActionResult CreateActionResult(ExceptionContext filterContext, int statusCode)
{
// Create our context
var context = new ControllerContext(filterContext.RequestContext, filterContext.Controller);
var statusCodeName = ((HttpStatusCode)statusCode).ToString();
// Create our route
var controller = (string)filterContext.RouteData.Values["controller"];
var action = (string)filterContext.RouteData.Values["action"];
var model = new HandleErrorInfo(filterContext.Exception, controller, action);
// Create our result
var view = SelectFirstView(context, string.Format("~/Views/Error/{0}.cshtml", statusCodeName), "~/Views/Error/Index.cshtml", statusCodeName);
var result = new ViewResult { ViewName = view, ViewData = new ViewDataDictionary<HandleErrorInfo>(model) };
// Return our result
return result;
}
/// <summary>
/// Gets the first view name that matches the supplied names
/// </summary>
/// <param name="context">The current context</param>
/// <param name="viewNames">A list of view names</param>
/// <returns></returns>
protected string SelectFirstView(ControllerContext context, params string[] viewNames)
{
return viewNames.First(view => ViewExists(context, view));
}
/// <summary>
/// Checks to see if a view exists
/// </summary>
/// <param name="context">The current context</param>
/// <param name="name">The name of the view to check</param>
/// <returns></returns>
protected bool ViewExists(ControllerContext context, string name)
{
var result = ViewEngines.Engines.FindView(context, name, null);
return result.View != null;
}
}
ご覧のとおり、エラーがApiExceptionでない場合は、ユーザーをエラー コントローラーにルーティングします。ApiExceptionは、MVC 内から API 呼び出しを行うときに発生する単なるエラーです。これらのエラーが発生した場合、JavaScript が処理できるように、JSON としてエラーをクライアントに返したいと思います。
エラーを処理しないとこれが実行されると思いましたが、代わりにサーバーエラーが生成されます (ただし、JSON エラーが含まれています)。
「/」アプリケーションでサーバー エラーが発生しました。
{"メッセージ":"validateMove 検証エラー:\r\nアイテムは発送され、移動できません"}
説明: 現在の Web 要求の実行中に未処理の例外が発生しました。エラーの詳細とコード内のどこでエラーが発生したかについては、スタック トレースを確認してください。
例外の詳細: SapphirePlus.Web.ApiException: {"メッセージ":"validateMove 検証エラー:\r\nアイテムは発送され、移動できません"}
ソース エラー:
181行目: if (response.StatusCode != HttpStatusCode.OK)
182 行目: throw new ApiException(result);
私の質問は、Application_Errorメソッドを取得して ApiException であるエラーを取得し、 エラーを JSON として返すことはできますか?