ATCraiger
ATCraiger

Reputation: 139

Web API returns stack trace instead of my code in catch block

I'm using ASP.net 4.8, Visual Studio 2019 V 16.4.3.

I am trying to get at an uploaded file from the user and save it. I am running the code inside of a try block, catching the necessary exceptions. Right now it is throwing an exception, jumping to my catch block, and then ignoring the code inside and returning the full stack trace. Why is it doing this? I'd also love to know what I'm doing wrong in trying to access the uploaded file, but that's probably another question.

public IHttpActionResult EditBusinessLogoAndCategory(string businessID, string categoryName)
        {
try {
                var httpRequest = HttpContext.Current.Request;
                foreach (string file in httpRequest.Files)
                {
                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created);
//... more code
}
            catch (NullReferenceException ex)
            {
                return Content(HttpStatusCode.InternalServerError, new ReturnVM(ReturnVM.ReturnStatus.Failure, ex.InnerException.InnerException.Message, "Internal Error saving image."));
            }
            catch (Exception ex)
            {
                return Content(HttpStatusCode.InternalServerError, new ReturnVM(ReturnVM.ReturnStatus.Failure, ex.InnerException.InnerException.Message, "Internal Error saving image."));
            }

It jumps immediately from the foreach loop when it tries to access httpRequest.Files, to the last catch block. It stops there, then instead of returning my message, it returns this:

{ "Message": "An error has occurred.", "ExceptionMessage": "Object reference not set to an instance of an object.", "ExceptionType": "System.NullReferenceException", "StackTrace": " at Prox.Controllers.BusinessController.EditBusinessLogoAndCategory(String businessID, String categoryName) in C:\Users\aaron\Desktop\Prox\Prox_API\Prox\Controllers\BusinessController.cs:line 382\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_2.b__2(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.AuthorizationFilterAttribute.d__3.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.AuthorizationFilterAttribute.d__3.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.AuthenticationFilterResult.d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()" }

I even have the correct catch block for this, but it ignores that one. How is it skipping my catch block? Is this is a bug in Visual Studio?

Upvotes: 0

Views: 717

Answers (1)

Humberto Rodrigues
Humberto Rodrigues

Reputation: 179

you need to check if your InnerException is filled currectly...

try something like this

public IHttpActionResult EditBusinessLogoAndCategory(string businessID, string categoryName)
        {
             try {
                var httpRequest = HttpContext.Current.Request;
                foreach (string file in httpRequest.Files)
                {
                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created);
             //... more code
            }
            catch (NullReferenceException ex)
            {
                return Content(HttpStatusCode.InternalServerError, new ReturnVM(ReturnVM.ReturnStatus.Failure, ex.Message, "Internal Error saving image."));
            }
            catch (Exception ex)
            {
                return Content(HttpStatusCode.InternalServerError, new ReturnVM(ReturnVM.ReturnStatus.Failure, ex.Message, "Internal Error saving image."));
            }
        }

Upvotes: 1

Related Questions