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
*/
use Exceptions\AppException;
if($exception === null){
return;
}
@ -26,7 +24,7 @@ else{
$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>';
}
?>

View file

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