本文整理汇总了C#中System.Web.Http.ExceptionHandling.ExceptionHandlerContext类的典型用法代码示例。如果您正苦于以下问题:C# ExceptionHandlerContext类的具体用法?C# ExceptionHandlerContext怎么用?C# ExceptionHandlerContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ExceptionHandlerContext类属于System.Web.Http.ExceptionHandling命名空间,在下文中一共展示了ExceptionHandlerContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HandleAsyncCore
private static async Task<HttpResponseMessage> HandleAsyncCore(IExceptionHandler handler,
ExceptionHandlerContext context, CancellationToken cancellationToken)
{
Contract.Assert(handler != null);
Contract.Assert(context != null);
await handler.HandleAsync(context, cancellationToken);
IHttpActionResult result = context.Result;
if (result == null)
{
return null;
}
HttpResponseMessage response = await result.ExecuteAsync(cancellationToken);
if (response == null)
{
throw new InvalidOperationException(Error.Format(SRResources.TypeMethodMustNotReturnNull,
typeof(IHttpActionResult).Name, "ExecuteAsync"));
}
return response;
}
示例2: Handle
// Create the error info object to be returned to the requestor
public override void Handle(ExceptionHandlerContext context)
{
context.Result =
new ResponseMessageResult
(context.Request.CreateResponse(HttpStatusCode.InternalServerError,
new ErrorInfo { Message = context.Exception.Message, Timestamp = DateTime.Now }));
}
示例3: ShouldHandle
public virtual bool ShouldHandle(ExceptionHandlerContext context)
{
//WebAPI v2 does not use IsOutermostCatchBlock anymore
//IsOutermostCatchBlock does not exists. Use CatchBlock.IsTopLevel instead:
//http://stackoverflow.com/a/22357634/1616023
return context.ExceptionContext.CatchBlock.IsTopLevel;
}
示例4: Handle
private static void Handle(ExceptionHandlerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
ExceptionContext exceptionContext = context.ExceptionContext;
Contract.Assert(exceptionContext != null);
Exception exception = exceptionContext.Exception;
HttpRequestMessage request = exceptionContext.Request;
if (request == null)
{
throw new ArgumentException(Error.Format(SRResources.TypePropertyMustNotBeNull,
typeof(ExceptionContext).Name, "Request"), "context");
}
if (exceptionContext.CatchBlock == ExceptionCatchBlocks.IExceptionFilter)
{
// The exception filter stage propagates unhandled exceptions by default (when no filter handles the
// exception).
return;
}
context.Result = new ResponseMessageResult(request.CreateErrorResponse(HttpStatusCode.InternalServerError,
exception));
}
示例5: Handle
public override void Handle(ExceptionHandlerContext context)
{
bool shouldHandle = this.ShouldHandle(context);
if (!shouldHandle)
{
return;
}
ErrorContext errorContext = null;
try
{
errorContext = ErrorContextService.Resolve(context);
var model = new ErrorViewModel(errorContext, context.RequestContext.RouteData.Values);
var jsonModel = model.ToJson();
var response = context.Request.CreateResponse(errorContext.HttpStatusCode, jsonModel);
context.Result = new ResponseMessageResult(response);
}
finally
{
ErrorContextService.Log(CurrentLogger, errorContext);
}
}
示例6: HandleAsync
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
{
var info = ExceptionDispatchInfo.Capture(context.Exception);
info.Throw();
return Task.FromResult(0);
}
示例7: Handle
public override void Handle(ExceptionHandlerContext context)
{
if (context.Exception is Code.ExceptionResponse)
{
Code.ExceptionResponse ex = context.Exception as Code.ExceptionResponse;
context.Result = new TextPlainErrorResult
{
Request = context.ExceptionContext.Request,
Content = Transfer.Response.Error(ex.Message)
};
}
else if (context.Exception is Code.ExceptionMaintenance)
{
context.Result = new TextPlainErrorResult
{
Content = Transfer.Response.Maintenance("Server is down for maintenance")
};
}
else
{
context.Result = new TextPlainErrorResult
{
Request = context.ExceptionContext.Request,
Content = Transfer.Response.Error(context.Exception.Message)
};
}
}
示例8: Handle
public override void Handle(ExceptionHandlerContext context)
{
HttpRequestMessage request = context.Request;
context.Result = new ResponseMessageResult(request.CreateResponse(HttpStatusCode.InternalServerError, new ErrorMsg(context.Exception.Message)));
}
示例9: HandleAsync
public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
{
// For exceptions at the top of the call stack, Result will start out non-null (due to
// LastChanceExceptionHandler). This class does not force exceptions back to unhandled in such cases, so it
// will not not trigger the host-level exception processing, such as the ASP.NET yellow screen.
return TaskHelpers.Completed();
}
示例10: HandleValidationException
private void HandleValidationException(ExceptionHandlerContext context)
{
context.Result = new GlobalExceptionResponse(context.ExceptionContext.Request, HttpStatusCode.BadRequest)
{
Message = context.Exception.Message
};
}
示例11: Handle
public override void Handle(ExceptionHandlerContext context)
{
var exception = context.Exception;
var httpException = exception as HttpException;
if (httpException != null)
{
context.Result = new SimpleErrorResult(context.Request, (HttpStatusCode)httpException.GetHttpCode(),
httpException.Message);
return;
}
if (exception is RootObjectNotFoundException)
{
context.Result = new SimpleErrorResult(context.Request, HttpStatusCode.NotFound,
exception.Message);
return;
}
if (exception is ChildObjectNotFoundException)
{
context.Result = new SimpleErrorResult(context.Request, HttpStatusCode.Conflict,
exception.Message);
return;
}
context.Result = new SimpleErrorResult(context.Request, HttpStatusCode.InternalServerError, exception.Message);
}
示例12: Handle
public override void Handle(ExceptionHandlerContext context)
{
// get base exception
var baseExp = context.Exception.GetBaseException();
// set the result
context.Result = new InternalServerErrorPlainTextResult(baseExp.Message, context.Request);
}
示例13: Handle
/// <summary>
/// 在衍生類別中覆寫時,同步處理例外狀況。
/// </summary>
/// <param name="context">例外狀況處理常式內容。</param>
public override void Handle(ExceptionHandlerContext context)
{
IError error = ErrorFactory.GetExceptionType(context);
context.Result = new ResponseMessageResult(error.GenerateExceptionMessage(context));
base.Handle(context);
}
示例14: HandleAsync
public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
{
var jsonResult = "Error Global";
context.Result = new ResponseMessageResult(new HttpResponseMessage { StatusCode = HttpStatusCode.InternalServerError, Content = new StringContent(jsonResult) });
return Task.FromResult(0);
}
示例15: Handle
/// <summary>
/// Format the result of the error context to be send back.
/// </summary>
/// <param name="context"></param>
public override void Handle(ExceptionHandlerContext context)
{
context.Result = new TextPlainErrorResult
{
Request = context.ExceptionContext.Request,
Content = $"We apologize but an unexpected error occurred. Please try again later. {context.Exception.Message}"
};
}