1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

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;
use App\Exceptions\FilePermissionsFailure;
use App\Exceptions\InternalPDFFailure;
use App\Exceptions\PhantomPDFFailure;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
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);
}
@ -134,6 +137,12 @@ class Handler extends ExceptionHandler
{
if ($exception instanceof ModelNotFoundException && $request->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);
}

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;
use App\Exceptions\FilePermissionsFailure;
use App\Utils\Ninja;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Facades\Artisan;
@ -61,6 +62,9 @@ class SelfUpdateController extends BaseController
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
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')));

View File

@ -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;

View File

@ -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

View File

@ -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 {

View File

@ -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');
}
}