-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates 400 responses to use the standard error response format with …
- Loading branch information
1 parent
6464a49
commit 239ec61
Showing
10 changed files
with
323 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
src/Microsoft.AspNet.WebApi.Versioning/Versioning/ApiVersioningOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
namespace Microsoft.Web.Http.Versioning | ||
{ | ||
using System.Diagnostics.Contracts; | ||
using System.Net.Http; | ||
using System.Net.Http.Formatting; | ||
using System.Web.Http; | ||
using static System.Net.HttpStatusCode; | ||
using static System.String; | ||
|
||
/// <content> | ||
/// Provides additional implementation specific to Microsoft ASP.NET Web API. | ||
/// </content> | ||
public partial class ApiVersioningOptions | ||
{ | ||
private CreateBadRequestDelegate createBadRequest = CreateDefaultBadRequest; | ||
|
||
/// <summary> | ||
/// Gets or sets the function to used to create HTTP 400 (Bad Request) responses related to API versioning. | ||
/// </summary> | ||
/// <value>The <see cref="CreateBadRequestDelegate">function</see> to used to create a HTTP 400 (Bad Request) | ||
/// <see cref="HttpResponseMessage">response</see> related to API versioning.</value> | ||
/// <remarks>The default value generates responses that are compliant with the Microsoft REST API Guidelines. | ||
/// This option should only be changed by service authors that intentionally want to deviate from the | ||
/// established guidance.</remarks> | ||
public CreateBadRequestDelegate CreateBadRequest | ||
{ | ||
get | ||
{ | ||
Contract.Ensures( createBadRequest != null ); | ||
return createBadRequest; | ||
} | ||
set | ||
{ | ||
Arg.NotNull( value, nameof( value ) ); | ||
createBadRequest = value; | ||
} | ||
} | ||
|
||
private static HttpResponseMessage CreateDefaultBadRequest( HttpRequestMessage request, string code, string message, string messageDetail ) | ||
{ | ||
if ( request == null || !IsODataRequest( request ) ) | ||
{ | ||
return CreateWebApiBadRequest( request, code, message, messageDetail ); | ||
} | ||
|
||
return CreateODataBadRequest( request, code, message, messageDetail ); | ||
} | ||
|
||
private static HttpResponseMessage CreateWebApiBadRequest( HttpRequestMessage request, string code, string message, string messageDetail ) | ||
{ | ||
var error = new HttpError(); | ||
var root = new HttpError() { ["Error"] = error }; | ||
|
||
if ( !IsNullOrEmpty( code ) ) | ||
{ | ||
error["Code"] = code; | ||
} | ||
|
||
if ( !IsNullOrEmpty( message ) ) | ||
{ | ||
error.Message = message; | ||
} | ||
|
||
if ( !IsNullOrEmpty( messageDetail ) && request?.ShouldIncludeErrorDetail() == true ) | ||
{ | ||
error["InnerError"] = new HttpError( messageDetail ); | ||
} | ||
|
||
if ( request == null ) | ||
{ | ||
return new HttpResponseMessage( BadRequest ) | ||
{ | ||
Content = new ObjectContent<HttpError>( root, new JsonMediaTypeFormatter() ) | ||
}; | ||
} | ||
|
||
return request.CreateErrorResponse( BadRequest, root ); | ||
} | ||
|
||
private static bool IsODataRequest( HttpRequestMessage request ) | ||
{ | ||
if ( request == null ) | ||
{ | ||
return false; | ||
} | ||
|
||
var routeValues = request.GetRouteData(); | ||
|
||
if ( routeValues == null ) | ||
{ | ||
return false; | ||
} | ||
|
||
if ( !routeValues.Values.ContainsKey( "odataPath" ) ) | ||
{ | ||
return false; | ||
} | ||
|
||
return request.GetConfiguration()?.Formatters.JsonFormatter == null; | ||
} | ||
|
||
private static HttpResponseMessage CreateODataBadRequest( HttpRequestMessage request, string code, string message, string messageDetail ) | ||
{ | ||
Contract.Requires( request != null ); | ||
|
||
var error = new HttpError(); | ||
|
||
if ( !IsNullOrEmpty( code ) ) | ||
{ | ||
error[HttpErrorKeys.ErrorCodeKey] = code; | ||
} | ||
|
||
if ( !IsNullOrEmpty( message ) ) | ||
{ | ||
error.Message = message; | ||
} | ||
|
||
if ( !IsNullOrEmpty( messageDetail ) && request?.ShouldIncludeErrorDetail() == true ) | ||
{ | ||
error[HttpErrorKeys.MessageDetailKey] = messageDetail; | ||
} | ||
|
||
return request.CreateErrorResponse( BadRequest, error ); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Microsoft.AspNet.WebApi.Versioning/Versioning/CreateBadRequestDelegate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace Microsoft.Web.Http.Versioning | ||
{ | ||
using System; | ||
using System.Net.Http; | ||
using System.Web.Http; | ||
|
||
/// <summary> | ||
/// Represents the function invoked to create a HTTP 400 (Bad Request) response related to API versioning. | ||
/// </summary> | ||
/// <param name="request">The current <see cref="HttpRequestMessage">HTTP request</see>.</param> | ||
/// <param name="code">The associated error code.</param> | ||
/// <param name="message">The error message.</param> | ||
/// <param name="messageDetail">The detailed error message, if any.</param> | ||
/// <returns>A <see cref="HttpResponseMessage">HTTP response</see> representing for status code 400 (Bad Request).</returns> | ||
public delegate HttpResponseMessage CreateBadRequestDelegate( HttpRequestMessage request, string code, string message, string messageDetail ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/Microsoft.AspNetCore.Mvc.Versioning/Versioning/ApiVersioningOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
namespace Microsoft.AspNetCore.Mvc.Versioning | ||
{ | ||
using Hosting; | ||
using Http; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Contracts; | ||
using static System.String; | ||
|
||
/// <content> | ||
/// Provides additional implementation specific to Microsoft ASP.NET Web API. | ||
/// </content> | ||
public partial class ApiVersioningOptions | ||
{ | ||
private CreateBadRequestDelegate createBadRequest = CreateDefaultBadRequest; | ||
|
||
/// <summary> | ||
/// Gets or sets the function to used to create HTTP 400 (Bad Request) responses related to API versioning. | ||
/// </summary> | ||
/// <value>The <see cref="CreateBadRequestDelegate">function</see> to used to create a HTTP 400 (Bad Request) | ||
/// <see cref="HttpResponse">response</see> related to API versioning.</value> | ||
/// <remarks>The default value generates responses that are compliant with the Microsoft REST API Guidelines. | ||
/// This option should only be changed by service authors that intentionally want to deviate from the | ||
/// established guidance.</remarks> | ||
[CLSCompliant( false )] | ||
public CreateBadRequestDelegate CreateBadRequest | ||
{ | ||
get | ||
{ | ||
Contract.Ensures( createBadRequest != null ); | ||
return createBadRequest; | ||
} | ||
set | ||
{ | ||
Arg.NotNull( value, nameof( value ) ); | ||
createBadRequest = value; | ||
} | ||
} | ||
|
||
private static BadRequestObjectResult CreateDefaultBadRequest( HttpRequest request, string code, string message, string messageDetail ) | ||
{ | ||
var error = new Dictionary<string, object>(); | ||
var root = new Dictionary<string, object>() { ["Error"] = error }; | ||
|
||
if ( !IsNullOrEmpty( code ) ) | ||
{ | ||
error["Code"] = code; | ||
} | ||
|
||
if ( !IsNullOrEmpty( message ) ) | ||
{ | ||
error["Message"] = message; | ||
} | ||
|
||
if ( !IsNullOrEmpty( messageDetail ) ) | ||
{ | ||
var environment = (IHostingEnvironment) request?.HttpContext.RequestServices.GetService( typeof( IHostingEnvironment ) ); | ||
|
||
if ( environment?.IsDevelopment() == true ) | ||
{ | ||
error["InnerError"] = new Dictionary<string, object>() { ["Message"] = messageDetail }; | ||
} | ||
} | ||
|
||
return new BadRequestObjectResult( root ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/Microsoft.AspNetCore.Mvc.Versioning/Versioning/CreateBadRequestDelegate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace Microsoft.AspNetCore.Mvc.Versioning | ||
{ | ||
using AspNetCore.Mvc; | ||
using Http; | ||
using System; | ||
|
||
/// <summary> | ||
/// Represents the function invoked to create a HTTP 400 (Bad Request) response related to API versioning. | ||
/// </summary> | ||
/// <param name="request">The current <see cref="HttpRequest">HTTP request</see>.</param> | ||
/// <param name="code">The associated error code.</param> | ||
/// <param name="message">The error message.</param> | ||
/// <param name="messageDetail">The detailed error message, if any.</param> | ||
/// <returns>A <see cref="BadRequestObjectResult">HTTP response</see> representing for status code 400 (Bad Request).</returns> | ||
[CLSCompliant( false )] | ||
public delegate BadRequestObjectResult CreateBadRequestDelegate( HttpRequest request, string code, string message, string messageDetail ); | ||
} |
Oops, something went wrong.