3

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".

7
  • Check the console for the error. Also check the response text of the request there too as it should give you the exception Commented Jan 4, 2018 at 13:13
  • ResponseText is "Bad Request" as you can see. Commented Jan 4, 2018 at 13:18
  • this answer 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? Commented Jan 4, 2018 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. Commented Jan 4, 2018 at 13:25
  • look into this and this. I guess its one of those settings messing with the custom errors behavior of IIS. Commented Jan 4, 2018 at 13:30

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
I tried putting a very big text and I got [ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: value]
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");
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.