refactor: 规范Expection处理

This commit is contained in:
xboard
2023-12-04 20:40:49 +08:00
parent aa0fe64afe
commit 0ab7dee52d
65 changed files with 625 additions and 362 deletions
+8 -7
View File
@@ -2,6 +2,7 @@
namespace App\Services;
use App\Exceptions\ApiException;
use App\Models\Coupon;
use App\Models\Order;
use Illuminate\Support\Facades\DB;
@@ -85,30 +86,30 @@ class CouponService
public function check()
{
if (!$this->coupon || !$this->coupon->show) {
abort(500, __('Invalid coupon'));
throw new ApiException(500, __('Invalid coupon'));
}
if ($this->coupon->limit_use <= 0 && $this->coupon->limit_use !== NULL) {
abort(500, __('This coupon is no longer available'));
throw new ApiException(500, __('This coupon is no longer available'));
}
if (time() < $this->coupon->started_at) {
abort(500, __('This coupon has not yet started'));
throw new ApiException(500, __('This coupon has not yet started'));
}
if (time() > $this->coupon->ended_at) {
abort(500, __('This coupon has expired'));
throw new ApiException(500, __('This coupon has expired'));
}
if ($this->coupon->limit_plan_ids && $this->planId) {
if (!in_array($this->planId, $this->coupon->limit_plan_ids)) {
abort(500, __('The coupon code cannot be used for this subscription'));
throw new ApiException(500, __('The coupon code cannot be used for this subscription'));
}
}
if ($this->coupon->limit_period && $this->period) {
if (!in_array($this->period, $this->coupon->limit_period)) {
abort(500, __('The coupon code cannot be used for this period'));
throw new ApiException(500, __('The coupon code cannot be used for this period'));
}
}
if ($this->coupon->limit_use_with_user !== NULL && $this->userId) {
if (!$this->checkLimitUseWithUser()) {
abort(500, __('The coupon can only be used :limit_use_with_user per person', [
throw new ApiException(500, __('The coupon can only be used :limit_use_with_user per person', [
'limit_use_with_user' => $this->coupon->limit_use_with_user
]));
}
+5 -6
View File
@@ -2,12 +2,11 @@
namespace App\Services;
use App\Exceptions\ApiException;
use App\Jobs\OrderHandleJob;
use App\Models\Order;
use App\Models\Plan;
use App\Models\User;
use App\Utils\CacheKey;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
class OrderService
@@ -45,7 +44,7 @@ class OrderService
]);
} catch (\Exception $e) {
DB::rollback();
abort(500, '开通失败');
throw new ApiException(500, '开通失败');
}
}
switch ((string)$order->period) {
@@ -75,12 +74,12 @@ class OrderService
if (!$this->user->save()) {
DB::rollBack();
abort(500, '开通失败');
throw new ApiException(500, '开通失败');
}
$order->status = 3;
if (!$order->save()) {
DB::rollBack();
abort(500, '开通失败');
throw new ApiException(500, '开通失败');
}
DB::commit();
@@ -93,7 +92,7 @@ class OrderService
if ($order->period === 'reset_price') {
$order->type = 4;
} else if ($user->plan_id !== NULL && $order->plan_id !== $user->plan_id && ($user->expired_at > time() || $user->expired_at === NULL)) {
if (!(int)admin_setting('plan_change_enable', 1)) abort(500, '目前不允许更改订阅,请联系客服或提交工单操作');
if (!(int)admin_setting('plan_change_enable', 1)) throw new ApiException(500, '目前不允许更改订阅,请联系客服或提交工单操作');
$order->type = 3;
if ((int)admin_setting('surplus_enable', 1)) $this->getSurplusValue($user, $order);
if ($order->surplus_amount >= $order->total_amount) {
+3 -2
View File
@@ -3,6 +3,7 @@
namespace App\Services;
use App\Exceptions\ApiException;
use App\Models\Payment;
class PaymentService
@@ -16,7 +17,7 @@ class PaymentService
{
$this->method = $method;
$this->class = '\\App\\Payments\\' . $this->method;
if (!class_exists($this->class)) abort(500, 'gate is not found');
if (!class_exists($this->class)) throw new ApiException(500, 'gate is not found');
if ($id) $payment = Payment::find($id)->toArray();
if ($uuid) $payment = Payment::where('uuid', $uuid)->first()->toArray();
$this->config = [];
@@ -32,7 +33,7 @@ class PaymentService
public function notify($params)
{
if (!$this->config['enable']) abort(500, 'gate is not enable');
if (!$this->config['enable']) throw new ApiException(500, 'gate is not enable');
return $this->payment->notify($params);
}
+3 -3
View File
@@ -1,10 +1,10 @@
<?php
namespace App\Services;
use App\Exceptions\ApiException;
use App\Jobs\SendTelegramJob;
use App\Models\User;
use \Curl\Curl;
use Illuminate\Mail\Markdown;
class TelegramService {
protected $api;
@@ -60,9 +60,9 @@ class TelegramService {
$curl->get($this->api . $method . '?' . http_build_query($params));
$response = $curl->response;
$curl->close();
if (!isset($response->ok)) abort(500, '请求失败');
if (!isset($response->ok)) throw new ApiException(500, '请求失败');
if (!$response->ok) {
abort(500, '来自TG的错误:' . $response->description);
throw new ApiException(500, '来自TG的错误:' . $response->description);
}
return $response;
}
-18
View File
@@ -2,7 +2,6 @@
namespace App\Services;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
class ThemeService
@@ -33,22 +32,5 @@ class ThemeService
} catch (\Exception $e) {
abort(500, "{$this->theme}初始化失败");
}
// $data = var_export($data, 1);
// try {
// if (!File::put(base_path() . "/config/theme/{$this->theme}.php", "<?php\n return $data ;")) {
// abort(500, "{$this->theme}初始化失败");
// }
// } catch (\Exception $e) {
// abort(500, '请检查V2Board目录权限');
// }
// try {
// Artisan::call('config:cache');
// while (true) {
// if (config("theme.{$this->theme}")) break;
// }
// } catch (\Exception $e) {
// abort(500, "{$this->theme}初始化失败");
// }
}
}
+3 -2
View File
@@ -2,6 +2,7 @@
namespace App\Services;
use App\Exceptions\ApiException;
use App\Jobs\SendEmailJob;
use App\Models\Ticket;
use App\Models\TicketMessage;
@@ -36,7 +37,7 @@ class TicketService {
$ticket = Ticket::where('id', $ticketId)
->first();
if (!$ticket) {
abort(500, '工单不存在');
throw new ApiException(500, '工单不存在');
}
$ticket->status = 0;
DB::beginTransaction();
@@ -52,7 +53,7 @@ class TicketService {
}
if (!$ticketMessage || !$ticket->save()) {
DB::rollback();
abort(500, '工单回复失败');
throw new ApiException(500, '工单回复失败');
}
DB::commit();
$this->sendEmailNotify($ticket, $ticketMessage);