Giter Site home page Giter Site logo

Comments (8)

Dresel avatar Dresel commented on June 29, 2024

Atm for AJAX results an empty json object is returned, I could also return "nothing", wouldn't matter :)

What information would you need (except of the status code which is sent anyway)? Some friendly HTTP Status Code Message? The Exception message (which can also be logged in the application)?

from easyerrorhandlingmvc.

Dresel avatar Dresel commented on June 29, 2024

And what content types should be supported? Plain text, JSON, XML, ... ?

from easyerrorhandlingmvc.

glombek avatar glombek commented on June 29, 2024

Thanks, I couldn't find the code causing this!

I was expecting the same HTML response as a non-ajax request. This is important for using AJAX with the HTML5 History API, for example.

I'd suggest not looking at the IsAjaxRequest method, perhaps respond to the Accepts header? Perhaps return HTML by default, if the header allows. If the Accepts header does not allow html/text, try the same with plaintext, JSON and XML?

I can't think of a use case where you would want a JSON 404, surely the code 404 and the error message should be enough? In this case, I'd probably always return the HTML.

from easyerrorhandlingmvc.

glombek avatar glombek commented on June 29, 2024

Is the reason it returns nothing with AJAX requests for saving bandwidth?

In this case, maybe return HTML if the Accepts header allows, otherwise return empty string.

from easyerrorhandlingmvc.

glombek avatar glombek commented on June 29, 2024

If this sounds like a sensible solution, I'll fork and pull request.

from easyerrorhandlingmvc.

Dresel avatar Dresel commented on June 29, 2024

Sounds legit, so:

Iterate through the headers
The first match sorted by quality parameter should win
If HTML, just return the ViewResult
If JSON / XML, return a simple object (HTTP Status Code, HTTP Message, Exception information**)
If there is no match, return an empty (or HTML) result? What does the HTTP specification says here :)

** If we want to return this information, we have to check IsCustomErrorEnabled so we do not expose sensible information in production mode.

from easyerrorhandlingmvc.

glombek avatar glombek commented on June 29, 2024

Sounds sensible. I'll take a look over the weekend, hopefully.

from easyerrorhandlingmvc.

Dresel avatar Dresel commented on June 29, 2024

The Controller now returns a JSON object with more information for AJAX requests:

string message = exception != null && !requestContext.HttpContext.IsCustomErrorEnabled ? exception.ToString() : string.Empty;

actionResult = new JsonResult
{
    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
    Data = new
    {
        httpStatusCode = renderedHttpStatusCode,
        httpStatusMessage = renderedHttpStatusCode.ToString(),
        message = message
    }
};

Message is only set when custom errors are disabled.

You might add your support for other content types over the weekend though :)

from easyerrorhandlingmvc.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.