Skip to content

Commit 694993d

Browse files
Merge pull request #40 from MarcinOrlowski/dev
Release 3.1.0
2 parents 5559b8c + eb28365 commit 694993d

9 files changed

+354
-84
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ See [compatibility docs](docs/compatibility.md) for details about backward compa
66

77
## CHANGE LOG ##
88

9+
* v3.1.0 (2017-02-28)
10+
* [RB-38] Added `encoding-options` to control data-to-json conversion.
11+
* [RB-38] Added optional encoding options args to all methods accepting `data` argument
12+
* [RB-34] Added option to control ExceptionHandeler behavior on debug builds
13+
* ExceptionHandler's debug is now added as `debug` node to make it more clear where it comes from
14+
915
* v3.0.3 (2017-02-24)
1016
* No changes. v3.0.2 was incorrectly released
1117

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": "3.0.3",
5+
"version": "3.1.0",
66
"keywords": [
77
"laravel",
88
"json",

config/response_builder.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,28 @@
8484

8585
],
8686

87+
/*
88+
|--------------------------------------------------------------------------
89+
| data-to-json encodong options
90+
|--------------------------------------------------------------------------
91+
|
92+
| This controls data JSON encoding. Since 3.1, encoding was relying on
93+
| framework defaults, however this caused valid UTF8 characters (i.e. accents)
94+
| to be returned escaped, which while technically correct (ant theoretically
95+
| transparent) might not be desired effects.
96+
|
97+
| To prevent escaping, add JSON_UNESCAPED_UNICODE:
98+
| JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_AMP|JSON_HEX_QUOT|JSON_UNESCAPED_UNICODE
99+
|
100+
| Default value:
101+
| JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_AMP|JSON_HEX_QUOT
102+
|
103+
| See http://php.net/manual/en/function.json-encode.php for details
104+
|
105+
*/
106+
'encoding_options' => JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_AMP|JSON_HEX_QUOT,
107+
108+
87109
/*
88110
|--------------------------------------------------------------------------
89111
| Exception handler error codes
@@ -131,4 +153,25 @@
131153

132154
],
133155

156+
157+
/*
158+
|--------------------------------------------------------------------------
159+
| Debug config
160+
|--------------------------------------------------------------------------
161+
|
162+
*/
163+
164+
'debug' => [
165+
'exception_handler' => [
166+
/**
167+
* When ExceptionHandler kicks in and this is set to @true (default),
168+
* then **if** your `app.debug` is `true` too, returned JSON structure
169+
* will contain `debug` node with additional exception base trace (class
170+
* name, file name, line number). If `app.debug` is anything but @true,
171+
* no debug info is added.
172+
*/
173+
'trace_enabled' => true,
174+
],
175+
],
176+
134177
];

src/ExceptionHandlerHelper.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ public static function render($request, Exception $exception)
6565
protected static function error(Exception $exception, $exception_type,
6666
$default_api_code, $default_http_code = HttpResponse::HTTP_BAD_REQUEST)
6767
{
68-
$api_code = Config::get("response_builder.exception_handler.exception.{$exception_type}.code", $default_api_code);
69-
$http_code = Config::get("response_builder.exception_handler.exception.{$exception_type}.http_code", 0);
68+
$base_config = 'response_builder.exception_handler.exception';
69+
70+
$api_code = Config::get("{$base_config}.{$exception_type}.code", $default_api_code);
71+
$http_code = Config::get("{$base_config}.{$exception_type}.http_code", 0);
7072

7173
// check if this is valid HTTP error code
7274
if ($http_code === 0) {
@@ -92,15 +94,16 @@ protected static function error(Exception $exception, $exception_type,
9294

9395
$data = [];
9496
if (Config::get('app.debug')) {
95-
$data = [
96-
'class' => get_class($exception),
97-
'file' => $exception->getFile(),
98-
'line' => $exception->getLine(),
99-
];
97+
if (Config::get('response_builder.debug.exception_handler.trace_enabled', true)) {
98+
$data = ['debug' => [
99+
'class' => get_class($exception),
100+
'file' => $exception->getFile(),
101+
'line' => $exception->getLine(),
102+
]];
103+
}
100104
}
101105

102106
// let's figure out what event we are handling now
103-
$base_config = 'response_builder.exception_handler.exception';
104107
if (Config::get("{$base_config}.http_not_found.code", ApiCodeBase::EX_HTTP_NOT_FOUND) === $api_code) {
105108
$base_api_code = ApiCodeBase::EX_HTTP_NOT_FOUND;
106109
} elseif (Config::get("{$base_config}.http_service_unavailable.code", ApiCodeBase::EX_HTTP_SERVICE_UNAVAILABLE) === $api_code) {
@@ -131,7 +134,6 @@ protected static function error(Exception $exception, $exception_type,
131134

132135
if ($error_message === '') {
133136
$error_message = Lang::get($key, [
134-
'error_code' => $api_code, // LEGACY!
135137
'api_code' => $api_code,
136138
'message' => $ex_message,
137139
'class' => get_class($exception),

0 commit comments

Comments
 (0)