-
Notifications
You must be signed in to change notification settings - Fork 467
Create ODataError in Non-Success responses #2341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
KenitoInc
merged 26 commits into
OData:master
from
KenitoInc:fix/2331-odata-error-patch
Dec 16, 2020
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
0b7919f
Create ODataError in Non-Success responses
KenitoInc 4f1ea03
Add NetCore tests
KenitoInc 35ccc3f
Update tests
KenitoInc b09ec79
Fix failing tests
KenitoInc d69d12c
Move logic to ODataController
KenitoInc 548be2f
Remove unnecessary using
KenitoInc 2ada4ab
Rename result classes
KenitoInc 73f8979
Add Unathorized and improve code
KenitoInc abd797e
Add test for Unauthorized
KenitoInc 8efa91c
Update baseline file
KenitoInc ed4b5b3
Add overload with ODataError
KenitoInc e7bb485
Add ODataError
KenitoInc 19a47b5
Fix based on review feedback
KenitoInc 0d89f91
Fix failing E2E test
KenitoInc eac575d
Update unit tests
KenitoInc ce95fdc
Update test controllers
KenitoInc b394cc0
Restrict Unauthorized to NetCore
KenitoInc 63f0ea2
Fix test controller
KenitoInc f80695f
Fix based on review comments
KenitoInc e22e3b7
Null check
KenitoInc c68f8f1
Update baseline files
KenitoInc 028913b
Add UnprocessableEntity and minor fixes
KenitoInc 1b193bb
Add UnProcessableEntity tests
KenitoInc 1e010ea
Add E2E test
KenitoInc eff3d41
Fix E2E tests bug
KenitoInc ec8ae2a
Update docstrings
KenitoInc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
65 changes: 65 additions & 0 deletions
65
src/Microsoft.AspNetCore.OData/Results/BadRequestODataResult.cs
This file contains hidden or 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,65 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.OData; | ||
|
|
||
| namespace Microsoft.AspNet.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 | ||
| { | ||
| /// <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 (message == null) | ||
| { | ||
| throw Common.Error.ArgumentNull("message"); | ||
| } | ||
|
|
||
| Error = new ODataError | ||
| { | ||
| Message = message, | ||
|
KenitoInc marked this conversation as resolved.
|
||
| ErrorCode = "400" | ||
| }; | ||
| } | ||
|
KenitoInc marked this conversation as resolved.
|
||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the class. | ||
| /// </summary> | ||
| /// <param name="odataError">OData Error.</param> | ||
| public BadRequestODataResult(ODataError odataError) | ||
| { | ||
| if (odataError == null) | ||
| { | ||
| throw Common.Error.ArgumentNull("odataError"); | ||
| } | ||
|
|
||
| Error = odataError; | ||
|
KenitoInc marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public async override Task ExecuteResultAsync(ActionContext context) | ||
| { | ||
| ObjectResult objectResult = new ObjectResult(Error) | ||
| { | ||
| StatusCode = StatusCodes.Status400BadRequest | ||
| }; | ||
|
|
||
| await objectResult.ExecuteResultAsync(context).ConfigureAwait(false); | ||
| } | ||
| } | ||
| } | ||
|
KenitoInc marked this conversation as resolved.
|
||
67 changes: 67 additions & 0 deletions
67
src/Microsoft.AspNetCore.OData/Results/ConflictODataResult.cs
This file contains hidden or 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,67 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| #if !NETSTANDARD2_0 | ||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.OData; | ||
|
|
||
| namespace Microsoft.AspNet.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 | ||
| { | ||
| /// <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 (message == null) | ||
| { | ||
| throw Common.Error.ArgumentNull("message"); | ||
| } | ||
|
|
||
| Error = new ODataError | ||
| { | ||
| Message = message, | ||
| ErrorCode = "409" | ||
| }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the class. | ||
| /// </summary> | ||
| /// <param name="odataError">OData Error.</param> | ||
| public ConflictODataResult(ODataError odataError) | ||
| { | ||
| if (odataError == null) | ||
| { | ||
| throw Common.Error.ArgumentNull("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); | ||
| } | ||
| } | ||
| } | ||
| #endif |
18 changes: 18 additions & 0 deletions
18
src/Microsoft.AspNetCore.OData/Results/IODataErrorResult.cs
This file contains hidden or 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,18 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using Microsoft.OData; | ||
|
|
||
| namespace Microsoft.AspNet.OData.Results | ||
| { | ||
| /// <summary> | ||
| /// Provide the interface for the details of a given OData Error result. | ||
| /// </summary> | ||
| public interface IODataErrorResult | ||
| { | ||
| /// <summary> | ||
| /// OData Error. | ||
| /// </summary> | ||
| ODataError Error { get; } | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
src/Microsoft.AspNetCore.OData/Results/NotFoundODataResult.cs
This file contains hidden or 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,65 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.OData; | ||
|
|
||
| namespace Microsoft.AspNet.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 | ||
| { | ||
| /// <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 (message == null) | ||
| { | ||
| throw Common.Error.ArgumentNull("message"); | ||
| } | ||
|
|
||
| Error = new ODataError | ||
| { | ||
| Message = message, | ||
| ErrorCode = "404" | ||
| }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the class. | ||
| /// </summary> | ||
| /// <param name="odataError">OData Error.</param> | ||
| public NotFoundODataResult(ODataError odataError) | ||
| { | ||
| if (odataError == null) | ||
| { | ||
| throw Common.Error.ArgumentNull("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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.