Question

When I get the status code 400 I handle the XMLHttpRequest object in this way (1) and it work. Anyway I suppose this code is quite specific to my use case.

I would like to be more generic like:
1) How should I handle the server error when I get the status code 500?


(1)

_.each((JSON.parse(xhr.responseText)).children, function (xhrObject, name) {
    element.find('[name="' + name + '"]')
        .parent().append($('<div>')
        .attr('class', 'is-input-invalid')
        .text(xhrObject.errors));
});
Was it helpful?

Solution

It depends on the application. There are three general things you may want to do in response to a 500:

  1. Report that an error happened.
  2. Ignore it.
  3. Try something else.

The second can be good when the operation isn't vital to the user (perhaps some background update-check they didn't even know you were doing), and is absolutely to be avoided otherwise (the only thing worse than having an error reported to you, is not having an error reported to you). The third is quite specialised (the "something else" to try will likely either be obvious, or impossible, depending on what you were trying to do in the first place).

To obtain anything useful from the 500, you will need the server-side of the code to send you something useful as the body of such messages. Just what "useful" is depends on what you are doing (e.g. plain-text, json, xml are the most likely things for the rest of your code to already be set up to deal with).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top