Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 46c6263

Browse files
committed
Fix API messages format
1 parent 4137a37 commit 46c6263

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

src/Api/External/Controller/ApiController.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Api\External\Controller;
66

77
use App\Api\External\Data\ErrorBucket;
8+
use App\Api\External\Exception\HttpException;
89
use Psr\Http\Message\ResponseFactoryInterface;
910
use Psr\Http\Message\ResponseInterface;
1011
use Psr\Http\Message\ServerRequestInterface;
@@ -15,6 +16,7 @@
1516
use roxblnfk\SmartStream\SmartStreamFactory;
1617
use RuntimeException;
1718
use Throwable;
19+
use Yiisoft\Http\Status;
1820
use Yiisoft\Injector\Injector;
1921

2022
abstract class ApiController implements MiddlewareInterface
@@ -38,7 +40,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
3840
try {
3941
$callable = [$this, $request->getMethod()];
4042
if (!is_callable($callable)) {
41-
throw new RuntimeException('Method not allowed.');
43+
throw new HttpException(Status::METHOD_NOT_ALLOWED, 'Method not allowed.');
4244
}
4345
$data = $this->injector->invoke($callable, [$request]);
4446
} catch (Throwable $e) {
@@ -48,7 +50,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
4850
}
4951

5052
protected function errorToBucket(Throwable $error): DataBucket {
51-
return new ErrorBucket($error, true);
53+
$bucket = new ErrorBucket($error, false);
54+
if ($error instanceof HttpException) {
55+
$bucket = $bucket->withStatusCode($error->getStatus());
56+
}
57+
return $bucket;
5258
}
5359

5460
protected function prepareResponse($data, ?ServerRequestInterface $request = null): ResponseInterface

src/Api/External/Data/ApiBucket.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,14 @@
88

99
class ApiBucket extends DataBucket
1010
{
11+
public function getData(): array
12+
{
13+
return [
14+
'success' => true,
15+
'status' => $this->getStatusCode() ?? 200,
16+
'data' => $this->data,
17+
'code' => null,
18+
'message' => null,
19+
];
20+
}
1121
}

src/Api/External/Data/ErrorBucket.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ public function __construct(\Throwable $error, bool $modeDev = false)
1818

1919
public function getData(): array
2020
{
21-
$data = [
22-
'code' => $this->error->getCode(),
23-
'message' => $this->error->getMessage(),
24-
];
21+
$data = parent::getData();
22+
$data['success'] = false;
23+
$data['data'] = null;
24+
$data['code'] = $this->error->getCode();
25+
$data['message'] = $this->error->getMessage();
2526
if ($this->modeDev) {
2627
$data['file'] = $this->error->getFile();
2728
$data['line'] = $this->error->getLine();
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Api\External\Exception;
6+
7+
use Exception;
8+
use Throwable;
9+
10+
class HttpException extends Exception
11+
{
12+
private int $status;
13+
public function __construct(int $status = 500, $message = "", $code = 0, Throwable $previous = null)
14+
{
15+
parent::__construct($message, $code, $previous);
16+
$this->status = $status;
17+
}
18+
public function getStatus(): int
19+
{
20+
return $this->status;
21+
}
22+
}

0 commit comments

Comments
 (0)