Skip to content

Commit

Permalink
add proper error message texts for JSON filter errors JSON_INPUT_TOO_…
Browse files Browse the repository at this point in the history
…BIG, JSON_INVALID and JSON_SYNTAX_ERROR
  • Loading branch information
mikey179 committed Dec 11, 2015
1 parent 894ebcd commit 53b531b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
* Raised minimum required PHP version to 5.5


### Other changes

* added proper error message texts for JSON filter errors `JSON_INPUT_TOO_BIG`, `JSON_INVALID` and `JSON_SYNTAX_ERROR`


5.2.1 (2015-06-22)
------------------

Expand Down
9 changes: 7 additions & 2 deletions src/main/php/filter/JsonFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function apply(Param $param)
}

if ($param->length() > 20000) {
$param->addError('JSON_INPUT_TOO_BIG');
$param->addError('JSON_INPUT_TOO_BIG', ['maxLength' => 20000]);
return null;
}

Expand All @@ -46,7 +46,12 @@ public function apply(Param $param)
$decodedJson = json_decode($value);
$errorCode = json_last_error();
if (JSON_ERROR_NONE !== $errorCode) {
$param->addError('JSON_SYNTAX_ERROR', ['errorCode' => $errorCode]);
$param->addError(
'JSON_SYNTAX_ERROR',
['errorCode' => $errorCode,
'errorMsg' => json_last_error_msg()
]
);
return null;
}

Expand Down
20 changes: 20 additions & 0 deletions src/main/resources/input/error/message.ini
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,23 @@ de_*="Bitte wählen Sie eine gültige Zeitspanne."
default="Please select a correct week."
en_*="Please select a correct week."
de_*="Bitte wählen Sie einen korrekte Woche."

[JSON_INPUT_TOO_BIG]
default="Please use not more than {maxLength} characters."
en_*="Please use not more than {maxLength} characters."
de_*="Bitte geben Sie nicht mehr als {maxLength} Zeichen an."
fr_*="Veuillez saisir au maximum {maxLength} caractères."
es_*="Debe incluir un máximo de {maxLength} caracteres."
ro_*="Vă rugăm să introduceţi cel mult {maxLength} caractere."
pl_*="Proszę wpisać nie więcej niż {maxLength} znaków."
it_*="Non utilizzi più di {maxLength} caratteri."

[JSON_INVALID]
default="The provided JSON data is invalid."
en_*="The provided JSON data is invalid."
de_*="Die übergebene JSON-Datenstruktur enthält kein gültiges JSON."

[JSON_SYNTAX_ERROR]
default="The provided JSON data contains a syntax error: {errorMsg}"
en_*="The provided JSON data contains a syntax error: {errorMsg}"
de_*="Die übergebene JSON-Datenstruktur enthält einen Syntaxfehler: {errorMsg}"
28 changes: 28 additions & 0 deletions src/test/php/filter/JsonFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,34 @@ public function addsErrorToParamForInvalidJsonStructure()
assertTrue($param->hasError('JSON_SYNTAX_ERROR'));
}

/**
* @test
* @since 6.0.0
*/
public function errorContainsErrorCode()
{
$param = $this->createParam('{"foo":"bar","foo","bar"}');
$this->jsonFilter->apply($param);
assertArrayHasKey(
'errorCode',
$param->errors()['JSON_SYNTAX_ERROR']->details()
);
}

/**
* @test
* @since 6.0.0
*/
public function errorContainsErrorMessage()
{
$param = $this->createParam('{"foo":"bar","foo","bar"}');
$this->jsonFilter->apply($param);
assertArrayHasKey(
'errorMsg',
$param->errors()['JSON_SYNTAX_ERROR']->details()
);
}

/**
* @test
*/
Expand Down

0 comments on commit 53b531b

Please sign in to comment.