Browse Source

fix: 🚑️ Use try catch to prevent 500 errors

LogischJo 2 năm trước cách đây
mục cha
commit
371a37df7a

+ 38 - 2
app/Http/Controllers/Moderation/TicketsController.php

@@ -25,7 +25,12 @@ class TicketsController extends Controller
 
     public function show($ticket_id)
     {
+        try {
         $ticket = Ticket::where('ticket_id', $ticket_id)->firstOrFail();
+        } catch (Exception $e)
+        {
+            return redirect()->back()->with('warning', __('Ticket not found on the server. It potentially got deleted earlier'));
+        }
         $ticketcomments = $ticket->ticketcomments;
         $ticketcategory = $ticket->ticketcategory;
         $server = Server::where('id', $ticket->server)->first();
@@ -34,8 +39,14 @@ class TicketsController extends Controller
     }
 
     public function changeStatus($ticket_id)
-    {
+    {   
+        try {
         $ticket = Ticket::where('ticket_id', $ticket_id)->firstOrFail();
+        } catch(Exception $e)
+        {
+            return redirect()->back()->with('warning', __('Ticket not found on the server. It potentially got deleted earlier'));
+        }
+
         if($ticket->status == "Closed"){
             $ticket->status = "Reopened";
             $ticket->save();
@@ -50,7 +61,14 @@ class TicketsController extends Controller
 
     public function delete($ticket_id)
     {
+        try {
         $ticket = Ticket::where('ticket_id', $ticket_id)->firstOrFail();
+        } catch (Exception $e)
+        {
+            return redirect()->back()->with('warning', __('Ticket not found on the server. It potentially got deleted earlier'));
+
+        }
+
         TicketComment::where('ticket_id', $ticket->id)->delete();
         $ticket->delete();
 
@@ -60,7 +78,14 @@ class TicketsController extends Controller
     public function reply(Request $request)
     {
         $this->validate($request, ['ticketcomment' => 'required']);
-        $ticket = Ticket::where('id', $request->input('ticket_id'))->firstOrFail();
+        try {
+            $ticket = Ticket::where('id', $request->input('ticket_id'))->firstOrFail();
+
+        }
+        catch (Exception $e){
+            return redirect()->back()->with('warning', __('Ticket not found on the server. It potentially got deleted earlier'));
+
+        }
         $ticket->status = 'Answered';
         $ticket->update();
         TicketComment::create([
@@ -68,7 +93,12 @@ class TicketsController extends Controller
             'user_id' => Auth::user()->id,
             'ticketcomment' => $request->input('ticketcomment'),
         ]);
+        try {
         $user = User::where('id', $ticket->user_id)->firstOrFail();
+        } catch(Exception $e)
+        {
+            return redirect()->back()->with('warning', __('User not found on the server. Check on the admin database or try again later.'));
+        }
         $newmessage = $request->input('ticketcomment');
         $user->notify(new ReplyNotification($ticket, $user, $newmessage));
 
@@ -145,7 +175,13 @@ class TicketsController extends Controller
 
     public function blacklistAdd(Request $request)
     {
+        try {
         $user = User::where('id', $request->user_id)->first();
+        }
+        catch (Exception $e){
+            return redirect()->back()->with('warning', __('User not found on the server. Check the admin database or try again later.'));
+
+        }
         $check = TicketBlacklist::where('user_id', $user->id)->first();
         if ($check) {
             $check->reason = $request->reason;

+ 16 - 1
app/Http/Controllers/TicketsController.php

@@ -72,7 +72,12 @@ class TicketsController extends Controller
 
     public function show($ticket_id)
     {
+       try {
         $ticket = Ticket::where('ticket_id', $ticket_id)->firstOrFail();
+       } catch (Exception $e)
+       {
+        return redirect()->back()->with('warning', __('Ticket not found on the server. It potentially got deleted earlier'));
+       }
         $ticketcomments = $ticket->ticketcomments;
         $ticketcategory = $ticket->ticketcategory;
         $server = Server::where('id', $ticket->server)->first();
@@ -88,7 +93,12 @@ class TicketsController extends Controller
             return redirect()->route('ticket.index')->with('error', __("You can't reply a ticket because you're on the blacklist for a reason: '".$check->reason."', please contact the administrator"));
         }
         $this->validate($request, ['ticketcomment' => 'required']);
+        try {
         $ticket = Ticket::where('id', $request->input('ticket_id'))->firstOrFail();
+        } catch (Exception $e)
+        {
+            return redirect()->back()->with('warning', __('Ticket not found on the server. It potentially got deleted earlier'));
+        }
         $ticket->status = 'Client Reply';
         $ticket->update();
         $ticketcomment = TicketComment::create([
@@ -105,8 +115,13 @@ class TicketsController extends Controller
         return redirect()->back()->with('success', __('Your comment has been submitted'));
     }
     public function changeStatus($ticket_id)
-    {
+    {   
+        try {
         $ticket = Ticket::where('user_id', Auth::user()->id)->where("ticket_id", $ticket_id)->firstOrFail();
+        } catch (Exception $e)
+        {
+            return redirect()->back()->with('warning', __('Ticket not found on the server. It potentially got deleted earlier'));
+        }
         if($ticket->status == "Closed"){
             $ticket->status = "Reopened";
             $ticket->save();