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

View File

@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\User;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Models\Payment;
use App\Utils\Dict;
@@ -33,7 +34,7 @@ class CommController extends Controller
$payment = Payment::where('id', $request->input('id'))
->where('payment', 'StripeCredit')
->first();
if (!$payment) abort(500, 'payment is not found');
if (!$payment) throw new ApiException(500, 'payment is not found');
return response([
'data' => $payment->config['stripe_pk_live']
]);

View File

@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\User;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Services\CouponService;
use Illuminate\Http\Request;
@@ -11,7 +12,7 @@ class CouponController extends Controller
public function check(Request $request)
{
if (empty($request->input('code'))) {
abort(500, __('Coupon cannot be empty'));
throw new ApiException(500, __('Coupon cannot be empty'));
}
$couponService = new CouponService($request->input('code'));
$couponService->setPlanId($request->input('plan_id'));

View File

@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\User;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Models\CommissionLog;
use App\Models\InviteCode;
@@ -15,7 +16,7 @@ class InviteController extends Controller
public function save(Request $request)
{
if (InviteCode::where('user_id', $request->user['id'])->where('status', 0)->count() >= admin_setting('invite_gen_limit', 5)) {
abort(500, __('The maximum number of creations has been reached'));
throw new ApiException(500, __('The maximum number of creations has been reached'));
}
$inviteCode = new InviteCode();
$inviteCode->user_id = $request->user['id'];

View File

@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\User;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Models\Knowledge;
use App\Models\User;
@@ -18,7 +19,7 @@ class KnowledgeController extends Controller
->where('show', 1)
->first()
->toArray();
if (!$knowledge) abort(500, __('Article does not exist'));
if (!$knowledge) throw new ApiException(500, __('Article does not exist'));
$user = User::find($request->user['id']);
$userService = new UserService();
if (!$userService->isAvailable($user)) {

View File

@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\User;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Requests\User\OrderSave;
use App\Models\Order;
@@ -53,12 +54,12 @@ class OrderController extends Controller
->where('trade_no', $request->input('trade_no'))
->first();
if (!$order) {
abort(500, __('Order does not exist or has been paid'));
throw new ApiException(500, __('Order does not exist or has been paid'));
}
$order['plan'] = Plan::find($order->plan_id);
$order['try_out_plan_id'] = (int)admin_setting('try_out_plan_id');
if (!$order['plan']) {
abort(500, __('Subscription plan does not exist'));
throw new ApiException(500, __('Subscription plan does not exist'));
}
if ($order->surplus_order_ids) {
$order['surplus_orders'] = Order::whereIn('id', $order->surplus_order_ids)->get();
@@ -72,7 +73,7 @@ class OrderController extends Controller
{
$userService = new UserService();
if ($userService->isNotCompleteOrderByUserId($request->user['id'])) {
abort(500, __('You have an unpaid or pending order, please try again later or cancel it'));
throw new ApiException(500, __('You have an unpaid or pending order, please try again later or cancel it'));
}
$planService = new PlanService($request->input('plan_id'));
@@ -81,36 +82,36 @@ class OrderController extends Controller
$user = User::find($request->user['id']);
if (!$plan) {
abort(500, __('Subscription plan does not exist'));
throw new ApiException(500, __('Subscription plan does not exist'));
}
if ($user->plan_id !== $plan->id && !$planService->haveCapacity() && $request->input('period') !== 'reset_price') {
abort(500, __('Current product is sold out'));
throw new ApiException(500, __('Current product is sold out'));
}
if ($plan[$request->input('period')] === NULL) {
abort(500, __('This payment period cannot be purchased, please choose another period'));
throw new ApiException(500, __('This payment period cannot be purchased, please choose another period'));
}
if ($request->input('period') === 'reset_price') {
if (!$userService->isAvailable($user) || $plan->id !== $user->plan_id) {
abort(500, __('Subscription has expired or no active subscription, unable to purchase Data Reset Package'));
throw new ApiException(500, __('Subscription has expired or no active subscription, unable to purchase Data Reset Package'));
}
}
if ((!$plan->show && !$plan->renew) || (!$plan->show && $user->plan_id !== $plan->id)) {
if ($request->input('period') !== 'reset_price') {
abort(500, __('This subscription has been sold out, please choose another subscription'));
throw new ApiException(500, __('This subscription has been sold out, please choose another subscription'));
}
}
if (!$plan->renew && $user->plan_id == $plan->id && $request->input('period') !== 'reset_price') {
abort(500, __('This subscription cannot be renewed, please change to another subscription'));
throw new ApiException(500, __('This subscription cannot be renewed, please change to another subscription'));
}
if (!$plan->show && $plan->renew && !$userService->isAvailable($user)) {
abort(500, __('This subscription has expired, please change to another subscription'));
throw new ApiException(500, __('This subscription has expired, please change to another subscription'));
}
DB::beginTransaction();
@@ -126,7 +127,7 @@ class OrderController extends Controller
$couponService = new CouponService($request->input('coupon_code'));
if (!$couponService->use($order)) {
DB::rollBack();
abort(500, __('Coupon failed'));
throw new ApiException(500, __('Coupon failed'));
}
$order->coupon_id = $couponService->getId();
}
@@ -141,14 +142,14 @@ class OrderController extends Controller
if ($remainingBalance > 0) {
if (!$userService->addBalance($order->user_id, - $order->total_amount)) {
DB::rollBack();
abort(500, __('Insufficient balance'));
throw new ApiException(500, __('Insufficient balance'));
}
$order->balance_amount = $order->total_amount;
$order->total_amount = 0;
} else {
if (!$userService->addBalance($order->user_id, - $user->balance)) {
DB::rollBack();
abort(500, __('Insufficient balance'));
throw new ApiException(500, __('Insufficient balance'));
}
$order->balance_amount = $user->balance;
$order->total_amount = $order->total_amount - $user->balance;
@@ -157,7 +158,7 @@ class OrderController extends Controller
if (!$order->save()) {
DB::rollback();
abort(500, __('Failed to create order'));
throw new ApiException(500, __('Failed to create order'));
}
DB::commit();
@@ -176,26 +177,26 @@ class OrderController extends Controller
->where('status', 0)
->first();
if (!$order) {
abort(500, __('Order does not exist or has been paid'));
throw new ApiException(500, __('Order does not exist or has been paid'));
}
// free process
if ($order->total_amount <= 0) {
$orderService = new OrderService($order);
if (!$orderService->paid($order->trade_no)) abort(500, '');
if (!$orderService->paid($order->trade_no)) throw new ApiException(500, '');
return response([
'type' => -1,
'data' => true
]);
}
$payment = Payment::find($method);
if (!$payment || $payment->enable !== 1) abort(500, __('Payment method is not available'));
if (!$payment || $payment->enable !== 1) throw new ApiException(500, __('Payment method is not available'));
$paymentService = new PaymentService($payment->payment, $payment->id);
$order->handling_amount = NULL;
if ($payment->handling_fee_fixed || $payment->handling_fee_percent) {
$order->handling_amount = round(($order->total_amount * ($payment->handling_fee_percent / 100)) + $payment->handling_fee_fixed);
}
$order->payment_id = $method;
if (!$order->save()) abort(500, __('Request failed, please try again later'));
if (!$order->save()) throw new ApiException(500, __('Request failed, please try again later'));
$result = $paymentService->pay([
'trade_no' => $tradeNo,
'total_amount' => isset($order->handling_amount) ? ($order->total_amount + $order->handling_amount) : $order->total_amount,
@@ -215,7 +216,7 @@ class OrderController extends Controller
->where('user_id', $request->user['id'])
->first();
if (!$order) {
abort(500, __('Order does not exist'));
throw new ApiException(500, __('Order does not exist'));
}
return response([
'data' => $order->status
@@ -244,20 +245,20 @@ class OrderController extends Controller
public function cancel(Request $request)
{
if (empty($request->input('trade_no'))) {
abort(500, __('Invalid parameter'));
throw new ApiException(500, __('Invalid parameter'));
}
$order = Order::where('trade_no', $request->input('trade_no'))
->where('user_id', $request->user['id'])
->first();
if (!$order) {
abort(500, __('Order does not exist'));
throw new ApiException(500, __('Order does not exist'));
}
if ($order->status !== 0) {
abort(500, __('You can only cancel pending orders'));
throw new ApiException(500, __('You can only cancel pending orders'));
}
$orderService = new OrderService($order);
if (!$orderService->cancel()) {
abort(500, __('Cancel failed'));
throw new ApiException(500, __('Cancel failed'));
}
return response([
'data' => true

View File

@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\User;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Models\Plan;
use App\Models\User;
@@ -17,10 +18,10 @@ class PlanController extends Controller
if ($request->input('id')) {
$plan = Plan::where('id', $request->input('id'))->first();
if (!$plan) {
abort(500, __('Subscription plan does not exist'));
throw new ApiException(500, __('Subscription plan does not exist'));
}
if ((!$plan->show && !$plan->renew) || (!$plan->show && $user->plan_id !== $plan->id)) {
abort(500, __('Subscription plan does not exist'));
throw new ApiException(500, __('Subscription plan does not exist'));
}
return response([
'data' => $plan

View File

@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\User;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Requests\User\TicketSave;
use App\Http\Requests\User\TicketWithdraw;
@@ -23,7 +24,7 @@ class TicketController extends Controller
->where('user_id', $request->user['id'])
->first();
if (!$ticket) {
abort(500, __('Ticket does not exist'));
throw new ApiException(500, __('Ticket does not exist'));
}
$ticket['message'] = TicketMessage::where('ticket_id', $ticket->id)->get();
for ($i = 0; $i < count($ticket['message']); $i++) {
@@ -49,7 +50,7 @@ class TicketController extends Controller
{
DB::beginTransaction();
if ((int)Ticket::where('status', 0)->where('user_id', $request->user['id'])->lockForUpdate()->count()) {
abort(500, __('There are other unresolved tickets'));
throw new ApiException(500, __('There are other unresolved tickets'));
}
$ticket = Ticket::create(array_merge($request->only([
'subject',
@@ -59,7 +60,7 @@ class TicketController extends Controller
]));
if (!$ticket) {
DB::rollback();
abort(500, __('Failed to open ticket'));
throw new ApiException(500, __('Failed to open ticket'));
}
$ticketMessage = TicketMessage::create([
'user_id' => $request->user['id'],
@@ -68,7 +69,7 @@ class TicketController extends Controller
]);
if (!$ticketMessage) {
DB::rollback();
abort(500, __('Failed to open ticket'));
throw new ApiException(500, __('Failed to open ticket'));
}
DB::commit();
$this->sendNotify($ticket, $request->input('message'));
@@ -80,22 +81,22 @@ class TicketController extends Controller
public function reply(Request $request)
{
if (empty($request->input('id'))) {
abort(500, __('Invalid parameter'));
throw new ApiException(500, __('Invalid parameter'));
}
if (empty($request->input('message'))) {
abort(500, __('Message cannot be empty'));
throw new ApiException(500, __('Message cannot be empty'));
}
$ticket = Ticket::where('id', $request->input('id'))
->where('user_id', $request->user['id'])
->first();
if (!$ticket) {
abort(500, __('Ticket does not exist'));
throw new ApiException(500, __('Ticket does not exist'));
}
if ($ticket->status) {
abort(500, __('The ticket is closed and cannot be replied'));
throw new ApiException(500, __('The ticket is closed and cannot be replied'));
}
if ($request->user['id'] == $this->getLastMessage($ticket->id)->user_id) {
abort(500, __('Please wait for the technical enginneer to reply'));
throw new ApiException(500, __('Please wait for the technical enginneer to reply'));
}
$ticketService = new TicketService();
if (!$ticketService->reply(
@@ -103,7 +104,7 @@ class TicketController extends Controller
$request->input('message'),
$request->user['id']
)) {
abort(500, __('Ticket reply failed'));
throw new ApiException(500, __('Ticket reply failed'));
}
$this->sendNotify($ticket, $request->input('message'));
return response([
@@ -115,17 +116,17 @@ class TicketController extends Controller
public function close(Request $request)
{
if (empty($request->input('id'))) {
abort(500, __('Invalid parameter'));
throw new ApiException(500, __('Invalid parameter'));
}
$ticket = Ticket::where('id', $request->input('id'))
->where('user_id', $request->user['id'])
->first();
if (!$ticket) {
abort(500, __('Ticket does not exist'));
throw new ApiException(500, __('Ticket does not exist'));
}
$ticket->status = 1;
if (!$ticket->save()) {
abort(500, __('Close failed'));
throw new ApiException(500, __('Close failed'));
}
return response([
'data' => true
@@ -142,18 +143,18 @@ class TicketController extends Controller
public function withdraw(TicketWithdraw $request)
{
if ((int)admin_setting('withdraw_close_enable', 0)) {
abort(500, 'user.ticket.withdraw.not_support_withdraw');
throw new ApiException(500, 'user.ticket.withdraw.not_support_withdraw');
}
if (!in_array(
$request->input('withdraw_method'),
admin_setting('commission_withdraw_method',Dict::WITHDRAW_METHOD_WHITELIST_DEFAULT)
)) {
abort(500, __('Unsupported withdrawal method'));
throw new ApiException(500, __('Unsupported withdrawal method'));
}
$user = User::find($request->user['id']);
$limit = admin_setting('commission_withdraw_limit', 100);
if ($limit > ($user->commission_balance / 100)) {
abort(500, __('The current required minimum withdrawal commission is :limit', ['limit' => $limit]));
throw new ApiException(500, __('The current required minimum withdrawal commission is :limit', ['limit' => $limit]));
}
DB::beginTransaction();
$subject = __('[Commission Withdrawal Request] This ticket is opened by the system');
@@ -164,7 +165,7 @@ class TicketController extends Controller
]);
if (!$ticket) {
DB::rollback();
abort(500, __('Failed to open ticket'));
throw new ApiException(500, __('Failed to open ticket'));
}
$message = sprintf("%s\r\n%s",
__('Withdrawal method') . "" . $request->input('withdraw_method'),
@@ -177,7 +178,7 @@ class TicketController extends Controller
]);
if (!$ticketMessage) {
DB::rollback();
abort(500, __('Failed to open ticket'));
throw new ApiException(500, __('Failed to open ticket'));
}
DB::commit();
$this->sendNotify($ticket, $message);

View File

@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\User;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Requests\User\UserChangePassword;
use App\Http\Requests\User\UserTransfer;
@@ -23,7 +24,7 @@ class UserController extends Controller
{
$user = User::find($request->user['id']);
if (!$user) {
abort(500, __('The user does not exist'));
throw new ApiException(500, __('The user does not exist'));
}
$authService = new AuthService($user);
return response([
@@ -35,7 +36,7 @@ class UserController extends Controller
{
$user = User::find($request->user['id']);
if (!$user) {
abort(500, __('The user does not exist'));
throw new ApiException(500, __('The user does not exist'));
}
$authService = new AuthService($user);
return response([
@@ -60,7 +61,7 @@ class UserController extends Controller
{
$user = User::find($request->user['id']);
if (!$user) {
abort(500, __('The user does not exist'));
throw new ApiException(500, __('The user does not exist'));
}
if (!Helper::multiPasswordVerify(
$user->password_algo,
@@ -68,13 +69,13 @@ class UserController extends Controller
$request->input('old_password'),
$user->password)
) {
abort(500, __('The old password is wrong'));
throw new ApiException(500, __('The old password is wrong'));
}
$user->password = password_hash($request->input('new_password'), PASSWORD_DEFAULT);
$user->password_algo = NULL;
$user->password_salt = NULL;
if (!$user->save()) {
abort(500, __('Save failed'));
throw new ApiException(500, __('Save failed'));
}
return response([
'data' => true
@@ -103,7 +104,7 @@ class UserController extends Controller
])
->first();
if (!$user) {
abort(500, __('The user does not exist'));
throw new ApiException(500, __('The user does not exist'));
}
$user['avatar_url'] = 'https://cdn.v2ex.com/gravatar/' . md5($user->email) . '?s=64&d=identicon';
return response([
@@ -143,12 +144,12 @@ class UserController extends Controller
])
->first();
if (!$user) {
abort(500, __('The user does not exist'));
throw new ApiException(500, __('The user does not exist'));
}
if ($user->plan_id) {
$user['plan'] = Plan::find($user->plan_id);
if (!$user['plan']) {
abort(500, __('Subscription plan does not exist'));
throw new ApiException(500, __('Subscription plan does not exist'));
}
}
$user['subscribe_url'] = Helper::getSubscribeUrl("/api/v1/client/subscribe?token={$user['token']}");
@@ -163,12 +164,12 @@ class UserController extends Controller
{
$user = User::find($request->user['id']);
if (!$user) {
abort(500, __('The user does not exist'));
throw new ApiException(500, __('The user does not exist'));
}
$user->uuid = Helper::guid(true);
$user->token = Helper::guid();
if (!$user->save()) {
abort(500, __('Reset failed'));
throw new ApiException(500, __('Reset failed'));
}
return response([
'data' => Helper::getSubscribeUrl('/api/v1/client/subscribe?token=' . $user->token)
@@ -184,12 +185,12 @@ class UserController extends Controller
$user = User::find($request->user['id']);
if (!$user) {
abort(500, __('The user does not exist'));
throw new ApiException(500, __('The user does not exist'));
}
try {
$user->update($updateData);
} catch (\Exception $e) {
abort(500, __('Save failed'));
throw new ApiException(500, __('Save failed'));
}
return response([
@@ -201,15 +202,15 @@ class UserController extends Controller
{
$user = User::find($request->user['id']);
if (!$user) {
abort(500, __('The user does not exist'));
throw new ApiException(500, __('The user does not exist'));
}
if ($request->input('transfer_amount') > $user->commission_balance) {
abort(500, __('Insufficient commission balance'));
throw new ApiException(500, __('Insufficient commission balance'));
}
$user->commission_balance = $user->commission_balance - $request->input('transfer_amount');
$user->balance = $user->balance + $request->input('transfer_amount');
if (!$user->save()) {
abort(500, __('Transfer failed'));
throw new ApiException(500, __('Transfer failed'));
}
return response([
'data' => true
@@ -220,7 +221,7 @@ class UserController extends Controller
{
$user = User::find($request->user['id']);
if (!$user) {
abort(500, __('The user does not exist'));
throw new ApiException(500, __('The user does not exist'));
}
$code = Helper::guid();