Skip to content

Commit 394ab97

Browse files
fix: Melhorar mensagens de erro para manipuladores de rota inválidos e garantir codificação JSON segura
1 parent eaf6239 commit 394ab97

5 files changed

Lines changed: 13 additions & 6 deletions

File tree

src/Core/Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ public function handle(?Request $request = null): Response
613613
// Buscar rotas disponíveis para suggestions
614614
$availableRoutes = array_map(
615615
fn($r) => "{$r['method']} {$r['path']}",
616-
array_slice($this->router::getAllRoutes(), 0, 10)
616+
array_slice($this->router::getRoutes(), 0, 10)
617617
);
618618

619619
throw ContextualException::routeNotFound(

src/Exceptions/Enhanced/ContextualException.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ private function generateDebugInfo(): void
113113
private function formatValue(mixed $value): string
114114
{
115115
if (is_array($value)) {
116-
return json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
116+
$result = json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
117+
return $result !== false ? $result : 'Array (unable to encode)';
117118
}
118119

119120
if (is_object($value)) {
@@ -128,7 +129,7 @@ private function formatValue(mixed $value): string
128129
return 'null';
129130
}
130131

131-
return (string) $value;
132+
return is_scalar($value) ? (string) $value : gettype($value);
132133
}
133134

134135
/**

src/Json/Pool/JsonBufferPool.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ public static function encodeWithPool(
147147
): string {
148148
// Usar threshold inteligente - para dados pequenos, json_encode é mais rápido
149149
if (!self::shouldUsePooling($data)) {
150-
return json_encode($data, $flags);
150+
$result = json_encode($data, $flags);
151+
if ($result === false) {
152+
throw new \JsonException('JSON encoding failed: ' . json_last_error_msg());
153+
}
154+
return $result;
151155
}
152156

153157
$optimalCapacity = self::getOptimalCapacity($data);

src/Utils/CallableResolver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ private static function resolveArrayCallable(array $handler): callable
8989
);
9090
}
9191

92+
/** @var callable */
9293
return [$objectOrClass, $method];
9394
}
9495

@@ -110,6 +111,7 @@ private static function resolveArrayCallable(array $handler): callable
110111
);
111112
}
112113

114+
/** @var callable */
113115
return [$objectOrClass, $method];
114116
}
115117

tests/Unit/Routing/ArrayCallableTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ function ($req, $res) {
194194
public function testInvalidArrayCallable(): void
195195
{
196196
$this->expectException(\InvalidArgumentException::class);
197-
$this->expectExceptionMessage('Handler must be a callable function');
197+
$this->expectExceptionMessage('Route handler validation failed: Method');
198198

199199
// Try to register an invalid callable
200200
Router::get('/invalid', [$this->controller, 'nonExistentMethod']);
@@ -206,7 +206,7 @@ public function testInvalidArrayCallable(): void
206206
public function testInvalidStaticCallable(): void
207207
{
208208
$this->expectException(\InvalidArgumentException::class);
209-
$this->expectExceptionMessage('Handler must be a callable function');
209+
$this->expectExceptionMessage('Route handler validation failed: Static method');
210210

211211
// Try to register an invalid static callable
212212
Router::get('/invalid', [TestController::class, 'nonExistentStaticMethod']);

0 commit comments

Comments
 (0)