Fixed ugly error when searching for a blank string

This commit is contained in:
Chris Kankiewicz
2020-02-17 16:29:55 -07:00
parent 4715974e29
commit 991a843cdb
5 changed files with 29 additions and 4 deletions

View File

@@ -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,
]);
}

View File

@@ -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,
]);
}
}

View File

@@ -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 &bull; Not Found
{{ code }} &bull; {{ message }}
</p>
</div>

View File

@@ -1,4 +1,5 @@
{% extends "layouts/app.twig" %}
{% set title = path == '.' ? 'Home' : path %}
{% block content %}
{% include "components/header.twig" %}

View File

@@ -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());
}
}