Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding more dedicated exceptions #739

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Smalot/PdfParser/Exception/EmptyPdfException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types = 1);

namespace Smalot\PdfParser\Exception;

class EmptyPdfException extends \Exception
{
}
7 changes: 7 additions & 0 deletions src/Smalot/PdfParser/Exception/MissingPdfHeaderException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types = 1);

namespace Smalot\PdfParser\Exception;

class MissingPdfHeaderException extends \Exception
{
}
10 changes: 6 additions & 4 deletions src/Smalot/PdfParser/RawData/RawDataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
namespace Smalot\PdfParser\RawData;

use Smalot\PdfParser\Config;
use Smalot\PdfParser\Exception\MissingPdfHeaderException;
use Smalot\PdfParser\Exception\EmptyPdfException;

class RawDataParser
{
Expand Down Expand Up @@ -938,17 +940,17 @@ protected function getXrefData(string $pdfData, int $offset = 0, array $xref = [
*
* @return array array of parsed PDF document objects
*
* @throws \Exception if empty PDF data given
* @throws \Exception if PDF data missing %PDF header
* @throws EmptyPdfException if empty PDF data given
* @throws MissingPdfHeaderException if PDF data missing `%PDF-` header
*/
public function parseData(string $data): array
{
if (empty($data)) {
throw new \Exception('Empty PDF data given.');
throw new EmptyPdfException('Empty PDF data given.');
}
// find the pdf header starting position
if (false === ($trimpos = strpos($data, '%PDF-'))) {
throw new \Exception('Invalid PDF data: missing %PDF header.');
throw new MissingPdfHeaderException('Invalid PDF data: Missing `%PDF-` header.');
}

// get PDF content string
Expand Down
Loading