forked from summerblue/laravel-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\Exceptions; | ||
|
||
use Exception; | ||
use Illuminate\Http\Request; | ||
|
||
class InternalException extends Exception | ||
{ | ||
protected $msgForUser; | ||
|
||
public function __construct(string $message, string $msgForUser = '系统内部错误', int $code = 500) | ||
{ | ||
parent::__construct($message, $code); | ||
$this->msgForUser = $msgForUser; | ||
} | ||
|
||
public function render(Request $request) | ||
{ | ||
if ($request->expectsJson()) { | ||
return response()->json(['msg' => $this->msgForUser], $this->code); | ||
} | ||
|
||
return view('pages.error', ['msg' => $this->msgForUser]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\Exceptions; | ||
|
||
use Exception; | ||
use Illuminate\Http\Request; | ||
|
||
class InvalidRequestException extends Exception | ||
{ | ||
public function __construct(string $message = "", int $code = 400) | ||
{ | ||
parent::__construct($message, $code); | ||
} | ||
|
||
public function render(Request $request) | ||
{ | ||
if ($request->expectsJson()) { | ||
// json() 方法第二个参数就是 Http 返回码 | ||
return response()->json(['msg' => $this->message], $this->code); | ||
} | ||
|
||
return view('pages.error', ['msg' => $this->message]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
@extends('layouts.app') | ||
@section('title', '错误') | ||
|
||
@section('content') | ||
<div class="card"> | ||
<div class="card-header">错误</div> | ||
<div class="card-body text-center"> | ||
<h1>{{ $msg }}</h1> | ||
<a class="btn btn-primary" href="{{ route('root') }}">返回首页</a> | ||
</div> | ||
</div> | ||
@endsection |