Skip to content
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

Create ODataError in Non-Success responses #2341

Merged
merged 26 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions src/Microsoft.AspNetCore.OData/ODataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.AspNet.OData.Common;
using Microsoft.AspNet.OData.Results;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OData;

namespace Microsoft.AspNet.OData
{
Expand Down Expand Up @@ -51,5 +52,131 @@ protected virtual UpdatedODataResult<TEntity> Updated<TEntity>(TEntity entity)

return new UpdatedODataResult<TEntity>(entity);
}

/// <summary>
/// Creates a <see cref="StatusCodeResult"/> that when executed will produce a Bad Request (400) response.
/// </summary>
/// <param name="message">Error Message</param>
/// <returns>A <see cref="BadRequestODataResult"/> with the specified values.</returns>
protected virtual BadRequestODataResult BadRequest(string message)
{
return new BadRequestODataResult(message);
}

/// <summary>
/// Creates a <see cref="StatusCodeResult"/> that when executed will produce a Bad Request (400) response.
/// </summary>
/// <param name="odataError">Parameter of type <see cref="ODataError"/>.</param>
/// <returns>A <see cref="BadRequestODataResult"/> with the specified values.</returns>
protected virtual BadRequestODataResult BadRequest(ODataError odataError)
{
return new BadRequestODataResult(odataError);
}

/// <summary>
/// Creates a <see cref="StatusCodeResult"/> that when executed will produce a Not Found (404) response.
/// </summary>
/// <param name="message">Error Message</param>
/// <returns>A <see cref="NotFoundODataResult"/> with the specified values.</returns>
protected virtual NotFoundODataResult NotFound(string message)
{
return new NotFoundODataResult(message);
}

/// <summary>
/// Creates a <see cref="StatusCodeResult"/> that when executed will produce a Not Found (404) response.
/// </summary>
/// <param name="odataError">Parameter of type <see cref="ODataError"/>.</param>
/// <returns>A <see cref="NotFoundODataResult"/> with the specified values.</returns>
protected virtual NotFoundODataResult NotFound(ODataError odataError)
{
return new NotFoundODataResult(odataError);
}

/// <summary>
/// Creates a <see cref="StatusCodeResult"/> that when executed will produce a Unauthorized (401) response.
/// </summary>
/// <param name="message">Error Message</param>
/// <returns>An <see cref="UnauthorizedODataResult"/> with the specified values.</returns>
protected virtual UnauthorizedODataResult Unauthorized(string message)
{
return new UnauthorizedODataResult(message);
}

/// <summary>
/// Creates a <see cref="StatusCodeResult"/> that when executed will produce a Unauthorized (401) response.
/// </summary>
/// <param name="odataError">Parameter of type <see cref="ODataError"/>.</param>
/// <returns>An <see cref="UnauthorizedODataResult"/> with the specified values.</returns>
protected virtual UnauthorizedODataResult Unauthorized(ODataError odataError)
{
return new UnauthorizedODataResult(odataError);
}

// ConflictResult and UnprocessableEntityResult were introduced in AspNet core 2.1, which is implemented from .Net standard 2.1
// https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.conflict?view=aspnetcore-2.1
// https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.unprocessableentityresult?view=aspnetcore-2.1
#if !NETSTANDARD2_0
/// <summary>
/// Creates a <see cref="StatusCodeResult"/> that when executed will produce a Conflict (409) response.
/// </summary>
/// <param name="message">Error Message</param>
/// <returns>A <see cref="ConflictODataResult"/> with the specified values.</returns>
protected virtual ConflictODataResult Conflict(string message)
{
return new ConflictODataResult(message);
}

/// <summary>
/// Creates a <see cref="StatusCodeResult"/> that when executed will produce a Conflict (409) response.
/// </summary>
/// <param name="odataError">Parameter of type <see cref="ODataError"/>.</param>
/// <returns>A <see cref="ConflictODataResult"/> with the specified values.</returns>
protected virtual ConflictODataResult Conflict(ODataError odataError)
{
return new ConflictODataResult(odataError);
}

/// <summary>
/// Creates a <see cref="StatusCodeResult"/> that when executed will produce an UnprocessableEntity (422) response.
/// </summary>
/// <param name="message">Error Message</param>
/// <returns>An <see cref="UnprocessableEntityODataResult"/> with the specified values.</returns>
protected virtual UnprocessableEntityODataResult UnprocessableEntity(string message)
{
return new UnprocessableEntityODataResult(message);
}

/// <summary>
/// Creates a <see cref="StatusCodeResult"/> that when executed will produce an UnprocessableEntity (422) response.
/// </summary>
/// <param name="odataError">Parameter of type <see cref="ODataError"/>.</param>
/// <returns>An <see cref="UnprocessableEntityODataResult"/> with the specified values.</returns>
protected virtual UnprocessableEntityODataResult UnprocessableEntity(ODataError odataError)
{
return new UnprocessableEntityODataResult(odataError);
}
#endif

/// <summary>
/// Creates a <see cref="ActionResult"/> that when executed will produce an <see cref="ODataError"/> response.
/// </summary>
/// <param name="errorCode">Http Error code.</param>
/// <param name="message">Http Error Message.</param>
/// <returns>An <see cref="Microsoft.AspNet.OData.Results.ODataErrorResult"/> with the specified values.</returns>
protected virtual ODataErrorResult ODataErrorResult(string errorCode, string message)
{
return new ODataErrorResult(errorCode, message);
}
KenitoInc marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Creates a <see cref="ActionResult"/> that when executed will produce an <see cref="ODataError"/> response.
/// </summary>
/// <param name="odataError"><see cref="ODataError"/>.</param>
/// <returns>An <see cref="Microsoft.AspNet.OData.Results.ODataErrorResult"/> with the specified values.</returns>
protected virtual ODataErrorResult ODataErrorResult(ODataError odataError)
{
return new ODataErrorResult(odataError);
}
}
}
65 changes: 65 additions & 0 deletions src/Microsoft.AspNetCore.OData/Results/BadRequestODataResult.cs
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.
Show resolved Hide resolved
ErrorCode = "400"
};
}
KenitoInc marked this conversation as resolved.
Show resolved Hide 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.
Show resolved Hide 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.
Show resolved Hide resolved
67 changes: 67 additions & 0 deletions src/Microsoft.AspNetCore.OData/Results/ConflictODataResult.cs
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 src/Microsoft.AspNetCore.OData/Results/IODataErrorResult.cs
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 src/Microsoft.AspNetCore.OData/Results/NotFoundODataResult.cs
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);
}
}
}
Loading