Add custom exceptions

This commit is contained in:
David Bomba 2021-04-12 14:36:51 +10:00
parent faa466e5c0
commit 477aa691a9
9 changed files with 93 additions and 15 deletions

View File

@ -0,0 +1,10 @@
<?php
namespace App\Exceptions;
use Exception;
class FilePermissionsFailure extends Exception
{
// ..
}

View File

@ -11,6 +11,9 @@
namespace App\Exceptions; namespace App\Exceptions;
use App\Exceptions\FilePermissionsFailure;
use App\Exceptions\InternalPDFFailure;
use App\Exceptions\PhantomPDFFailure;
use Exception; use Exception;
use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException; use Illuminate\Auth\AuthenticationException;
@ -94,7 +97,7 @@ class Handler extends ExceptionHandler
} }
} }
if(config('ninja.expanded_logging')) // if(config('ninja.expanded_logging'))
parent::report($exception); parent::report($exception);
} }
@ -134,6 +137,12 @@ class Handler extends ExceptionHandler
{ {
if ($exception instanceof ModelNotFoundException && $request->expectsJson()) { if ($exception instanceof ModelNotFoundException && $request->expectsJson()) {
return response()->json(['message'=>$exception->getMessage()], 400); 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()) { } elseif ($exception instanceof ThrottleRequestsException && $request->expectsJson()) {
return response()->json(['message'=>'Too many requests'], 429); return response()->json(['message'=>'Too many requests'], 429);
} elseif ($exception instanceof FatalThrowableError && $request->expectsJson()) { } elseif ($exception instanceof FatalThrowableError && $request->expectsJson()) {
@ -152,8 +161,7 @@ class Handler extends ExceptionHandler
} elseif ($exception instanceof MethodNotAllowedHttpException && $request->expectsJson()) { } elseif ($exception instanceof MethodNotAllowedHttpException && $request->expectsJson()) {
return response()->json(['message'=>'Method not support for this route'], 404); return response()->json(['message'=>'Method not support for this route'], 404);
} elseif ($exception instanceof ValidationException && $request->expectsJson()) { } 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); return response()->json(['message' => 'The given data was invalid.', 'errors' => $exception->validator->getMessageBag()], 422);
} elseif ($exception instanceof RelationNotFoundException && $request->expectsJson()) { } elseif ($exception instanceof RelationNotFoundException && $request->expectsJson()) {
return response()->json(['message' => $exception->getMessage()], 400); return response()->json(['message' => $exception->getMessage()], 400);
@ -161,8 +169,6 @@ class Handler extends ExceptionHandler
return response()->json(['message' => $exception->getMessage()], 400); return response()->json(['message' => $exception->getMessage()], 400);
} elseif ($exception instanceof GenericPaymentDriverFailure) { } elseif ($exception instanceof GenericPaymentDriverFailure) {
$data['message'] = $exception->getMessage(); $data['message'] = $exception->getMessage();
//dd($data);
// return view('errors.layout', $data);
} }
return parent::render($request, $exception); return parent::render($request, $exception);

View File

@ -0,0 +1,10 @@
<?php
namespace App\Exceptions;
use Exception;
class InternalPDFFailure extends Exception
{
// ..
}

View File

@ -0,0 +1,10 @@
<?php
namespace App\Exceptions;
use Exception;
class PhantomPDFFailure extends Exception
{
// ..
}

View File

@ -11,6 +11,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Exceptions\FilePermissionsFailure;
use App\Utils\Ninja; use App\Utils\Ninja;
use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Artisan;
@ -61,6 +62,9 @@ class SelfUpdateController extends BaseController
return response()->json(['message' => ctrans('texts.self_update_not_available')], 403); return response()->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 // Check if new version is available
if($updater->source()->isNewVersionAvailable()) { 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() public function checkVersion()
{ {
return trim(file_get_contents(config('ninja.version_url'))); return trim(file_get_contents(config('ninja.version_url')));

View File

@ -12,6 +12,7 @@
namespace App\Jobs\Entity; namespace App\Jobs\Entity;
use App\Exceptions\FilePermissionsFailure;
use App\Models\Account; use App\Models\Account;
use App\Models\Credit; use App\Models\Credit;
use App\Models\CreditInvitation; use App\Models\CreditInvitation;
@ -168,6 +169,7 @@ class CreateEntityPdf implements ShouldQueue
else { else {
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true)); $pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
} }
} catch (\Exception $e) { } catch (\Exception $e) {
nlog(print_r($e->getMessage(), 1)); nlog(print_r($e->getMessage(), 1));
} }
@ -176,8 +178,20 @@ class CreateEntityPdf implements ShouldQueue
info($maker->getCompiledHTML()); info($maker->getCompiledHTML());
} }
if ($pdf) { if ($pdf) {
try{
Storage::disk($this->disk)->put($file_path, $pdf); Storage::disk($this->disk)->put($file_path, $pdf);
}
catch(\Exception $e)
{
throw new FilePermissionsFailure('Could not write the PDF, permission issues!');
}
} }
return $file_path; return $file_path;

View File

@ -183,7 +183,7 @@ class SubscriptionService
return redirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id); 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 //calculate based on daily prices
@ -206,14 +206,17 @@ class SubscriptionService
//user has multiple amounts outstanding //user has multiple amounts outstanding
return $target->price - $this->calculateProRataRefund($outstanding->first()); return $target->price - $this->calculateProRataRefund($outstanding->first());
} }
elseif ($outstanding->count > 1) { elseif ($outstanding->count() > 1) {
//user is changing plan mid frequency cycle //user is changing plan mid frequency cycle
//we cannot handle this if there are more than one invoice outstanding. //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 //determine the start date

View File

@ -11,6 +11,7 @@
namespace App\Utils\PhantomJS; namespace App\Utils\PhantomJS;
use App\Exceptions\PhantomPDFFailure;
use App\Jobs\Util\SystemLogger; use App\Jobs\Util\SystemLogger;
use App\Models\CreditInvitation; use App\Models\CreditInvitation;
use App\Models\Design; use App\Models\Design;
@ -91,8 +92,6 @@ class Phantom
$instance = Storage::disk(config('filesystems.default'))->put($file_path, $pdf); $instance = Storage::disk(config('filesystems.default'))->put($file_path, $pdf);
// nlog($instance);
// nlog($file_path);
return $file_path; return $file_path;
} }
@ -128,6 +127,8 @@ class Phantom
SystemLog::TYPE_PDF_FAILURE, SystemLog::TYPE_PDF_FAILURE,
$invitation->contact->client $invitation->contact->client
); );
throw new PhantomPDFFailure('There was an error generating the PDF with Phantom JS');
} }
else { else {

View File

@ -12,6 +12,7 @@
namespace App\Utils\Traits\Pdf; namespace App\Utils\Traits\Pdf;
use App\Exceptions\InternalPDFFailure;
use Beganovich\Snappdf\Snappdf; use Beganovich\Snappdf\Snappdf;
trait PdfMaker trait PdfMaker
@ -33,8 +34,14 @@ trait PdfMaker
$pdf->setChromiumPath(config('ninja.snappdf_chromium_path')); $pdf->setChromiumPath(config('ninja.snappdf_chromium_path'));
} }
return $pdf $generated = $pdf
->setHtml($html) ->setHtml($html)
->generate(); ->generate();
if($generated)
return $generated;
throw new InternalPDFFailure('There was an issue generating the PDF locally');
} }
} }