locked
How would someone using java be able to get the full error message from web api? RRS feed

  • Question

  • User453286268 posted

    I have a java developer who is using a .net web api. I have enabled full error messages and I have tested it on POSTMAN and I am getting the full error with no problems. Does anyone know what he would have to do to get that full error message. He says that he is still just getting error codes like 400 with no details.

    Thursday, May 12, 2016 10:19 PM

Answers

  • User36583972 posted

    Hi lolzoid,

    In Web API, we can use Exception filters to handle runtime errors in your Web API application. You can define a CustomExceptionFilter class like the below.

      public class CustomExceptionFilter : ExceptionFilterAttribute
        {
            public override void OnException(HttpActionExecutedContext actionExecutedContext)
            {
                HttpStatusCode status = HttpStatusCode.InternalServerError;
                String message = String.Empty;
                var exceptionType = actionExecutedContext.Exception.GetType();
                
                if (exceptionType == typeof(UnauthorizedAccessException))
                {
                    message = "Access to the Web API is not authorized."; //you can define message by yourself
                    status = HttpStatusCode.Unauthorized;
                }
                else if (exceptionType == typeof(DivideByZeroException))
                {
                    message = "Internal Server Error.";//you can define message by yourself
                    status = HttpStatusCode.InternalServerError;
                }
                else
                {
                    message = "Not found.";//you can define message by yourself
                    status = HttpStatusCode.NotFound;
                }
                actionExecutedContext.Response = new HttpResponseMessage()
                {
                    Content = new StringContent(message, System.Text.Encoding.UTF8, "text/plain"),
                    StatusCode = status
                };
                base.OnException(actionExecutedContext);
            }
    
        }
    

    You can register your exception filters like the below.

     public static void Register(HttpConfiguration config)
            {           
                //.................
                config.Filters.Add(new CustomExceptionFilter());
                //..................
            }

    The following tutorials for your reference.

    1: Exception Handling in ASP.NET Web API:

    http://www.asp.net/web-api/overview/error-handling/exception-handling

    2: NET Web API 2.0 Service with a Java Client:

    http://www.codeproject.com/Articles/827669/NET-Web-API-Service-with-a-Java-Client

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 13, 2016 2:20 AM