mirror of https://github.com/abpframework/abp.git
8 changed files with 139 additions and 112 deletions
@ -0,0 +1,14 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared |
||||
|
{ |
||||
|
public class AbpErrorPageOptions |
||||
|
{ |
||||
|
public readonly IDictionary<string, string> ErrorPageUrls; |
||||
|
|
||||
|
public AbpErrorPageOptions() |
||||
|
{ |
||||
|
ErrorPageUrls = new Dictionary<string, string>(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,64 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using Microsoft.AspNetCore.Diagnostics; |
||||
|
using Microsoft.AspNetCore.Mvc; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.AspNetCore.ExceptionHandling; |
||||
|
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Pages.Error; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Controllers |
||||
|
{ |
||||
|
public class ErrorController : AbpController |
||||
|
{ |
||||
|
private readonly IExceptionToErrorInfoConverter _errorInfoConverter; |
||||
|
private readonly IHttpExceptionStatusCodeFinder _statusCodeFinder; |
||||
|
private readonly AbpErrorPageOptions _abpErrorPageOptions; |
||||
|
|
||||
|
public ErrorController( |
||||
|
IExceptionToErrorInfoConverter exceptionToErrorInfoConverter, |
||||
|
IHttpExceptionStatusCodeFinder httpExceptionStatusCodeFinder, IOptions<AbpErrorPageOptions> abpErrorPageOptions) |
||||
|
{ |
||||
|
_errorInfoConverter = exceptionToErrorInfoConverter; |
||||
|
_statusCodeFinder = httpExceptionStatusCodeFinder; |
||||
|
_abpErrorPageOptions = abpErrorPageOptions.Value; |
||||
|
} |
||||
|
|
||||
|
public IActionResult Index(int httpStatusCode) |
||||
|
{ |
||||
|
var exHandlerFeature = HttpContext.Features.Get<IExceptionHandlerFeature>(); |
||||
|
|
||||
|
var exception = exHandlerFeature != null |
||||
|
? exHandlerFeature.Error |
||||
|
: new Exception("Unhandled exception!"); //TODO: Localize?
|
||||
|
|
||||
|
var errorInfo = _errorInfoConverter.Convert(exception); |
||||
|
|
||||
|
if (httpStatusCode == 0) |
||||
|
{ |
||||
|
httpStatusCode = (int)_statusCodeFinder.GetStatusCode(HttpContext, exception); |
||||
|
} |
||||
|
|
||||
|
HttpContext.Response.StatusCode = httpStatusCode; |
||||
|
|
||||
|
var page = GetErrorPageUrl(httpStatusCode); |
||||
|
|
||||
|
return View(page, new AbpErrorPageModel |
||||
|
{ |
||||
|
ErrorInfo = errorInfo, |
||||
|
HttpStatusCode = httpStatusCode |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
private string GetErrorPageUrl(int statusCode) |
||||
|
{ |
||||
|
var page = _abpErrorPageOptions.ErrorPageUrls.GetOrDefault(statusCode.ToString()); |
||||
|
|
||||
|
if (string.IsNullOrWhiteSpace(page)) |
||||
|
{ |
||||
|
return "~/Pages/Error/Default.cshtml"; |
||||
|
} |
||||
|
|
||||
|
return page; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
using Volo.Abp.Http; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Pages.Error |
||||
|
{ |
||||
|
public class AbpErrorPageModel |
||||
|
{ |
||||
|
public RemoteServiceErrorInfo ErrorInfo { get; set; } |
||||
|
|
||||
|
public int HttpStatusCode { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,40 @@ |
|||||
|
@using System.Linq |
||||
|
@using System.Collections.Generic |
||||
|
@using Localization.Resources.AbpUi |
||||
|
@using Microsoft.AspNetCore.Mvc.Localization |
||||
|
@using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Pages.Error |
||||
|
@model AbpErrorPageModel |
||||
|
@inject IHtmlLocalizer<AbpUiResource> L |
||||
|
@{ |
||||
|
var errorMessage = Model.ErrorInfo.Message; |
||||
|
var errorDetails = Model.ErrorInfo.Details; |
||||
|
if (errorDetails.IsNullOrEmpty()) |
||||
|
{ |
||||
|
errorDetails = errorMessage; |
||||
|
errorMessage = L["Error"].Value + "!"; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
<h1> |
||||
|
[@Model.HttpStatusCode] @errorMessage |
||||
|
</h1> |
||||
|
|
||||
|
<div> |
||||
|
<p> |
||||
|
@errorDetails |
||||
|
</p> |
||||
|
<p> |
||||
|
@if (!Model.ErrorInfo.ValidationErrors.IsNullOrEmpty()) |
||||
|
{ |
||||
|
foreach (var validationError in Model.ErrorInfo.ValidationErrors) |
||||
|
{ |
||||
|
<text>* </text>@(validationError.Message) |
||||
|
if (validationError.Members != null && validationError.Members.Any()) |
||||
|
{ |
||||
|
<text>(@string.Join(", ", validationError.Members))</text> |
||||
|
} |
||||
|
<br /> |
||||
|
} |
||||
|
} |
||||
|
</p> |
||||
|
</div> |
||||
@ -1,41 +0,0 @@ |
|||||
@page |
|
||||
@using System.Linq |
|
||||
@using System.Collections.Generic |
|
||||
@using Localization.Resources.AbpUi |
|
||||
@using Microsoft.AspNetCore.Mvc.Localization |
|
||||
@using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Pages.Error |
|
||||
@model IndexModel |
|
||||
@inject IHtmlLocalizer<AbpUiResource> L |
|
||||
@{ |
|
||||
var errorMessage = Model.ErrorInfo.Message; |
|
||||
var errorDetails = Model.ErrorInfo.Details; |
|
||||
if (errorDetails.IsNullOrEmpty()) |
|
||||
{ |
|
||||
errorDetails = errorMessage; |
|
||||
errorMessage = L["Error"].Value + "!"; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
<h1> |
|
||||
[@Model.HttpStatusCode] @errorMessage |
|
||||
</h1> |
|
||||
|
|
||||
<div> |
|
||||
<p> |
|
||||
@errorDetails |
|
||||
</p> |
|
||||
<p> |
|
||||
@if (!Model.ErrorInfo.ValidationErrors.IsNullOrEmpty()) |
|
||||
{ |
|
||||
foreach (var validationError in Model.ErrorInfo.ValidationErrors) |
|
||||
{ |
|
||||
<text>* </text>@(validationError.Message) |
|
||||
if (validationError.Members != null && validationError.Members.Any()) |
|
||||
{ |
|
||||
<text>(@string.Join(", ", validationError.Members))</text> |
|
||||
} |
|
||||
<br /> |
|
||||
} |
|
||||
} |
|
||||
</p> |
|
||||
</div> |
|
||||
@ -1,69 +0,0 @@ |
|||||
using System; |
|
||||
using Microsoft.AspNetCore.Diagnostics; |
|
||||
using Microsoft.AspNetCore.Mvc; |
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages; |
|
||||
using Volo.Abp.AspNetCore.ExceptionHandling; |
|
||||
using Volo.Abp.Http; |
|
||||
|
|
||||
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Pages.Error |
|
||||
{ |
|
||||
public class IndexModel : PageModel |
|
||||
{ |
|
||||
public RemoteServiceErrorInfo ErrorInfo { get; set; } |
|
||||
|
|
||||
[BindProperty(SupportsGet = true)] |
|
||||
public int HttpStatusCode { get; set; } |
|
||||
|
|
||||
private readonly IExceptionToErrorInfoConverter _errorInfoConverter; |
|
||||
private readonly IHttpExceptionStatusCodeFinder _statusCodeFinder; |
|
||||
|
|
||||
public IndexModel(IExceptionToErrorInfoConverter errorInfoConverter, IHttpExceptionStatusCodeFinder statusCodeFinder) |
|
||||
{ |
|
||||
_errorInfoConverter = errorInfoConverter; |
|
||||
_statusCodeFinder = statusCodeFinder; |
|
||||
} |
|
||||
|
|
||||
public void OnGet() |
|
||||
{ |
|
||||
HandleError(); |
|
||||
} |
|
||||
|
|
||||
public void OnPost() |
|
||||
{ |
|
||||
HandleError(); |
|
||||
} |
|
||||
|
|
||||
public void OnPut() |
|
||||
{ |
|
||||
HandleError(); |
|
||||
} |
|
||||
|
|
||||
public void OnDelete() |
|
||||
{ |
|
||||
HandleError(); |
|
||||
} |
|
||||
|
|
||||
public void OnPatch() |
|
||||
{ |
|
||||
HandleError(); |
|
||||
} |
|
||||
|
|
||||
private void HandleError() |
|
||||
{ |
|
||||
var exHandlerFeature = HttpContext.Features.Get<IExceptionHandlerFeature>(); |
|
||||
|
|
||||
var exception = exHandlerFeature != null |
|
||||
? exHandlerFeature.Error |
|
||||
: new Exception("Unhandled exception!"); //TODO: Localize?
|
|
||||
|
|
||||
ErrorInfo = _errorInfoConverter.Convert(exception); |
|
||||
|
|
||||
if (HttpStatusCode == 0) |
|
||||
{ |
|
||||
HttpStatusCode = (int)_statusCodeFinder.GetStatusCode(HttpContext, exception); |
|
||||
} |
|
||||
|
|
||||
HttpContext.Response.StatusCode = HttpStatusCode; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue