main
Jing Li 2024-04-17 13:13:05 +08:00
parent fd5c2c679e
commit 1dcc10d0d3
2 changed files with 16 additions and 7 deletions

View File

@ -41,7 +41,7 @@ abstract class AdminController extends Controller
DB::commit(); DB::commit();
} catch (Throwable $th) { } catch (Throwable $th) {
DB::rollBack(); DB::rollBack();
throw $this->prepareException($th); return $this->renderException($th);
} }
return $this->response()->successMessage(__('admin.save').__('admin.successfully')); return $this->response()->successMessage(__('admin.save').__('admin.successfully'));
@ -70,7 +70,7 @@ abstract class AdminController extends Controller
DB::commit(); DB::commit();
} catch (Throwable $th) { } catch (Throwable $th) {
DB::rollBack(); DB::rollBack();
throw $this->prepareException($th); return $this->renderException($th);
} }
return $this->response()->successMessage(__('admin.save').__('admin.successfully')); return $this->response()->successMessage(__('admin.save').__('admin.successfully'));
@ -89,7 +89,7 @@ abstract class AdminController extends Controller
DB::commit(); DB::commit();
} catch (Throwable $th) { } catch (Throwable $th) {
DB::rollBack(); DB::rollBack();
throw $this->prepareException($th); return $this->renderException($th);
} }
return $this->response()->successMessage(__('admin.delete').__('admin.successfully')); return $this->response()->successMessage(__('admin.delete').__('admin.successfully'));
@ -106,16 +106,23 @@ abstract class AdminController extends Controller
return $path; return $path;
} }
protected function prepareException(Throwable $e) protected function renderException(Throwable $e)
{ {
report($e);
if ($e instanceof AdminException) {
return $e->render();
}
$message = $e->getMessage();
if ($e instanceof ValidationException) { if ($e instanceof ValidationException) {
$message = Arr::first($e->errors()); $message = Arr::first($e->errors());
if (is_array($message)) { if (is_array($message)) {
$message = Arr::first($message); $message = Arr::first($message) ?: '参数错误';
} }
$e = new AdminException($message ?: '参数错误');
} }
return $e; return $this->response()->fail($message);
} }
} }

View File

@ -6,6 +6,7 @@ use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable; use Throwable;
use Illuminate\Http\{Request}; use Illuminate\Http\{Request};
use Illuminate\Validation\ValidationException; use Illuminate\Validation\ValidationException;
use Slowlyo\OwlAdmin\Exceptions\AdminException;
class Handler extends ExceptionHandler class Handler extends ExceptionHandler
{ {
@ -21,6 +22,7 @@ class Handler extends ExceptionHandler
]; ];
protected $dontReport = [ protected $dontReport = [
AdminException::class,
RuntimeException::class, RuntimeException::class,
]; ];