Skip to content
This repository was archived by the owner on Feb 18, 2023. It is now read-only.

Commit 39c4bc2

Browse files
committed
Working 5.5 Upgrade, Add handler for Validation Exception.
1 parent a9122c3 commit 39c4bc2

File tree

5 files changed

+18
-1
lines changed

5 files changed

+18
-1
lines changed

app/Providers/ErrorHandlerServiceProvider.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use Dingo\Api\Routing\Helpers;
66
use Illuminate\Support\ServiceProvider;
77
use App\Exceptions\BodyTooLargeException;
8+
use Dingo\Api\Exception\ResourceException;
89
use Illuminate\Auth\AuthenticationException;
10+
use Illuminate\Validation\ValidationException;
911
use Illuminate\Database\Eloquent\ModelNotFoundException;
1012

1113
class ErrorHandlerServiceProvider extends ServiceProvider
@@ -39,5 +41,12 @@ public function register()
3941
app('Dingo\Api\Exception\Handler')->register(function (BodyTooLargeException $exception) {
4042
return $this->response->error('The body is too large', 413);
4143
});
44+
app('Dingo\Api\Exception\Handler')->register(function (ValidationException $exception) {
45+
if (request()->expectsJson()) {
46+
throw new ResourceException('Validation Error', $exception->errors());
47+
}
48+
49+
return redirect()->back()->withInput(request()->input())->withErrors($exception->errors());
50+
});
4251
}
4352
}

tests/Feature/Users/PermissionsEndpointsTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Tests\Feature\Users;
44

5-
use App\Entities\Permission;
65
use Tests\TestCase;
76
use App\Entities\User;
7+
use App\Entities\Permission;
88
use Laravel\Passport\Passport;
9+
use Spatie\Permission\PermissionRegistrar;
910
use Illuminate\Foundation\Testing\DatabaseMigrations;
1011

1112
class PermissionsEndpointsTest extends TestCase
@@ -18,6 +19,7 @@ function setUp()
1819
{
1920
parent::setUp();
2021
$this->installApp();
22+
$this->app->make(PermissionRegistrar::class)->registerPermissions();
2123
}
2224

2325
function test_it_can_list_permissions()

tests/Feature/Users/ProfileEndpointsTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Entities\User;
77
use Laravel\Passport\Passport;
88
use Illuminate\Contracts\Hashing\Hasher;
9+
use Spatie\Permission\PermissionRegistrar;
910
use Illuminate\Foundation\Testing\DatabaseMigrations;
1011

1112
class ProfileEndpointsTest extends TestCase
@@ -17,6 +18,7 @@ function setUp()
1718
{
1819
parent::setUp();
1920
$this->installApp();
21+
$this->app->make(PermissionRegistrar::class)->registerPermissions();
2022
}
2123

2224
function test_it_gets_user_profile()

tests/Feature/Users/RolesEndpointsTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Entities\Role;
88
use App\Entities\Permission;
99
use Laravel\Passport\Passport;
10+
use Spatie\Permission\PermissionRegistrar;
1011
use Illuminate\Foundation\Testing\DatabaseMigrations;
1112

1213
class RolesEndpointsTest extends TestCase
@@ -18,6 +19,7 @@ function setUp()
1819
{
1920
parent::setUp();
2021
$this->installApp();
22+
$this->app->make(PermissionRegistrar::class)->registerPermissions();
2123
}
2224

2325

tests/Feature/Users/UsersEndpointsTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Tests\TestCase;
77
use App\Entities\User;
88
use Laravel\Passport\Passport;
9+
use Spatie\Permission\PermissionRegistrar;
910
use Illuminate\Foundation\Testing\DatabaseMigrations;
1011

1112
class UsersEndpointsTest extends TestCase
@@ -17,6 +18,7 @@ function setUp()
1718
{
1819
parent::setUp();
1920
$this->installApp();
21+
$this->app->make(PermissionRegistrar::class)->registerPermissions();
2022
}
2123

2224
function test_it_responds_unauthenticated_for_list_users()

0 commit comments

Comments
 (0)