Skip to content

Commit fbe8db1

Browse files
committed
Improve error messages
1 parent 70966b3 commit fbe8db1

File tree

6 files changed

+28
-12
lines changed

6 files changed

+28
-12
lines changed

src/Concerns/ValidatesKvStore.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace App\Concerns;
66

7+
use App\Enums\HttpStatusEnum;
78
use React\Http\Message\Response;
89

910
trait ValidatesKvStore
@@ -17,14 +18,25 @@ trait ValidatesKvStore
1718
private function validateKeyOrError(string $key): ?Response
1819
{
1920
if (empty($key)) {
20-
return Response::plaintext('Key is required')->withStatus(400);
21+
return $this->createValidationErrorResponse('Key is required');
2122
}
2223

2324
// Check for lowercase snake_case format
2425
if (!preg_match('/^[a-z0-9]+(_[a-z0-9]+)*$/', $key)) {
25-
return Response::plaintext('Key must be in lowercase snake_case format')->withStatus(400);
26+
return $this->createValidationErrorResponse('Key must be in lowercase snake_case format');
2627
}
2728

2829
return null;
2930
}
31+
32+
/**
33+
* Create a validation error response
34+
*/
35+
private function createValidationErrorResponse(string $message): Response
36+
{
37+
$status = HttpStatusEnum::bad_request;
38+
39+
return Response::plaintext("Error {$status->value}: $message")
40+
->withStatus($status->value);
41+
}
3042
}

src/Controllers/KvStore/DeleteKeyController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Controllers\KvStore;
66

77
use App\Concerns\ValidatesKvStore;
8+
use App\Enums\HttpStatusEnum;
89
use App\Exceptions\KvStoreException;
910
use App\Services\KvStoreService;
1011
use Psr\Http\Message\ServerRequestInterface;
@@ -32,12 +33,12 @@ public function __invoke(ServerRequestInterface $request)
3233

3334
return $this->kvStore->delete($key)->then(
3435
function () {
35-
return Response::plaintext('OK')->withStatus(200);
36+
return Response::plaintext('')->withStatus(HttpStatusEnum::no_content->value);
3637
},
3738

3839
function (KvStoreException $e) {
3940
error_log("Error deleting key: " . $e->getMessage());
40-
return Response::plaintext('Internal Server Error')->withStatus(500);
41+
return Response::plaintext('')->withStatus(HttpStatusEnum::internal_server_error->value);
4142
}
4243
);
4344
}

src/Controllers/KvStore/GetKeyController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Controllers\KvStore;
66

77
use App\Concerns\ValidatesKvStore;
8+
use App\Enums\HttpStatusEnum;
89
use App\Exceptions\KvStoreException;
910
use App\Services\KvStoreService;
1011
use Psr\Http\Message\ServerRequestInterface;
@@ -33,15 +34,15 @@ public function __invoke(ServerRequestInterface $request)
3334
return $this->kvStore->get($key)->then(
3435
function ($result) {
3536
if (empty($result->rows)) {
36-
return Response::plaintext('')->withStatus(404);
37+
return Response::plaintext('')->withStatus(HttpStatusEnum::not_found->value);
3738
}
3839

3940
return Response::plaintext($result->rows[0]['value']);
4041
},
4142

4243
function (KvStoreException $e) {
4344
error_log("Error getting key: " . $e->getMessage());
44-
return Response::plaintext('Internal Server Error')->withStatus(500);
45+
return Response::plaintext('')->withStatus(HttpStatusEnum::internal_server_error->value);
4546
}
4647
);
4748
}

src/Controllers/KvStore/ListKeysController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace App\Controllers\KvStore;
66

7+
use App\Enums\HttpStatusEnum;
78
use App\Exceptions\KvStoreException;
89
use App\Services\KvStoreService;
9-
use Psr\Http\Message\ServerRequestInterface;
1010
use React\Http\Message\Response;
1111

1212
class ListKeysController
@@ -35,8 +35,8 @@ function ($result) {
3535

3636
function (KvStoreException $e) {
3737
error_log("Error listing keys: " . $e->getMessage());
38-
Response::plaintext('Internal Server Error')->withStatus(500);
39-
}
38+
Response::plaintext('')->withStatus(HttpStatusEnum::internal_server_error->value);
39+
}
4040
);
4141
}
4242
}

src/Controllers/KvStore/SetKeyController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Controllers\KvStore;
66

77
use App\Concerns\ValidatesKvStore;
8+
use App\Enums\HttpStatusEnum;
89
use App\Exceptions\KvStoreException;
910
use App\Services\KvStoreService;
1011
use Psr\Http\Message\ServerRequestInterface;
@@ -34,12 +35,12 @@ public function __invoke(ServerRequestInterface $request)
3435

3536
return $this->kvStore->set($key, $value)->then(
3637
function () {
37-
return Response::plaintext('OK')->withStatus(200);
38+
return Response::plaintext('')->withStatus(HttpStatusEnum::no_content->value);
3839
},
3940

4041
function (KvStoreException $e) {
4142
error_log("Error setting key: " . $e->getMessage());
42-
return Response::plaintext('Internal Server Error')->withStatus(500);
43+
return Response::plaintext('')->withStatus(HttpStatusEnum::internal_server_error->value);
4344
}
4445
);
4546
}

src/Middleware/PlainTextErrorResponseMiddleware.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ private function handle(ResponseInterface $response): ResponseInterface
2727
{
2828
$statusCode = $response->getStatusCode();
2929

30-
if ($statusCode >= 400) {
30+
// Status 400 is used for validation errors and should be excluded
31+
if ($statusCode >= 401) {
3132
$statusEnum = HttpStatusEnum::tryFrom($statusCode);
3233

3334
$description = $statusEnum ? $statusEnum->getDescription() : 'Error';

0 commit comments

Comments
 (0)