Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customized error responses for ApiVersioning errors in webapi dotnet core

I am creating a package lib for all the errors in a Webapi service. This library will be used for providing custom responses for BadRequest, BadArgument, ApiVersionsing etc.. related errors. I need help in customizing Apiversion related errors for - ApiVersionUnspecified, UnsupportedApiVersion, InvalidApiVersion, AmbiguousApiVersion. I have follow this article to include api-versioning for my project - https://www.hanselman.com/blog/ASPNETCoreRESTfulWebAPIVersioningMadeEasy.aspx

I have checked the github wiki for the above package and found that "Depending on the desired behavior, you can extend the DefaultErrorResponseProvider or you can implement your own IErrorResponseProvider from stratch.

To wire up an alternate error response behavior, replace the default provider with your own:"

options => options.ErrorResponses = new MyErrorResponseProvider();

However; I am not quite getting how can I customize the default error responses in MyErrorResponseProvider class. Can somebody please provide me with any example so I can get started with this?

Thanks in advance!

like image 407
arpymastro Avatar asked Mar 06 '18 06:03

arpymastro


2 Answers

The answer customizes only the error message returned by the ASP.NET API Versioning.

To customize the whole response you can implement it by returning ObjectResult.

Startup.cs

// Add API Versioning to the service container to your project
        services.AddApiVersioning(config =>
        {
            // Advertise the API versions supported for the particular endpoint
            config.ReportApiVersions = true;
            config.ErrorResponses = new ApiVersioningErrorResponseProvider();//Send standard error response when API version error.
        });

ApiVersioningErrorResponseProvider.cs

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Versioning;

public class ApiVersioningErrorResponseProvider : DefaultErrorResponseProvider
{
    public override IActionResult CreateResponse(ErrorResponseContext context)
    {
       //You can initialize your own class here. Below is just a sample.
        var errorResponse = new
        {
            ResponseCode = 101,
            ResponseMessages = "Something went wrong while selecting the api version",
            HelpLink = "https://github.com/microsoft/aspnet-api-versioning/wiki/Error-Response-Provider"
        };
        var response = new ObjectResult(errorResponse);
        response.StatusCode = (int)HttpStatusCode.BadRequest;

        return response;
    }
}

Which produces below output:

{
"ResponseCode": 101,
"ResponseMessages": "Something went wrong while selecting the api version",
"HelpLink": "https://github.com/microsoft/aspnet-api-versioning/wiki/Error-Response-Provider"
}
like image 32
Kishan Vaishnav Avatar answered Nov 08 '22 00:11

Kishan Vaishnav


Found the way of implementing above as -

class MyErrorResponseProvider : DefaultErrorResponseProvider
{
// note: in Web API the response type is HttpResponseMessage
public override IActionResult CreateResponse( ErrorResponseContext context )
{
       switch ( context.ErrorCode )
       {
           case "UnsupportedApiVersion":
               context = new ErrorResponseContext(
                   context.Request,
                   context.StatusCode,
                   context.ErrorCode,
                   "My custom error message.",
                   context.MessageDetail );
               break;
       }

       return base.CreateResponse( context );
}
}

Thanks to github issue @ - https://github.com/Microsoft/aspnet-api-versioning/issues/233

like image 83
arpymastro Avatar answered Nov 08 '22 02:11

arpymastro