Jeroen Everhard

Jeroen Everhard

  • 1.5k
  • 111
  • 7.4k

WebApi2 OData return custom error object

Apr 14 2017 9:35 AM

Hello,

I've searched this intensive but can't get it to work.

I've haven an Web AP2 OData API and need to return a custom error class.

Here is what I have:

public class Error
{
public string Code { get; set; }
public string Message { get; set; }
}

In Global.asax.cs

protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);

GlobalConfiguration.Configuration.MessageHandlers.Add(new MessageInterceptor());
}


public class MessageInterceptor : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{

return base.SendAsync(request, cancellationToken).ContinueWith(
task =>
{
var body = task.Result.Content.ReadAsStringAsync().Result;
var resultObj = JsonConvert.DeserializeObject(body.Replace("value", "Results"));
task.Result.Content = new ObjectContent(typeof(object), resultObj, new JsonMediaTypeFormatter());
return task.Result;
}, cancellationToken);
}
}

In WebApiConfig,cs

public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{

config.Filters.Add(new HandleApiExceptionAttribute());

}

}

public class HandleApiExceptionAttribute : ExceptionFilterAttribute, IExceptionFilter
{
public override void OnException(HttpActionExecutedContext context)
{
if (context.Exception is Error)
{
var res = context.Exception.Message;

//Define the Response Message
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent(res),
ReasonPhrase = res
};

//Create the Error Response
context.Response = response;
}
}


public class OrdersController : ODataControllerA controller
{
private Context db = new Context();

// GET: odata/Orders
[Queryable]
public IQueryable<Orders> GetOrders(ODataQueryOptions<Orders> opts)

{

<some code producing error>

Error theError = new Error()
{
Code = "1000",
Message = "Geen filter gespecificeerd"
};

HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest, theError);
//return Request.CreateResponse(HttpStatusCode.BadRequest, theError);
throw new HttpResponseException(response);

}


When I try this this crashes in teh MessageInterceptor.

This is there because a third party consuming the API want's it in the specific format.

When the code runs correct is return results{}

On error it should return Error{code:, message: }

Anyone ideas?


Jeroen

Answers (1)