1<?php 2/** 3 * DatabaseConnectException.php 4 * 5 * -Description- 6 * 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program. If not, see <https://www.gnu.org/licenses/>. 19 * 20 * @link https://www.librenms.org 21 * @copyright 2018 Tony Murray 22 * @author Tony Murray <murraytony@gmail.com> 23 */ 24 25namespace LibreNMS\Exceptions; 26 27use Illuminate\Database\QueryException; 28use LibreNMS\Interfaces\Exceptions\UpgradeableException; 29 30class DatabaseConnectException extends \Exception implements UpgradeableException 31{ 32 /** 33 * Try to convert the given Exception to a DatabaseConnectException 34 * 35 * @param \Exception $exception 36 * @return static|null 37 */ 38 public static function upgrade($exception) 39 { 40 // connect exception, convert to our standard connection exception 41 return $exception instanceof QueryException && in_array($exception->getCode(), [1044, 1045, 2002]) ? 42 new static( 43 config('app.debug') ? $exception->getMessage() : $exception->getPrevious()->getMessage(), 44 $exception->getCode(), 45 $exception 46 ) : 47 null; 48 } 49 50 /** 51 * Render the exception into an HTTP or JSON response. 52 * 53 * @return \Illuminate\Http\Response 54 */ 55 public function render(\Illuminate\Http\Request $request) 56 { 57 $title = trans('exceptions.database_connect.title'); 58 59 return $request->wantsJson() ? response()->json([ 60 'status' => 'error', 61 'message' => "$title: " . $this->getMessage(), 62 ]) : response()->view('errors.generic', [ 63 'title' => $title, 64 'content' => $this->getMessage(), 65 ]); 66 } 67} 68