OData WebApi 2 Error Handling

In my web service, I override ExceptionHandler, but it is not clear to me how you format the exception to conform to the OData Error standard. Perhaps I am approaching this incorrectly, since I cannot find examples on the Internet.

From my point of view, from web api 2 there is a concept of global exception handling in which you use a custom ExceptionHandler to handle any exceptions caused by the service. Exception is expected to update ExceptionContext.Result with the new IHttpActionResult (). How do you format the data entered in IHttpActionResult for formatting in OData Error.

The following is an ExceptionHandler snippet, and I am fixated on how you redefine the context. Run the correct OData HttpResponse message.

public class CustomExceptionHandler: ExceptionHandler { public override void Handle(ExceptionHandlerContext context) { HttpResponseMessage msg = context.Request.CreateErrorResponse(HttpStatusCode.NotFound, new ODataError { ErrorCode = context.Exception.Message, Message = context.Exception.InnerException.Message, InnerError = new ODataInnerError { Message = context.Exception.InnerException.Message } }); context.Result = //How do you wrap the OData HttpResponseMessage into a IHttpActionResult } } 

Any advice appreciated, Thanks, D

+5
source share
1 answer
 context.Result = new System.Web.Http.Results.ResponseMessageResult(msg); 
0
source

Source: https://habr.com/ru/post/1237708/


All Articles