To reproduce:
1. Create an AJAX request to your drupal application
2. When handling the AJAX request, cause a TypeError

Expected behaviour:
The AJAX request returns with a 500 (bad request) http status code

Actual behaviour:
The AJAX request returns with a 200 (ok) http status code

Context:
Ubuntu 16.04.2 LTS
Php 7.1.5
Apache 2.4.18

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

PtrTon created an issue. See original summary.

cilefen’s picture

Status: Active » Needs review
Issue tags: -php error, -ajax form, -http error 500, -ajax http error 200
cilefen’s picture

jsst’s picture

Status: Needs review » Needs work

This patch does not work as advertised: it only sends a 500 status code when Drupal is configured to display errors. I'd expect a 500 response regardless of the error level.

The patch in #2688999 seems to take care of this point, so in combination these two should work. Note the patch in #2688999 by itself is not enough to deal with the issue describe here. For example:

http_response_code(500);
$response = new Response();
$response->send();

Above snippet will result in a 200 OK response message.

PtrTon’s picture

cilefen’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 5: ajax_error_handling-1.patch, failed testing. View results

PtrTon’s picture

PtrTon’s picture

Status: Needs work » Needs review
jsst’s picture

Status: Needs review » Needs work

The new patch combines the fix for AJAX error handling with the patch from #2688999. Since that one has the tag "needs work", it's only fair to say this one needs work too :)

I don't think we should combine those patches because they are separate issues. Why not try to fix the error handling specifically for AJAX requests, something like:

  if (\Drupal::hasRequest() && \Drupal::request()->isXmlHttpRequest()) {
    if ($fatal) {
+     http_response_code(500);

      if (error_displayable($error)) {
        // When called from JavaScript, simply output the error message.
        // Should not translate the string to avoid errors producing more errors.
        $response->setContent(SafeMarkup::format('%type: @message in %function (line %line of %file).', $error));
+       $response->setStatusCode(500);
        $response->send();
      }
      exit;
    }

That way, this patch could be merged into core without depending on #2688999.

PtrTon’s picture

Agreed with jsst, let's keep the scope of this issue to AJAX requests only. Updated patch accordingly.

PtrTon’s picture

Status: Needs work » Needs review
jsst’s picture

Status: Needs review » Reviewed & tested by the community
cilefen’s picture

Title: PHP TypeError during AJAX request resolves to a 200 http status code » XML HTTP error responses set a 200 error code instead of a 500
Status: Reviewed & tested by the community » Postponed

Thank you, everyone, for working on this. I think this work should be postponed on #2688999: Default all responses to 500 before any code is run so PHP will throw 500's on FATAL errors because setting http_response_code(500) will be redundant after #2688999 is finished.

I am surprised the #11 patch applies because it is targeting a top-level web/ directory that does not exist in Drupal core.

@jsst For your reference, please read about the core gates for reviewing purposes. In particular, this one would need a test.

Version: 8.3.4 » 8.3.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

Version: 8.3.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

alexpott’s picture

Status: Postponed » Needs work

The related issue #2688999: Default all responses to 500 before any code is run so PHP will throw 500's on FATAL errors has got stuck. I think we should continue here as regardless of what happens in #2688999: Default all responses to 500 before any code is run so PHP will throw 500's on FATAL errors fixing this particular bug is a good idea.

alexpott’s picture

Here's a fix plus a test so we don't break this in the future.

vijaycs85’s picture

Status: Needs review » Reviewed & tested by the community

+1

+++ b/core/includes/errors.inc
@@ -199,11 +199,15 @@ function _drupal_log_error($error, $fatal = FALSE) {
+      else {
+        $response->setStatusCode(500);
+      }
+      $response->send();

good to send the response back, if the error display isn't on.

The last submitted patch, 19: 2892370-19.test-only.patch, failed testing. View results

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 19: 2892370-19.patch, failed testing. View results

alexpott’s picture

Status: Needs work » Reviewed & tested by the community

Pretty sure the Drupal\Tests\field_layout\FunctionalJavascript\FieldLayoutTest fail is regular random JS test fail. Passes locally.

Version: 9.2.x-dev » 9.3.x-dev

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 19: 2892370-19.patch, failed testing. View results

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

gstivanin’s picture

Having the same issue on core 10.1.0 and patch #19 it's not applying, so rerolling this patch to apply on 10.1.0

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.