Remove IErrorDescriptionFactory
This commit is contained in:
parent
d6ba2ee966
commit
9b4b373014
|
|
@ -214,9 +214,6 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
services.TryAddTransient<DisableRequestSizeLimitFilter>();
|
services.TryAddTransient<DisableRequestSizeLimitFilter>();
|
||||||
services.TryAddTransient<RequestFormLimitsFilter>();
|
services.TryAddTransient<RequestFormLimitsFilter>();
|
||||||
|
|
||||||
// Error description
|
|
||||||
services.TryAddSingleton<IErrorDescriptionFactory, DefaultErrorDescriptorFactory>();
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// ModelBinding, Validation
|
// ModelBinding, Validation
|
||||||
//
|
//
|
||||||
|
|
|
||||||
|
|
@ -1,19 +0,0 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
||||||
|
|
||||||
using Microsoft.AspNetCore.Mvc.Abstractions;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Mvc.Infrastructure
|
|
||||||
{
|
|
||||||
public class ErrorDescriptionContext
|
|
||||||
{
|
|
||||||
public ErrorDescriptionContext(ActionDescriptor actionDescriptor)
|
|
||||||
{
|
|
||||||
ActionDescriptor = actionDescriptor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ActionDescriptor ActionDescriptor { get; }
|
|
||||||
|
|
||||||
public object Result { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
||||||
|
|
||||||
using Microsoft.AspNetCore.Mvc.Abstractions;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Mvc.Infrastructure
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Defines a contract for creating or modifying an error response.
|
|
||||||
/// </summary>
|
|
||||||
public interface IErrorDescriptionFactory
|
|
||||||
{
|
|
||||||
object CreateErrorDescription(ActionDescriptor actionDescriptor, object result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Mvc.Infrastructure
|
|
||||||
{
|
|
||||||
public interface IErrorDescriptorProvider
|
|
||||||
{
|
|
||||||
int Order { get; }
|
|
||||||
|
|
||||||
void OnProvidersExecuting(ErrorDescriptionContext context);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -2,19 +2,15 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
|
||||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Mvc.Internal
|
namespace Microsoft.AspNetCore.Mvc.Internal
|
||||||
{
|
{
|
||||||
public class ApiBehaviorOptionsSetup : IConfigureOptions<ApiBehaviorOptions>
|
public class ApiBehaviorOptionsSetup : IConfigureOptions<ApiBehaviorOptions>
|
||||||
{
|
{
|
||||||
private readonly IErrorDescriptionFactory _errorDescriptionFactory;
|
|
||||||
|
|
||||||
public ApiBehaviorOptionsSetup(IErrorDescriptionFactory errorDescriptionFactory)
|
public ApiBehaviorOptionsSetup()
|
||||||
{
|
{
|
||||||
_errorDescriptionFactory = errorDescriptionFactory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Configure(ApiBehaviorOptions options)
|
public void Configure(ApiBehaviorOptions options)
|
||||||
|
|
@ -28,13 +24,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
|
||||||
|
|
||||||
IActionResult GetInvalidModelStateResponse(ActionContext context)
|
IActionResult GetInvalidModelStateResponse(ActionContext context)
|
||||||
{
|
{
|
||||||
var errorDetails = _errorDescriptionFactory.CreateErrorDescription(
|
var result = new BadRequestObjectResult(context.ModelState);
|
||||||
context.ActionDescriptor,
|
|
||||||
context.ModelState);
|
|
||||||
|
|
||||||
var result = (errorDetails is ModelStateDictionary modelState) ?
|
|
||||||
new BadRequestObjectResult(modelState) :
|
|
||||||
new BadRequestObjectResult(errorDetails);
|
|
||||||
|
|
||||||
result.ContentTypes.Add("application/json");
|
result.ContentTypes.Add("application/json");
|
||||||
result.ContentTypes.Add("application/xml");
|
result.ContentTypes.Add("application/xml");
|
||||||
|
|
|
||||||
|
|
@ -1,51 +0,0 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using Microsoft.AspNetCore.Mvc.Abstractions;
|
|
||||||
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Mvc.Internal
|
|
||||||
{
|
|
||||||
public class DefaultErrorDescriptorFactory : IErrorDescriptionFactory
|
|
||||||
{
|
|
||||||
private readonly IErrorDescriptorProvider[] _providers;
|
|
||||||
|
|
||||||
public DefaultErrorDescriptorFactory(IEnumerable<IErrorDescriptorProvider> providers)
|
|
||||||
{
|
|
||||||
if (providers == null)
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(providers));
|
|
||||||
}
|
|
||||||
|
|
||||||
_providers = providers.OrderBy(p => p.Order).ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
public object CreateErrorDescription(ActionDescriptor actionDescriptor, object result)
|
|
||||||
{
|
|
||||||
if (actionDescriptor == null)
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(actionDescriptor));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result == null)
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(result));
|
|
||||||
}
|
|
||||||
|
|
||||||
var context = new ErrorDescriptionContext(actionDescriptor)
|
|
||||||
{
|
|
||||||
Result = result,
|
|
||||||
};
|
|
||||||
|
|
||||||
for (var i = 0; i < _providers.Length; i++)
|
|
||||||
{
|
|
||||||
_providers[i].OnProvidersExecuting(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
return context.Result ?? result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -6,7 +6,6 @@ using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using BasicWebSite;
|
|
||||||
using BasicWebSite.Models;
|
using BasicWebSite.Models;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
@ -70,18 +69,10 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
State = "WA",
|
State = "WA",
|
||||||
Zip = "Invalid",
|
Zip = "Invalid",
|
||||||
};
|
};
|
||||||
var expected = new[]
|
var expected = new Dictionary<string, string[]>
|
||||||
{
|
{
|
||||||
new VndError
|
{"Name", new string[] {"The field Name must be a string with a minimum length of 5 and a maximum length of 30."}},
|
||||||
{
|
{"Zip", new string[]{ @"The field Zip must match the regular expression '\d{5}'."}}
|
||||||
Path = "Name",
|
|
||||||
Message = "The field Name must be a string with a minimum length of 5 and a maximum length of 30.",
|
|
||||||
},
|
|
||||||
new VndError
|
|
||||||
{
|
|
||||||
Path = "Zip",
|
|
||||||
Message = @"The field Zip must match the regular expression '\d{5}'.",
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
var contactString = JsonConvert.SerializeObject(contactModel);
|
var contactString = JsonConvert.SerializeObject(contactModel);
|
||||||
|
|
||||||
|
|
@ -91,14 +82,9 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
||||||
Assert.Equal("application/vnd.error+json", response.Content.Headers.ContentType.MediaType);
|
Assert.Equal("application/vnd.error+json", response.Content.Headers.ContentType.MediaType);
|
||||||
var actual = JsonConvert.DeserializeObject<VndError[]>(await response.Content.ReadAsStringAsync());
|
var content = await response.Content.ReadAsStringAsync();
|
||||||
actual = actual.OrderBy(e => e.Path).ToArray();
|
var actual = JsonConvert.DeserializeObject<Dictionary<string, string[]>>(content);
|
||||||
Assert.Equal(expected.Length, actual.Length);
|
Assert.Equal(expected, actual);
|
||||||
for (var i = 0; i < expected.Length; i++)
|
|
||||||
{
|
|
||||||
Assert.Equal(expected[i].Path, expected[i].Path);
|
|
||||||
Assert.Equal(expected[i].Message, expected[i].Message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,10 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.Mvc.Abstractions;
|
using Microsoft.AspNetCore.Mvc.Abstractions;
|
||||||
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace BasicWebSite
|
namespace BasicWebSite
|
||||||
|
|
@ -42,7 +38,6 @@ namespace BasicWebSite
|
||||||
services.AddSingleton<IActionDescriptorProvider, ActionDescriptorCreationCounter>();
|
services.AddSingleton<IActionDescriptorProvider, ActionDescriptorCreationCounter>();
|
||||||
services.AddHttpContextAccessor();
|
services.AddHttpContextAccessor();
|
||||||
services.AddSingleton<ContactsRepository>();
|
services.AddSingleton<ContactsRepository>();
|
||||||
services.AddSingleton<IErrorDescriptorProvider, VndErrorDescriptionProvider>();
|
|
||||||
services.AddScoped<RequestIdService>();
|
services.AddScoped<RequestIdService>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,23 +0,0 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
|
||||||
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
|
|
||||||
namespace BasicWebSite
|
|
||||||
{
|
|
||||||
public class VndError
|
|
||||||
{
|
|
||||||
public string LogRef { get; set; }
|
|
||||||
|
|
||||||
public string Path { get; set; }
|
|
||||||
|
|
||||||
public string Message { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,44 +0,0 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
|
||||||
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
|
||||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
|
|
||||||
namespace BasicWebSite
|
|
||||||
{
|
|
||||||
public class VndErrorDescriptionProvider : IErrorDescriptorProvider
|
|
||||||
{
|
|
||||||
public int Order => 0;
|
|
||||||
|
|
||||||
public void OnProvidersExecuting(ErrorDescriptionContext context)
|
|
||||||
{
|
|
||||||
if (context.ActionDescriptor.FilterDescriptors.Any(f => f.Filter is VndErrorAttribute) &&
|
|
||||||
context.Result is ModelStateDictionary dictionary)
|
|
||||||
{
|
|
||||||
var vndErrors = new List<VndError>();
|
|
||||||
foreach (var item in dictionary)
|
|
||||||
{
|
|
||||||
foreach (var modelError in item.Value.Errors)
|
|
||||||
{
|
|
||||||
vndErrors.Add(new VndError
|
|
||||||
{
|
|
||||||
LogRef = modelError.ErrorMessage,
|
|
||||||
Path = item.Key,
|
|
||||||
Message = modelError.ErrorMessage,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
context.Result = vndErrors;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue