private static Task <bool> HandleCommonException( ExceptionContext context, CancellationToken cancellationToken) { var exception = context.Exception.Demystify(); if (exception is AggregateException) { // In async call, the exception will be wrapped as AggregateException exception = exception.InnerException.Demystify(); } if (exception == null) { return(Task.FromResult(false)); } HttpStatusCode code; switch (true) { case true when exception is StatusCodeException: code = (exception as StatusCodeException).StatusCode; context.Result = new StatusCodeResult((int)code); break; case true when exception is ODataException: code = HttpStatusCode.BadRequest; var response = EnableQueryAttribute.CreateErrorResponse(exception.Message, exception); context.Result = new BadRequestObjectResult(response); break; case true when exception is SecurityException: code = HttpStatusCode.Forbidden; context.Result = new ForbidResult(); break; case true when exception is NotImplementedException: code = HttpStatusCode.NotImplemented; context.Result = new StatusCodeResult((int)code); break; default: code = HttpStatusCode.InternalServerError; context.Result = new StatusCodeResult((int)code); break; } // When exception occured in a ChangeSet request, // exception must be handled in OnChangeSetCompleted // to avoid deadlock in Github Issue #82. var changeSetProperty = context.HttpContext.GetChangeSet(); if (changeSetProperty != null) { changeSetProperty.Exceptions.Add(exception); changeSetProperty.OnChangeSetCompleted(); } if (code != HttpStatusCode.Unused) { return(Task.FromResult(true)); } return(Task.FromResult(false)); }