-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ODataError for non-success responses (#623)
- Loading branch information
Showing
21 changed files
with
4,811 additions
and
5 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
252 changes: 252 additions & 0 deletions
252
src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml
Large diffs are not rendered by default.
Oops, something went wrong.
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
74 changes: 74 additions & 0 deletions
74
src/Microsoft.AspNetCore.OData/Results/BadRequestODataResult.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,74 @@ | ||
//----------------------------------------------------------------------------- | ||
// <copyright file="BadRequestODataResult.cs" company=".NET Foundation"> | ||
// Copyright (c) .NET Foundation and Contributors. All rights reserved. | ||
// See License.txt in the project root for license information. | ||
// </copyright> | ||
//------------------------------------------------------------------------------ | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.OData; | ||
using ErrorUtils = Microsoft.AspNetCore.OData.Error; | ||
|
||
namespace Microsoft.AspNetCore.OData.Results | ||
{ | ||
/// <summary> | ||
/// Represents a result that when executed will produce a Bad Request (400) response. | ||
/// </summary> | ||
/// <remarks>This result creates an <see cref="ODataError"/> with status code: 400.</remarks> | ||
public class BadRequestODataResult : BadRequestResult, IODataErrorResult | ||
{ | ||
private const string errorCode = "400"; | ||
|
||
/// <summary> | ||
/// OData error. | ||
/// </summary> | ||
public ODataError Error { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the class. | ||
/// </summary> | ||
/// <param name="message">Error message.</param> | ||
public BadRequestODataResult(string message) | ||
{ | ||
if (string.IsNullOrEmpty(message)) | ||
{ | ||
throw ErrorUtils.ArgumentNullOrEmpty(nameof(message)); | ||
} | ||
|
||
Error = new ODataError | ||
{ | ||
Message = message, | ||
ErrorCode = errorCode | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the class. | ||
/// </summary> | ||
/// <param name="odataError">An <see cref="ODataError"/> object.</param> | ||
public BadRequestODataResult(ODataError odataError) | ||
{ | ||
if (odataError == null) | ||
{ | ||
throw ErrorUtils.ArgumentNull(nameof(odataError)); | ||
} | ||
|
||
ErrorUtils.ValidateErrorCode(errorCode, odataError); | ||
|
||
Error = odataError; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async override Task ExecuteResultAsync(ActionContext context) | ||
{ | ||
ObjectResult objectResult = new ObjectResult(Error) | ||
{ | ||
StatusCode = StatusCodes.Status400BadRequest | ||
}; | ||
|
||
await objectResult.ExecuteResultAsync(context).ConfigureAwait(false); | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/Microsoft.AspNetCore.OData/Results/ConflictODataResult.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,74 @@ | ||
//----------------------------------------------------------------------------- | ||
// <copyright file="ConflictODataResult.cs" company=".NET Foundation"> | ||
// Copyright (c) .NET Foundation and Contributors. All rights reserved. | ||
// See License.txt in the project root for license information. | ||
// </copyright> | ||
//------------------------------------------------------------------------------ | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.OData; | ||
using ErrorUtils = Microsoft.AspNetCore.OData.Error; | ||
|
||
namespace Microsoft.AspNetCore.OData.Results | ||
{ | ||
/// <summary> | ||
/// Represents a result that when executed will produce a Conflict (409) response. | ||
/// </summary> | ||
/// <remarks>This result creates an <see cref="ODataError"/> with status code: 409.</remarks> | ||
public class ConflictODataResult : ConflictResult, IODataErrorResult | ||
{ | ||
private const string errorCode = "409"; | ||
|
||
/// <summary> | ||
/// OData error. | ||
/// </summary> | ||
public ODataError Error { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the class. | ||
/// </summary> | ||
/// <param name="message">Error message.</param> | ||
public ConflictODataResult(string message) | ||
{ | ||
if (string.IsNullOrEmpty(message)) | ||
{ | ||
throw ErrorUtils.ArgumentNullOrEmpty(nameof(message)); | ||
} | ||
|
||
Error = new ODataError | ||
{ | ||
Message = message, | ||
ErrorCode = errorCode | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the class. | ||
/// </summary> | ||
/// <param name="odataError">An <see cref="ODataError"/> object.</param> | ||
public ConflictODataResult(ODataError odataError) | ||
{ | ||
if (odataError == null) | ||
{ | ||
throw ErrorUtils.ArgumentNull(nameof(odataError)); | ||
} | ||
|
||
ErrorUtils.ValidateErrorCode(errorCode, odataError); | ||
|
||
Error = odataError; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async override Task ExecuteResultAsync(ActionContext context) | ||
{ | ||
ObjectResult objectResult = new ObjectResult(Error) | ||
{ | ||
StatusCode = StatusCodes.Status409Conflict | ||
}; | ||
|
||
await objectResult.ExecuteResultAsync(context).ConfigureAwait(false); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Microsoft.AspNetCore.OData/Results/IODataErrorResult.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,24 @@ | ||
//----------------------------------------------------------------------------- | ||
// <copyright file="IODataErrorResult.cs" company=".NET Foundation"> | ||
// Copyright (c) .NET Foundation and Contributors. All rights reserved. | ||
// See License.txt in the project root for license information. | ||
// </copyright> | ||
//------------------------------------------------------------------------------ | ||
|
||
using Microsoft.OData; | ||
|
||
namespace Microsoft.AspNetCore.OData.Results | ||
{ | ||
/// <summary> | ||
/// Provide the interface for the details of a given OData error result. | ||
/// </summary> | ||
public interface IODataErrorResult | ||
{ | ||
/// <summary> | ||
/// OData error. | ||
/// </summary> | ||
#pragma warning disable CA1716 // Identifiers should not match keywords | ||
ODataError Error { get; } | ||
#pragma warning restore CA1716 // Identifiers should not match keywords | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/Microsoft.AspNetCore.OData/Results/NotFoundODataResult.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,74 @@ | ||
//----------------------------------------------------------------------------- | ||
// <copyright file="NotFoundODataResult.cs" company=".NET Foundation"> | ||
// Copyright (c) .NET Foundation and Contributors. All rights reserved. | ||
// See License.txt in the project root for license information. | ||
// </copyright> | ||
//------------------------------------------------------------------------------ | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.OData; | ||
using ErrorUtils = Microsoft.AspNetCore.OData.Error; | ||
|
||
namespace Microsoft.AspNetCore.OData.Results | ||
{ | ||
/// <summary> | ||
/// Represents a result that when executed will produce a Not Found (404) response. | ||
/// </summary> | ||
/// <remarks>This result creates an <see cref="ODataError"/> with status code: 404.</remarks> | ||
public class NotFoundODataResult : NotFoundResult, IODataErrorResult | ||
{ | ||
private const string errorCode = "404"; | ||
|
||
/// <summary> | ||
/// OData error. | ||
/// </summary> | ||
public ODataError Error { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the class. | ||
/// </summary> | ||
/// <param name="message">Error message.</param> | ||
public NotFoundODataResult(string message) | ||
{ | ||
if (string.IsNullOrEmpty(message)) | ||
{ | ||
throw ErrorUtils.ArgumentNullOrEmpty(nameof(message)); | ||
} | ||
|
||
Error = new ODataError | ||
{ | ||
Message = message, | ||
ErrorCode = errorCode | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the class. | ||
/// </summary> | ||
/// <param name="odataError">An <see cref="ODataError"/> object.</param> | ||
public NotFoundODataResult(ODataError odataError) | ||
{ | ||
if (odataError == null) | ||
{ | ||
throw ErrorUtils.ArgumentNull(nameof(odataError)); | ||
} | ||
|
||
ErrorUtils.ValidateErrorCode(errorCode, odataError); | ||
|
||
Error = odataError; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async override Task ExecuteResultAsync(ActionContext context) | ||
{ | ||
ObjectResult objectResult = new ObjectResult(Error) | ||
{ | ||
StatusCode = StatusCodes.Status404NotFound | ||
}; | ||
|
||
await objectResult.ExecuteResultAsync(context).ConfigureAwait(false); | ||
} | ||
} | ||
} |
Oops, something went wrong.