private async Task HandleResultExceptionAsync(HttpContext context, ApiException resultException) { string path = context.Request.Path; if (string.Equals(resultException.GetErrorCode(), NotFoundApiException.TenantNotFound) && _tenantSelectorFallbackUrl != null && !string.Equals(path, _tenantSelectorFallbackUrl)) { // redirect to the tenant selector context.Response.Redirect(_tenantSelectorFallbackUrl); return; } string redirectUrl = null; if (_webPort == null || context.Connection.LocalPort != _webPort) { // we have a call to some API endpoint, so just return the error JSON if (resultException.Redirect() && (string.IsNullOrEmpty(path) || !path.ToLower().StartsWith("/error"))) { redirectUrl = GetRedirectUrl(resultException); } await resultException.WriteResponseAsync(context, redirectUrl).ConfigureAwait(false); return; } // we have a call to our web interface, we need to go to the error page // but not twice, because then we'd run in circles if (!string.IsNullOrEmpty(path) && path.ToLower().StartsWith("/error")) { // we ARE already on the error page, use critical fallback URL context.Response.Redirect(_criticalFallbackUrl); return; } redirectUrl = GetRedirectUrl(resultException); context.Response.Redirect(redirectUrl); }