Skip to content

Commit

Permalink
Ignore invalid characters in JSON encoding. Fixes #138
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Sep 13, 2019
1 parent 43b825a commit 66c813c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/ErrorPage/ErrorPageViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,13 @@ public function report(): array

public function jsonEncode($data): string
{
return json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
$jsonOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;

if (version_compare(phpversion(), '7.2', '>=')) {
return json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR | $jsonOptions);
}

return json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR | $jsonOptions);
}

public function getAssetContents(string $asset): string
Expand Down
29 changes: 29 additions & 0 deletions tests/ErrorPageViewModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Facade\Ignition\Tests;

use Facade\FlareClient\Report;
use Facade\Ignition\ErrorPage\ErrorPageViewModel;
use Facade\Ignition\IgnitionConfig;

class ErrorPageViewModelTest extends TestCase
{
/** @test */
public function it_can_encode_invalid_user_data()
{
$flareClient = $this->app->make('flare.client');

$exception = new \Exception('Test Exception');

/** @var Report $report */
$report = $flareClient->createReport($exception);

$report->group('bad-utf8', [
'name' => 'Marcel'.utf8_decode('ø'),
]);

$model = new ErrorPageViewModel($exception, new IgnitionConfig([]), $report, []);

$this->assertNotEmpty($model->jsonEncode($report->toArray()));
}
}

0 comments on commit 66c813c

Please sign in to comment.