Skip to content

fix(template): skip setting status code once headers are sent - #65795

Open
MarioPym wants to merge 1 commit into
nextcloud:masterfrom
MarioPym:fix/skip-status-code-after-headers-sent
Open

MarioPym wants to merge 1 commit into
nextcloud:masterfrom
MarioPym:fix/skip-status-code-after-headers-sent

Conversation

@MarioPym

Copy link
Copy Markdown

Summary

printErrorPage() and printExceptionErrorPage() always call http_response_code(). When they are reached after output has already been sent (for example an error in the middle of a download, or an uncaught exception handled by the global exception handler), PHP logs "http_response_code(): Cannot set response code - headers already sent", which hides the real error in the log.

The status code cannot be changed at that point anyway, so this only sets it while headers can still be sent. The check lives in a small private helper used by both methods.

This is the same warning as #45835. #47971 fixed it for one caller in OC_Files; this guards the error page itself so it covers every path.

The test runs the code in a separate PHP process, because PHPUnit buffers the test output and headers are never sent inside the test itself.

How to test

  1. Create a small script that sends output before showing an error page,
    e.g. ocs-provider/demo.php:
    <?php
    require_once __DIR__ . '/../lib/base.php';
    echo "output already sent\n";
    flush();
    \OCP\Server::get(\OCP\Template\ITemplateManager::class)->printErrorPage('Demo', 'late error', 500);
  2. Open it in the browser or with curl https://your-instance/ocs-provider/demo.php.
  3. Check data/nextcloud.log:
    • Before: http_response_code(): Cannot set response code - headers already sent ... TemplateManager.php#72
    • After: no warning.
  4. The same happens with printExceptionErrorPage(new \RuntimeException('Demo'), 500).
  5. Error pages without previous output still return their status code (e.g. 503).

Unit test: tests/lib/Template/TemplateManagerTest.php

Checklist

AI (if applicable)

  • The content of this PR was partly or fully generated using AI

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Mario Alfonso Pym Mejía <pymm@hitss.com>
@MarioPym
MarioPym requested a review from a team as a code owner October 22, 2026 15:21
@MarioPym
MarioPym requested review from come-nc, icewind1991, leftybournes and provokateurin and removed request for a team October 22, 2026 15:21
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Hello there,
Thank you so much for taking the time and effort to create a pull request to our Nextcloud project.

We hope that the review process is going smooth and is helpful for you. We want to ensure your pull request is reviewed to your satisfaction. If you have a moment, our community management team would very much appreciate your feedback on your experience with this PR review process.

Your feedback is valuable to us as we continuously strive to improve our community developer experience. Please take a moment to complete our short survey by clicking on the following link: https://cloud.nextcloud.com/apps/forms/s/i9Ago4EQRZ7TWxjfmeEpPkf6

Thank you for contributing to Nextcloud and we hope to hear from you soon!

(If you believe you should not receive this message, you can add yourself to the blocklist.)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant