mirror of
https://github.com/DirectoryLister/DirectoryLister.git
synced 2025-08-26 15:25:18 +02:00
Fixed ugly error when searching for a blank string
This commit is contained in:
@@ -51,14 +51,16 @@ class DirectoryHandler
|
||||
try {
|
||||
$files = $this->finder->in($path)->depth(0);
|
||||
} catch (DirectoryNotFoundException $exception) {
|
||||
return $this->view->render($response->withStatus(404), '404.twig');
|
||||
return $this->view->render($response->withStatus(404), 'error.twig', [
|
||||
'code' => 404,
|
||||
'message' => 'Not Found',
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->view->render($response, 'index.twig', [
|
||||
'files' => $files,
|
||||
'path' => $path,
|
||||
'readme' => $this->readme($files),
|
||||
'title' => $path == '.' ? 'Home' : $path,
|
||||
]);
|
||||
}
|
||||
|
||||
|
@@ -40,6 +40,13 @@ class SearchHandler
|
||||
{
|
||||
$search = $request->getQueryParams()['search'];
|
||||
|
||||
if (empty($search)) {
|
||||
return $this->view->render($response->withStatus(422), 'error.twig', [
|
||||
'code' => 422,
|
||||
'message' => 'Unprocessable Entity',
|
||||
]);
|
||||
}
|
||||
|
||||
$files = $this->finder->in('.')->name(
|
||||
sprintf('/(?:.*)%s(?:.*)/i', preg_quote($search, '/'))
|
||||
);
|
||||
@@ -47,7 +54,6 @@ class SearchHandler
|
||||
return $this->view->render($response, 'index.twig', [
|
||||
'files' => $files,
|
||||
'search' => $search,
|
||||
'title' => $search,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@@ -1,11 +1,12 @@
|
||||
{% extends 'layouts/app.twig' %}
|
||||
{% set title = message %}
|
||||
|
||||
{% block content %}
|
||||
{% include 'components/header.twig' %}
|
||||
|
||||
<div id="content" class="flex-grow container flex justify-center items-center mx-auto px-4">
|
||||
<p class="font-thin text-4xl text-gray-600">
|
||||
404 • Not Found
|
||||
{{ code }} • {{ message }}
|
||||
</p>
|
||||
</div>
|
||||
|
@@ -1,4 +1,5 @@
|
||||
{% extends "layouts/app.twig" %}
|
||||
{% set title = path == '.' ? 'Home' : path %}
|
||||
|
||||
{% block content %}
|
||||
{% include "components/header.twig" %}
|
||||
|
@@ -27,4 +27,19 @@ class SearchHandlerTest extends TestCase
|
||||
$this->assertInstanceOf(ResponseInterface::class, $response);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
public function test_it_returns_an_error_for_a_blank_search(): void
|
||||
{
|
||||
$this->container->call(TwigProvider::class);
|
||||
|
||||
$handler = new SearchHandler(new Finder, $this->container->get(Twig::class));
|
||||
|
||||
$request = $this->createMock(Request::class);
|
||||
$request->method('getQueryParams')->willReturn(['search' => '']);
|
||||
|
||||
$response = $handler($request, new Response);
|
||||
|
||||
$this->assertInstanceOf(ResponseInterface::class, $response);
|
||||
$this->assertEquals(422, $response->getStatusCode());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user