Skip to content

Commit 331757f

Browse files
[9.x] Default 404 message on denyAsNotFound (#43901)
* default 404 message when denyAsNotFound * fix test 404 * wording * message parse on response class * partial test fixing * proposed message precedence change * formatting * code style Co-authored-by: Tim MacDonald <[email protected]>
1 parent 4431fb3 commit 331757f

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

src/Illuminate/Foundation/Exceptions/Handler.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,9 @@ protected function prepareException(Throwable $e)
378378
return match (true) {
379379
$e instanceof BackedEnumCaseNotFoundException => new NotFoundHttpException($e->getMessage(), $e),
380380
$e instanceof ModelNotFoundException => new NotFoundHttpException($e->getMessage(), $e),
381-
$e instanceof AuthorizationException && $e->hasStatus() => new HttpException($e->status(), $e->getMessage(), $e),
381+
$e instanceof AuthorizationException && $e->hasStatus() => new HttpException(
382+
$e->status(), $e->response()?->message() ?: (Response::$statusTexts[$e->status()] ?? 'Whoops, looks like something went wrong.'), $e
383+
),
382384
$e instanceof AuthorizationException && ! $e->hasStatus() => new AccessDeniedHttpException($e->getMessage(), $e),
383385
$e instanceof TokenMismatchException => new HttpException(419, $e->getMessage(), $e),
384386
$e instanceof SuspiciousOperationException => new NotFoundHttpException('Bad hostname provided.', $e),

tests/Auth/AuthAccessResponseTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function testItSetsEmptyStatusOnExceptionWhenAuthorizing()
4343
} catch (AuthorizationException $e) {
4444
$this->assertNull($e->status());
4545
$this->assertFalse($e->hasStatus());
46+
$this->assertSame('foo', $e->response()->message());
4647
$this->assertSame('foo', $e->getMessage());
4748
$this->assertSame(3, $e->getCode());
4849
}
@@ -56,6 +57,7 @@ public function testItSetsStatusOnExceptionWhenAuthorizing()
5657
} catch (AuthorizationException $e) {
5758
$this->assertSame(418, $e->status());
5859
$this->assertTrue($e->hasStatus());
60+
$this->assertSame('foo', $e->response()->message());
5961
$this->assertSame('foo', $e->getMessage());
6062
$this->assertSame(3, $e->getCode());
6163
}
@@ -66,6 +68,7 @@ public function testItSetsStatusOnExceptionWhenAuthorizing()
6668
} catch (AuthorizationException $e) {
6769
$this->assertSame(404, $e->status());
6870
$this->assertTrue($e->hasStatus());
71+
$this->assertSame('foo', $e->response()->message());
6972
$this->assertSame('foo', $e->getMessage());
7073
$this->assertSame(3, $e->getCode());
7174
}
@@ -76,6 +79,7 @@ public function testItSetsStatusOnExceptionWhenAuthorizing()
7679
} catch (AuthorizationException $e) {
7780
$this->assertSame(444, $e->status());
7881
$this->assertTrue($e->hasStatus());
82+
$this->assertNull($e->response()->message());
7983
$this->assertSame('This action is unauthorized.', $e->getMessage());
8084
$this->assertSame(0, $e->getCode());
8185
}
@@ -86,6 +90,7 @@ public function testItSetsStatusOnExceptionWhenAuthorizing()
8690
} catch (AuthorizationException $e) {
8791
$this->assertSame(444, $e->status());
8892
$this->assertTrue($e->hasStatus());
93+
$this->assertSame('foo', $e->response()->message());
8994
$this->assertSame('foo', $e->getMessage());
9095
$this->assertSame(3, $e->getCode());
9196
}
@@ -96,6 +101,7 @@ public function testItSetsStatusOnExceptionWhenAuthorizing()
96101
} catch (AuthorizationException $e) {
97102
$this->assertSame(404, $e->status());
98103
$this->assertTrue($e->hasStatus());
104+
$this->assertNull($e->response()->message());
99105
$this->assertSame('This action is unauthorized.', $e->getMessage());
100106
$this->assertSame(0, $e->getCode());
101107
}
@@ -106,6 +112,7 @@ public function testItSetsStatusOnExceptionWhenAuthorizing()
106112
} catch (AuthorizationException $e) {
107113
$this->assertSame(404, $e->status());
108114
$this->assertTrue($e->hasStatus());
115+
$this->assertSame('foo', $e->response()->message());
109116
$this->assertSame('foo', $e->getMessage());
110117
$this->assertSame(3, $e->getCode());
111118
}

tests/Integration/Foundation/ExceptionHandlerTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Tests\Integration\Foundation;
44

5+
use Illuminate\Auth\Access\AuthorizationException;
56
use Illuminate\Auth\Access\Response;
67
use Illuminate\Support\Facades\Route;
78
use Orchestra\Testbench\TestCase;
@@ -41,4 +42,69 @@ public function testItRendersAuthorizationExceptionsWithCustomStatusCode()
4142
'message' => 'expected message',
4243
]);
4344
}
45+
46+
public function testItRendersAuthorizationExceptionsWithStatusCodeTextWhenNoMessageIsSet()
47+
{
48+
Route::get('test-route', fn () => Response::denyWithStatus(404)->authorize());
49+
50+
// HTTP request...
51+
$this->get('test-route')
52+
->assertStatus(404)
53+
->assertSeeText('Not Found');
54+
55+
// JSON request...
56+
$this->getJson('test-route')
57+
->assertStatus(404)
58+
->assertExactJson([
59+
'message' => 'Not Found',
60+
]);
61+
62+
Route::get('test-route', fn () => Response::denyWithStatus(418)->authorize());
63+
64+
// HTTP request...
65+
$this->get('test-route')
66+
->assertStatus(418)
67+
->assertSeeText("I'm a teapot", false);
68+
69+
// JSON request...
70+
$this->getJson('test-route')
71+
->assertStatus(418)
72+
->assertExactJson([
73+
'message' => "I'm a teapot",
74+
]);
75+
}
76+
77+
public function testItRendersAuthorizationExceptionsWithStatusButWithoutResponse()
78+
{
79+
Route::get('test-route', fn () => throw (new AuthorizationException())->withStatus(418));
80+
81+
// HTTP request...
82+
$this->get('test-route')
83+
->assertStatus(418)
84+
->assertSeeText("I'm a teapot", false);
85+
86+
// JSON request...
87+
$this->getJson('test-route')
88+
->assertStatus(418)
89+
->assertExactJson([
90+
'message' => "I'm a teapot",
91+
]);
92+
}
93+
94+
public function testItHasFallbackErrorMessageForUnknownStatusCodes()
95+
{
96+
Route::get('test-route', fn () => throw (new AuthorizationException())->withStatus(399));
97+
98+
// HTTP request...
99+
$this->get('test-route')
100+
->assertStatus(399)
101+
->assertSeeText('Whoops, looks like something went wrong.');
102+
103+
// JSON request...
104+
$this->getJson('test-route')
105+
->assertStatus(399)
106+
->assertExactJson([
107+
'message' => 'Whoops, looks like something went wrong.',
108+
]);
109+
}
44110
}

0 commit comments

Comments
 (0)