22
33namespace Pterodactyl \Http \Controllers \Admin ;
44
5+ use Exception ;
56use PDOException ;
67use Illuminate \View \View ;
78use Pterodactyl \Models \DatabaseHost ;
@@ -118,17 +119,22 @@ public function view(int $host): View
118119 * @param \Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest $request
119120 * @return \Illuminate\Http\RedirectResponse
120121 *
121- * @throws \Pterodactyl\Exceptions\Model\DataValidationException
122- * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
122+ * @throws \Throwable
123123 */
124124 public function create (DatabaseHostFormRequest $ request ): RedirectResponse
125125 {
126126 try {
127127 $ host = $ this ->creationService ->handle ($ request ->normalize ());
128- } catch (PDOException $ ex ) {
129- $ this ->alert ->danger ($ ex ->getMessage ())->flash ();
130-
131- return redirect ()->route ('admin.databases ' );
128+ } catch (Exception $ exception ) {
129+ if ($ exception instanceof PDOException || $ exception ->getPrevious () instanceof PDOException) {
130+ $ this ->alert ->danger (
131+ sprintf ('There was an error while trying to connect to the host or while executing a query: "%s" ' , $ exception ->getMessage ())
132+ )->flash ();
133+
134+ redirect ()->route ('admin.databases ' )->withInput ($ request ->validated ());
135+ } else {
136+ throw $ exception ;
137+ }
132138 }
133139
134140 $ this ->alert ->success ('Successfully created a new database host on the system. ' )->flash ();
@@ -143,8 +149,7 @@ public function create(DatabaseHostFormRequest $request): RedirectResponse
143149 * @param \Pterodactyl\Models\DatabaseHost $host
144150 * @return \Illuminate\Http\RedirectResponse
145151 *
146- * @throws \Pterodactyl\Exceptions\Model\DataValidationException
147- * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
152+ * @throws \Throwable
148153 */
149154 public function update (DatabaseHostFormRequest $ request , DatabaseHost $ host ): RedirectResponse
150155 {
@@ -153,9 +158,17 @@ public function update(DatabaseHostFormRequest $request, DatabaseHost $host): Re
153158 try {
154159 $ this ->updateService ->handle ($ host ->id , $ request ->normalize ());
155160 $ this ->alert ->success ('Database host was updated successfully. ' )->flash ();
156- } catch (PDOException $ ex ) {
157- $ this ->alert ->danger ($ ex ->getMessage ())->flash ();
158- $ redirect ->withInput ($ request ->normalize ());
161+ } catch (Exception $ exception ) {
162+ // Catch any SQL related exceptions and display them back to the user, otherwise just
163+ // throw the exception like normal and move on with it.
164+ if ($ exception instanceof PDOException || $ exception ->getPrevious () instanceof PDOException) {
165+ $ this ->alert ->danger (
166+ sprintf ('There was an error while trying to connect to the host or while executing a query: "%s" ' , $ exception ->getMessage ())
167+ )->flash ();
168+ $ redirect ->withInput ($ request ->normalize ());
169+ } else {
170+ throw $ exception ;
171+ }
159172 }
160173
161174 return $ redirect ;
0 commit comments