-
-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[5.x] Refactor frontend formFailure to handle precognitive and fetch …
…exceptions (#10376)
- Loading branch information
1 parent
cfbe3eb
commit dacb506
Showing
2 changed files
with
53 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 |
---|---|---|
|
@@ -932,4 +932,49 @@ public function it_removes_any_uploaded_assets_when_a_listener_throws_a_validati | |
|
||
Storage::disk('avatars')->assertMissing('avatar.jpg'); | ||
} | ||
|
||
#[Test] | ||
public function it_renders_exceptions_thrown_during_json_requests_as_standard_laravel_errors() | ||
{ | ||
Event::listen(function (\Statamic\Events\FormSubmitted $event) { | ||
throw ValidationException::withMessages(['some' => 'error']); | ||
}); | ||
|
||
$response = $this | ||
->postJson('/!/forms/contact', [ | ||
'name' => 'Name', | ||
'email' => '[email protected]', | ||
'message' => 'This is a message', | ||
]); | ||
|
||
$json = $response->json(); | ||
|
||
$this->assertArrayHasKey('message', $json); | ||
$this->assertArrayHasKey('errors', $json); | ||
$this->assertSame($json['errors'], ['some' => ['error']]); | ||
} | ||
|
||
#[Test] | ||
public function it_renders_exceptions_thrown_during_xml_http_requests_in_statamic_error_format() | ||
{ | ||
Event::listen(function (\Statamic\Events\FormSubmitted $event) { | ||
throw ValidationException::withMessages(['some' => 'error']); | ||
}); | ||
|
||
$response = $this | ||
->withHeaders([ | ||
'X-Requested-With' => 'XMLHttpRequest', | ||
]) | ||
->postJson('/!/forms/contact', [ | ||
'name' => 'Name', | ||
'email' => '[email protected]', | ||
'message' => 'This is a message', | ||
]); | ||
|
||
$json = $response->json(); | ||
|
||
$this->assertArrayHasKey('error', $json); | ||
$this->assertArrayHasKey('errors', $json); | ||
$this->assertSame($json['error'], ['some' => 'error']); | ||
} | ||
} |