Fix error handling for duplicate ebook placeholders

This commit is contained in:
Alex Cabal 2025-01-09 14:00:50 -06:00
parent 047a76b788
commit d9fe3e9303
3 changed files with 17 additions and 15 deletions

View file

@ -0,0 +1,7 @@
<?
namespace Exceptions;
class EbookPlaceholderExistsException extends AppException{
/** @var string $message */
protected $message = 'An ebook placeholder for this book already exists.';
}

View file

@ -3,8 +3,6 @@
* @var ?Exception $exception * @var ?Exception $exception
*/ */
use Exceptions\AppException;
if($exception === null){ if($exception === null){
return; return;
} }
@ -26,7 +24,7 @@ else{
$message = 'An error occurred.'; $message = 'An error occurred.';
} }
if(!($ex instanceof AppException) || $ex->MessageType == Enums\ExceptionMessageType::Text){ if(!($ex instanceof Exceptions\AppException) || $ex->MessageType == Enums\ExceptionMessageType::Text){
$message = '<p>' . str_replace('CAPTCHA', '<abbr class="acronym">CAPTCHA</abbr>', Formatter::EscapeHtml($message)) . '</p>'; $message = '<p>' . str_replace('CAPTCHA', '<abbr class="acronym">CAPTCHA</abbr>', Formatter::EscapeHtml($message)) . '</p>';
} }
?> ?>

View file

@ -35,17 +35,14 @@ try{
$ebook->Create(); $ebook->Create();
} }
catch(Exceptions\DuplicateEbookException $ex){ catch(Exceptions\DuplicateEbookException $ex){
// If the identifier already exists but a `Project` was sent with this request, create the `Project` anyway. $ebook = Ebook::GetByIdentifier($ebook->Identifier);
$existingEbook = Ebook::GetByIdentifier($ebook->Identifier);
if($ebook->EbookPlaceholder?->IsInProgress && $existingEbook->ProjectInProgress === null && $project !== null){ // An existing `EbookPlaceholder` already exists.
$ebook->EbookId = $existingEbook->EbookId; $ex = new Exceptions\EbookPlaceholderExistsException('An ebook placeholder already exists for this book: <a href="' . $ebook->Url . '">' . Formatter::EscapeHtml($ebook->Title) . '</a>.');
$_SESSION['is-only-ebook-project-created'] = true;
} $ex->MessageType = Enums\ExceptionMessageType::Html;
else{
// The existing ebook already has a `Project`, throw the exception and really fail. throw $ex;
$ebook = $existingEbook;
throw new Exceptions\ProjectExistsException('This ebook already exists, and already has an in-progress project.');
}
} }
if($ebook->EbookPlaceholder?->IsInProgress && $project !== null){ if($ebook->EbookPlaceholder?->IsInProgress && $project !== null){
@ -98,7 +95,7 @@ catch(Exceptions\LoginRequiredException){
catch(Exceptions\InvalidPermissionsException | Exceptions\InvalidHttpMethodException | Exceptions\HttpMethodNotAllowedException){ catch(Exceptions\InvalidPermissionsException | Exceptions\InvalidHttpMethodException | Exceptions\HttpMethodNotAllowedException){
Template::ExitWithCode(Enums\HttpCode::Forbidden); Template::ExitWithCode(Enums\HttpCode::Forbidden);
} }
catch(Exceptions\InvalidEbookException | Exceptions\ProjectExistsException | Exceptions\InvalidProjectException $ex){ catch(Exceptions\InvalidEbookException | Exceptions\EbookPlaceholderExistsException | Exceptions\InvalidProjectException $ex){
$_SESSION['ebook'] = $ebook; $_SESSION['ebook'] = $ebook;
$_SESSION['exception'] = $ex; $_SESSION['exception'] = $ex;