Repository navigation
Gracefully handle already done ocr (#308) [Backport #308 to stable31] - #319
Conversation
* 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>
d3f49ee to
780974a
Compare
|
There was a problem hiding this comment.
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
OcrAlreadyDoneExceptionand throw it when exit code6is received - Update
OcrServiceto 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() { |
There was a problem hiding this comment.
[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.
|
|
||
| class OcrAlreadyDoneException extends Exception { | ||
| public function __construct(string $message) { | ||
| $this->message = $message; |
There was a problem hiding this comment.
The constructor overrides the exception message without calling parent::__construct($message). Consider calling the parent constructor to ensure proper exception initialization.
| $this->message = $message; | |
| parent::__construct($message); |
| } 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]); |
There was a problem hiding this comment.
[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.
| $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]); |



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