2

I'm having a problem sending a custom message on error to an ajax call. My controller returns something like this:

return new HttpStatusCodeResult(400, "My error!");

And my ajax code looks like this:

error: function (xhr, httpStatusMessage) {
              console.log(xhr.statusText);
              console.log(httpStatusMessage);
}

The problem is that xhr.statusCode and httpStatusMessage is always "error". What am I doing wrong now? I'm expecting to have "My error!" in xhr.statusText.

I'm using ASP.NET MVC 5 and jquery-1.10.2

My xhr output is:

abort:ƒ ( statusText )
always:ƒ ()
complete:ƒ ()
done:ƒ ()
error:ƒ ()
fail:ƒ ()
getAllResponseHeaders:ƒ ()
getResponseHeader:ƒ ( key )
overrideMimeType:ƒ ( type )
pipe:ƒ ( /* fnDone, fnFail, fnProgress */ )
progress:ƒ ()
promise:ƒ ( obj )
readyState:4
responseText:"Bad Request"
setRequestHeader:ƒ ( name, value )
state:ƒ ()
status:400
statusCode:ƒ ( map )
statusText:"error"
success:ƒ ()
then:ƒ ( /* fnDone, fnFail, fnProgress */ )

My Web.config httpErrors configuration looks like this:

<httpErrors existingResponse="PassThrough" errorMode="Custom">
      <remove statusCode="404" />
      <error statusCode="404" path="/Error/NotFound" responseMode="ExecuteURL" />
      <remove statusCode="403" />
      <error statusCode="403" path="/Error/Forbidden" responseMode="ExecuteURL" />
    </httpErrors>

and still, on my dev environment, the responseText is empty and the statusText is just "error".

Robert
  • 255
  • 2
  • 15
  • 37
  • Check the console for the error. Also check the response text of the request there too as it should give you the exception – Rory McCrossan Jan 04 '18 at 13:13
  • ResponseText is "Bad Request" as you can see. – Robert Jan 04 '18 at 13:18
  • [this answer](https://stackoverflow.com/a/37311074/1132334) is suggesting that you should send an empty `ContentResult` with the custom error message written to the response body. could you show more of what you're doing in the controller part? – Cee McSharpface Jan 04 '18 at 13:23
  • I tried all of that and it still doesn't work. The thing is that I uploaded on azure and there I get the custom message just right so the problem is on my local dev environment. – Robert Jan 04 '18 at 13:25
  • look into [this](https://stackoverflow.com/q/31040671/1132334) and [this](https://gist.github.com/remi/929007). I guess its one of those settings messing with the custom errors behavior of IIS. – Cee McSharpface Jan 04 '18 at 13:30
  • related: https://stackoverflow.com/q/31287090/1132334 – Cee McSharpface Jan 05 '18 at 11:31
  • if it was not for the iis version and mvc tags, I'd be tempted to mark this one as a duplicate of https://stackoverflow.com/questions/434272/iis7-overrides-customerrors-when-setting-response-statuscode – Cee McSharpface Jan 05 '18 at 12:02

1 Answers1

7

You need to set a property in your Web.Config file.

Citing a user of this web page on github, emphasis mine,

By default IIS will mask your error codes and replace them with default errors. The "PassThrough" option tells IIS to leave your custom errors alone and render them as-is.

"Bad Request" is the default http error text for the status code 400.

So there is the setting as documented here and outlined here,

<configuration>
  <system.webServer>
    <httpErrors existingResponse="PassThrough"></httpErrors>
  </system.webServer>
</configuration>

Consult the documentation carefully for your version of IIS, there is lots of subtle version differences.

EDIT

Not really specific to MVC, but it is how I once solved it (part of production code), and what seems to have helped OP as well:

Response.TrySkipIisCustomErrors = true;
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
Response.ContentType = "text/plain";
Response.Write(new String('_', 513) + "my custom message");

This absurd minimum character limit thing may or may not be needed depending on IIS version. I would too be grateful if somebody could shed a little more light on this underdocumented behavior.

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
  • I had this problem before. Now I remembered that part of the solution was a minimum character limit. Sounds weird, but can you try to make your custom error message longer than 512 characters? just pad with any non-whitespace. – Cee McSharpface Jan 05 '18 at 09:30
  • I tried putting a very big text and I got [ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: value] – Robert Jan 05 '18 at 11:24
  • argh. where? in the `HttpStatusCodeResult` constuctor? I looked up my code where I had this and it does not really apply here unfortunately (not an MVC controller), but here goes: `context.Response.Write(new String('_', 513) + "my custom error text");` – Cee McSharpface Jan 05 '18 at 11:34
  • 2
    Write a response with the following code in controller: Response.TrySkipIisCustomErrors = true; Response.StatusCode = (int)HttpStatusCode.BadRequest; return Content("message"); this is how I got it to work and it's based on your help. – Robert Jan 05 '18 at 11:40