Skip to content

Gracefully handle already done ocr (#308) [Backport #308 to stable31] - #319

Merged
R0Wi merged 2 commits into
stable31from
backport-308-to-stable31
Jul 6, 2025
Merged

R0Wi merged 2 commits into
stable31from
backport-308-to-stable31

Conversation

@R0Wi

@R0Wi R0Wi commented Jul 6, 2025

Copy link
Copy Markdown
Contributor
  • Gracefully handle already done ocr

Throw OcrResultEmptyException if OCRmyPDF returns exit code 6 (ExitCode.already_done_ocr).
Fix of #293

  • Update composer.lock to get latest OCP

  • Update psalm baseline

  • Introduce OcrAlreadyDoneException and add tests

  • Add additional integration tests for new result code handling


XueSheng-GIT and others added 2 commits July 6, 2025 20:18
* Gracefully handle already done ocr

Throw OcrResultEmptyException if OCRmyPDF returns exit code 6
(ExitCode.already_done_ocr).
Fix of #293

Signed-off-by: XueSheng-GIT <Michael.Frase@gmx.de>

* Update composer.lock to get latest OCP

* Update psalm baseline

* Introduce OcrAlreadyDoneException and add tests

* Add additional integration tests for new result code handling

---------

Signed-off-by: XueSheng-GIT <Michael.Frase@gmx.de>
Co-authored-by: Robin Windey <ro.windey@gmail.com>
@R0Wi
R0Wi force-pushed the backport-308-to-stable31 branch from d3f49ee to 780974a Compare July 6, 2025 20:23
@R0Wi
R0Wi requested a review from Copilot July 6, 2025 20:25
@sonarqubecloud

sonarqubecloud Bot commented Jul 6, 2025

Copy link
Copy Markdown

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for gracefully handling OCR requests that have already been completed by introducing a dedicated exception, updating core services to catch it, and expanding test coverage.

  • Introduce OcrAlreadyDoneException and throw it when exit code 6 is received
  • Update OcrService to catch and handle the new exception under skip-file mode
  • Add unit and integration tests to cover the new exception path and reset test baselines

Reviewed Changes

Copilot reviewed 16 out of 33 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
lib/Exception/OcrAlreadyDoneException.php New exception class for already-done OCR
lib/Service/OcrService.php Catch OcrAlreadyDoneException instead of empty-result exception
lib/OcrProcessors/Remote/WorkflowOcrRemoteProcessor.php Throw OcrAlreadyDoneException on exit code 6
lib/OcrProcessors/Local/OcrMyPdfBasedProcessor.php Throw OcrAlreadyDoneException on exit code 6
lib/OcrProcessors/OcrProcessorFactory.php Register processors with updated handlers
tests/Unit/Service/OcrServiceTest.php Update mock throws and add test for non-skip mode exception
tests/Unit/OcrProcessors/Remote/WorkflowOcrRemoteProcessorTest.php Add test for exit-code-6 handling
tests/Unit/OcrProcessors/Local/PdfOcrProcessorTest.php Add test for exit-code-6 handling in local processor
tests/Integration/OcrBackendServiceTest.php Add integration test for skip-file behavior
tests/Integration/LocalBackendTest.php Add integration tests for skip-file and skip-text modes
tests/Integration/IntegrationTestApiClient.php Add reset() to clear test state
tests/Integration/BackendTestBase.php Extend addOperation signature and clear operation cache
tests/psalm-baseline.xml Remove obsolete Psalm entry
Comments suppressed due to low confidence (1)

tests/Integration/LocalBackendTest.php:119

  • The assertion message says 'Expected no TextRecognizedEvent' but you are asserting a count of 1. Update the message to reflect that one event is expected.
		$this->assertEquals(1, count($this->capturedEvents), 'Expected no TextRecognizedEvent');

$processor->ocrFile($this->fileBefore, $workflowSettings, $this->defaultGlobalSettings);
}

public function testThrowsOcrAlreadyDoneExceptionIfErrorCodeIsEquals6() {

Copilot AI Jul 6, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider using $this->expectException(OcrAlreadyDoneException::class) at the start of this test instead of the manual try-catch and flag — it improves readability and aligns with other tests.

Copilot uses AI. Check for mistakes.

class OcrAlreadyDoneException extends Exception {
public function __construct(string $message) {
$this->message = $message;

Copilot AI Jul 6, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constructor overrides the exception message without calling parent::__construct($message). Consider calling the parent constructor to ensure proper exception initialization.

Suggested change
$this->message = $message;
parent::__construct($message);

Copilot uses AI. Check for mistakes.
} catch (OcrAlreadyDoneException $ex) {
// #232: Skip file if OCR was already done
if ($settings->getOcrMode() === WorkflowSettings::OCR_MODE_SKIP_FILE) {
$this->logger->debug('Skipping empty OCR result for file with id {fileId} because OCR mode is set to \'skip file\'', ['fileId' => $fileId]);

Copilot AI Jul 6, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The debug log still references an 'empty OCR result', which could be misleading now that this is handling the 'already done' case. Consider updating the log message to mention the already-done scenario.

Suggested change
$this->logger->debug('Skipping empty OCR result for file with id {fileId} because OCR mode is set to \'skip file\'', ['fileId' => $fileId]);
$this->logger->debug('Skipping file with id {fileId} because OCR has already been performed and OCR mode is set to \'skip file\'', ['fileId' => $fileId]);

Copilot uses AI. Check for mistakes.
@R0Wi
R0Wi merged commit 4e9e708 into stable31 Jul 6, 2025
@R0Wi
R0Wi deleted the backport-308-to-stable31 branch July 6, 2025 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants