示例#1
0
        private async Task HandleResultExceptionAsync(HttpContext context, ApiException resultException)
        {
            if (_webPort == null || context.Connection.LocalPort != _webPort)
            {
                // we have a call to some endpoint outside of our web interface, so just return the error JSON

                await resultException.WriteResponseAsync(context).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

            string path = context.Request.Path;

            if (!string.IsNullOrEmpty(path) && path.ToLower().StartsWith("/error"))
            {
                // we ARE already on the error page, use critical fallback URL

                context.Response.Redirect(_criticalFallbackUrl);

                return;
            }

            string errorCode        = resultException.GetErrorCode();
            string errorDescription = resultException.Message;

            if (_translationsProvider != null)
            {
                errorDescription = _translationsProvider.TranslateError(resultException.GetErrorCode(), resultException.Message, resultException.Uuid, resultException.Name);
            }

            context.Response.Redirect($"/Error?errorCode={HttpUtility.UrlEncode(errorCode ?? "")}&errorDescription={HttpUtility.UrlEncode(errorDescription ?? "")}");
        }
        private string GetRedirectUrl(ApiException resultException)
        {
            string errorCode        = resultException.GetErrorCode();
            string errorDescription = resultException.Message;

            if (_translationsProvider != null)
            {
                errorDescription = _translationsProvider.TranslateError(resultException.GetErrorCode(), resultException.Message, resultException.Uuid, resultException.Name);
            }

            if (!string.IsNullOrEmpty(errorDescription))
            {
                errorDescription = _dataProtectionProvider.CreateProtector("Error").Protect(errorDescription);
            }

            return($"/Error?errorCode={HttpUtility.UrlEncode(errorCode ?? "")}&errorDescription={HttpUtility.UrlEncode(errorDescription ?? "")}");
        }
        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);
        }