From 477aa691a914ebc769c36b3eac4580c7e3beece4 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 12 Apr 2021 14:36:51 +1000 Subject: [PATCH] Add custom exceptions --- app/Exceptions/FilePermissionsFailure.php | 10 ++++++++++ app/Exceptions/Handler.php | 18 ++++++++++++------ app/Exceptions/InternalPDFFailure.php | 10 ++++++++++ app/Exceptions/PhantomPDFFailure.php | 10 ++++++++++ app/Http/Controllers/SelfUpdateController.php | 17 +++++++++++++++++ app/Jobs/Entity/CreateEntityPdf.php | 16 +++++++++++++++- .../Subscription/SubscriptionService.php | 9 ++++++--- app/Utils/PhantomJS/Phantom.php | 5 +++-- app/Utils/Traits/Pdf/PdfMaker.php | 13 ++++++++++--- 9 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 app/Exceptions/FilePermissionsFailure.php create mode 100644 app/Exceptions/InternalPDFFailure.php create mode 100644 app/Exceptions/PhantomPDFFailure.php diff --git a/app/Exceptions/FilePermissionsFailure.php b/app/Exceptions/FilePermissionsFailure.php new file mode 100644 index 0000000000..760a3b2d75 --- /dev/null +++ b/app/Exceptions/FilePermissionsFailure.php @@ -0,0 +1,10 @@ +expectsJson()) { return response()->json(['message'=>$exception->getMessage()], 400); + }elseif($exception instanceof InternalPDFFailure && $request->expectsJson()){ + return response()->json(['message' => $exception->getMessage()], 500); + }elseif($exception instanceof PhantomPDFFailure && $request->expectsJson()){ + return response()->json(['message' => $exception->getMessage()], 500); + }elseif($exception instanceof FilePermissionsFailure) { + return response()->json(['message' => $exception->getMessage()], 500); } elseif ($exception instanceof ThrottleRequestsException && $request->expectsJson()) { return response()->json(['message'=>'Too many requests'], 429); } elseif ($exception instanceof FatalThrowableError && $request->expectsJson()) { @@ -152,8 +161,7 @@ class Handler extends ExceptionHandler } elseif ($exception instanceof MethodNotAllowedHttpException && $request->expectsJson()) { return response()->json(['message'=>'Method not support for this route'], 404); } elseif ($exception instanceof ValidationException && $request->expectsJson()) { - info(print_r($exception->validator->getMessageBag(), 1)); - + nlog($exception->validator->getMessageBag()); return response()->json(['message' => 'The given data was invalid.', 'errors' => $exception->validator->getMessageBag()], 422); } elseif ($exception instanceof RelationNotFoundException && $request->expectsJson()) { return response()->json(['message' => $exception->getMessage()], 400); @@ -161,9 +169,7 @@ class Handler extends ExceptionHandler return response()->json(['message' => $exception->getMessage()], 400); } elseif ($exception instanceof GenericPaymentDriverFailure) { $data['message'] = $exception->getMessage(); - //dd($data); - // return view('errors.layout', $data); - } + } return parent::render($request, $exception); } diff --git a/app/Exceptions/InternalPDFFailure.php b/app/Exceptions/InternalPDFFailure.php new file mode 100644 index 0000000000..ce85ad3c7e --- /dev/null +++ b/app/Exceptions/InternalPDFFailure.php @@ -0,0 +1,10 @@ +json(['message' => ctrans('texts.self_update_not_available')], 403); } + if(!$this->testWritable()) + throw new FilePermissionsFailure('Cannot update system because files are not writable!'); + // Check if new version is available if($updater->source()->isNewVersionAvailable()) { @@ -90,6 +94,19 @@ class SelfUpdateController extends BaseController } + private function testWritable() + { + $directoryIterator = new \RecursiveDirectoryIterator(base_path()); + + foreach (new \RecursiveIteratorIterator($directoryIterator) as $file) { + if ($file->isFile() && ! $file->isWritable()) { + return false; + } + } + + return true; + } + public function checkVersion() { return trim(file_get_contents(config('ninja.version_url'))); diff --git a/app/Jobs/Entity/CreateEntityPdf.php b/app/Jobs/Entity/CreateEntityPdf.php index edaa6776ea..79a479ac98 100644 --- a/app/Jobs/Entity/CreateEntityPdf.php +++ b/app/Jobs/Entity/CreateEntityPdf.php @@ -12,6 +12,7 @@ namespace App\Jobs\Entity; +use App\Exceptions\FilePermissionsFailure; use App\Models\Account; use App\Models\Credit; use App\Models\CreditInvitation; @@ -168,6 +169,7 @@ class CreateEntityPdf implements ShouldQueue else { $pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true)); } + } catch (\Exception $e) { nlog(print_r($e->getMessage(), 1)); } @@ -176,8 +178,20 @@ class CreateEntityPdf implements ShouldQueue info($maker->getCompiledHTML()); } + if ($pdf) { - Storage::disk($this->disk)->put($file_path, $pdf); + + try{ + + Storage::disk($this->disk)->put($file_path, $pdf); + + } + catch(\Exception $e) + { + + throw new FilePermissionsFailure('Could not write the PDF, permission issues!'); + + } } return $file_path; diff --git a/app/Services/Subscription/SubscriptionService.php b/app/Services/Subscription/SubscriptionService.php index 494e53d2b1..f372bcd7c0 100644 --- a/app/Services/Subscription/SubscriptionService.php +++ b/app/Services/Subscription/SubscriptionService.php @@ -183,7 +183,7 @@ class SubscriptionService return redirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id); } - public function calculateUpgradePrice(RecurringInvoice $recurring_invoice, Subscription $target) + public function calculateUpgradePrice(RecurringInvoice $recurring_invoice, Subscription $target) :?float { //calculate based on daily prices @@ -206,14 +206,17 @@ class SubscriptionService //user has multiple amounts outstanding return $target->price - $this->calculateProRataRefund($outstanding->first()); } - elseif ($outstanding->count > 1) { + elseif ($outstanding->count() > 1) { //user is changing plan mid frequency cycle //we cannot handle this if there are more than one invoice outstanding. + return null; } + return null; + } - private function calculateProRataRefund($invoice) + private function calculateProRataRefund($invoice) :float { //determine the start date diff --git a/app/Utils/PhantomJS/Phantom.php b/app/Utils/PhantomJS/Phantom.php index 3f2945cfbb..a9efb57941 100644 --- a/app/Utils/PhantomJS/Phantom.php +++ b/app/Utils/PhantomJS/Phantom.php @@ -11,6 +11,7 @@ namespace App\Utils\PhantomJS; +use App\Exceptions\PhantomPDFFailure; use App\Jobs\Util\SystemLogger; use App\Models\CreditInvitation; use App\Models\Design; @@ -91,8 +92,6 @@ class Phantom $instance = Storage::disk(config('filesystems.default'))->put($file_path, $pdf); -// nlog($instance); -// nlog($file_path); return $file_path; } @@ -128,6 +127,8 @@ class Phantom SystemLog::TYPE_PDF_FAILURE, $invitation->contact->client ); + + throw new PhantomPDFFailure('There was an error generating the PDF with Phantom JS'); } else { diff --git a/app/Utils/Traits/Pdf/PdfMaker.php b/app/Utils/Traits/Pdf/PdfMaker.php index 95343e4257..0d26f5eff0 100644 --- a/app/Utils/Traits/Pdf/PdfMaker.php +++ b/app/Utils/Traits/Pdf/PdfMaker.php @@ -12,6 +12,7 @@ namespace App\Utils\Traits\Pdf; +use App\Exceptions\InternalPDFFailure; use Beganovich\Snappdf\Snappdf; trait PdfMaker @@ -33,8 +34,14 @@ trait PdfMaker $pdf->setChromiumPath(config('ninja.snappdf_chromium_path')); } - return $pdf - ->setHtml($html) - ->generate(); + $generated = $pdf + ->setHtml($html) + ->generate(); + + if($generated) + return $generated; + + + throw new InternalPDFFailure('There was an issue generating the PDF locally'); } }