Skip to content

Commit dd5fc74

Browse files
Merge pull request #68 from MarcinOrlowski/dev
Release 4.0.1
2 parents 980621f + 53c2466 commit dd5fc74

File tree

6 files changed

+40
-18
lines changed

6 files changed

+40
-18
lines changed

CHANGES.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@ See [compatibility docs](docs/compatibility.md) for details about backward compa
44

55
## CHANGE LOG ##
66

7+
* v4.0.1 (2017-04-10)
8+
* TestingHelpers trait's `validateResponseStructure()` method is now public
9+
* [RB-64] Fixed Exception Handler generated HTTP code being out of allowed range in some cases
10+
* [RB-65] Exception Handler Helper now deals with messages using non-UTF8 or broken encoding
11+
* Exception Handler's trace data is now properly placed into `trace` leaf
12+
713
* v4.0.0 (2017-04-10)
814
* **BACKWARD INCOMPATIBILE CHANGES**
915
* [RB-59] Added option to remap response JSON keys to user provided values
1016
* [RB-54] Debug data no longer pollutes `data` leaf. Instead, it adds `debug` dictionary to root data structure.
1117
* [RB-37] Added support for Laravel 5.3+ `unauthenticated()` in Exception Handler. See new config keys defails
1218
* [RB-47] Exception Handler now supports `FormRequests` and returns all messages in `ResponseBuilder::KEY_MESSAGES`
1319
* Uncaught `HttpResponse::HTTP_UNAUTHORIZED` exception is now handled same way `authentication_exception` is
14-
* [RB=56] Added configurable key for debug trace added to returned JSON response (if enabled)
20+
* [RB-56] Added configurable key for debug trace added to returned JSON response (if enabled)
1521
* Added traits to help testing your config and ApiCodes with ease. See `Unit Testing your ApiCodes` docs for details
1622
* `ApiCodeBase` class is now named `BaseApiCodes`
1723
* [RB-35] ExceptionHandlerHelper is now covered by tests

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ then then feel free to donate to the project. Send some Bitcoins (BTC) to `1Lbfb
4949
* API chaining/cascading support,
5050
* Includes traits to help [unit testing your API code](docs/testing.md),
5151
* Provides own [exception handler helper](docs/exceptions.md) to ensure your API stays consumable even in case of unexpected,
52-
* No extra dependencies.
52+
* No extra mandatory dependencies.
5353

5454
----
5555

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "marcin-orlowski/laravel-api-response-builder",
33
"description": "Helps building nice, normalized and easy to consume REST API responses.",
44
"homepage": "https://github.com/MarcinOrlowski/laravel-api-response-builder",
5-
"version": "4.0.0",
5+
"version": "4.0.1",
66
"keywords": [
77
"laravel",
88
"json",

docs/docs.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,15 @@ to handle them yourself by calling `Lang::get()` manually first and pass the res
366366

367367
## Requirements ##
368368

369-
There're no special requirements. Once you fulfill Laravel's requirements you are all good. Minimum
370-
versions `ResponseBuilder` is tested against are:
369+
Minimum requirements:
371370

372-
* PHP 5.5+
373-
* Laravel 5.1.45+
371+
* PHP 5.5
372+
* Laravel 5.1.45
374373

375-
all newer versions of PHP and Laravel are also supported out of the box.
374+
The following PHP extensions are optional but strongly recommended:
375+
376+
* iconv
377+
* mb_string
376378

377379
----
378380

src/ExceptionHandlerHelper.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ protected static function error(Exception $exception, $exception_type,
116116
}
117117

118118
// can it be considered valid HTTP error code?
119-
if ($http_code < 400) {
119+
if (($http_code < 400) || ($http_code > 499)) {
120120
$http_code = 0;
121121
}
122-
} elseif ($http_code < 400) {
122+
} elseif (($http_code < 400) || ($http_code > 499)) {
123123
$http_code = 0;
124124
}
125125

@@ -128,12 +128,14 @@ protected static function error(Exception $exception, $exception_type,
128128
$http_code = $default_http_code;
129129
}
130130

131-
$debug_data = null;
132-
if (Config::get(ResponseBuilder::CONF_KEY_DEBUG_EX_TRACE_ENABLED, true)) {
133-
$debug_data = [
134-
ResponseBuilder::KEY_CLASS => get_class($exception),
135-
ResponseBuilder::KEY_FILE => $exception->getFile(),
136-
ResponseBuilder::KEY_LINE => $exception->getLine(),
131+
$trace_data = null;
132+
if (Config::get(ResponseBuilder::CONF_KEY_DEBUG_EX_TRACE_ENABLED, false)) {
133+
$trace_data = [
134+
Config::get(ResponseBuilder::CONF_KEY_DEBUG_EX_TRACE_KEY, ResponseBuilder::KEY_TRACE) => [
135+
ResponseBuilder::KEY_CLASS => get_class($exception),
136+
ResponseBuilder::KEY_FILE => $exception->getFile(),
137+
ResponseBuilder::KEY_LINE => $exception->getLine(),
138+
],
137139
];
138140
}
139141

@@ -166,6 +168,18 @@ protected static function error(Exception $exception, $exception_type,
166168
// let's build error message
167169
$error_message = '';
168170
$ex_message = trim($exception->getMessage());
171+
172+
// ensure we won't fail due to exception incorect encoding
173+
if (!mb_check_encoding($ex_message, 'UTF-8')) {
174+
// let's check there's iconv and mb_string available
175+
if (function_exists('iconv') && function_exists('mb_detec_encoding')) {
176+
$ex_message = iconv(mb_detect_encoding($ex_message, mb_detect_order(), true), 'UTF-8', $ex_message);
177+
} else {
178+
// lame fallback, in case there's no iconv/mb_string installed
179+
$ex_message = htmlspecialchars_decode(htmlspecialchars($ex_message, ENT_SUBSTITUTE, 'UTF-8'));
180+
}
181+
}
182+
169183
if (Config::get('response_builder.exception_handler.use_exception_message_first', true)) {
170184
if ($ex_message === '') {
171185
$ex_message = get_class($exception);
@@ -182,7 +196,7 @@ protected static function error(Exception $exception, $exception_type,
182196
]);
183197
}
184198

185-
return ResponseBuilder::errorWithMessageAndDataAndDebug($api_code, $error_message, $data, $http_code, null, $debug_data);
199+
return ResponseBuilder::errorWithMessageAndDataAndDebug($api_code, $error_message, $data, $http_code, null, $trace_data);
186200
}
187201

188202
}

tests/Traits/TestingHelpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ private function getResponseObjectRaw($expected_api_code, $expected_http_code, $
238238
*
239239
* @return void
240240
*/
241-
protected function validateResponseStructure($json_object, array $extra_keys = [])
241+
public function validateResponseStructure($json_object, array $extra_keys = [])
242242
{
243243
$this->assertTrue(is_object($json_object));
244244

0 commit comments

Comments
 (0)