Skip to content

Commit 2189a9f

Browse files
committed
app solution moved to the plugin
1 parent 54a0203 commit 2189a9f

File tree

6 files changed

+52
-35
lines changed

6 files changed

+52
-35
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ The recommended way to install composer packages is:
99
```
1010
composer require rrd108/cakephp-json-api-exception
1111
```
12+
13+
```
14+
bin/cake plugin load JsonApiException
15+
```

src/Controller/AppController.php

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace JsonApiException\Error\Exception;
4+
5+
use Cake\Datasource\EntityInterface;
6+
use Cake\Http\Exception\NotAcceptableException;
7+
8+
class JSonApiException extends NotAcceptableException
9+
{
10+
protected $requestErrors;
11+
12+
public function __construct(EntityInterface $entity, $message = null, $code = 422)
13+
{
14+
$this->requestErrors = $entity->getErrors();
15+
16+
if ($message === null) {
17+
$message = 'Bad Request';
18+
}
19+
20+
parent::__construct($message, $code);
21+
}
22+
23+
public function getRequestErrors()
24+
{
25+
return $this->requestErrors;
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace JsonApiException\Error;
4+
5+
use Cake\Error\ExceptionRenderer;
6+
7+
class JsonApiExceptionRenderer extends ExceptionRenderer
8+
{
9+
public function jsonApi($exception)
10+
{
11+
$response = $this->controller->getResponse();
12+
return $response
13+
->withStringBody(json_encode(['errors' => $exception->getRequestErrors()]))
14+
->withStatus(406)
15+
->withType('application/json');
16+
}
17+
}

src/Plugin.php

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace JsonApiException;
56

7+
use Cake\Core\Configure;
68
use Cake\Core\BasePlugin;
7-
use Cake\Core\PluginApplicationInterface;
89
use Cake\Http\MiddlewareQueue;
9-
use Cake\Routing\RouteBuilder;
10+
use Cake\Core\PluginApplicationInterface;
1011

1112
/**
1213
* Plugin for JsonApiException
@@ -24,29 +25,7 @@ class Plugin extends BasePlugin
2425
*/
2526
public function bootstrap(PluginApplicationInterface $app): void
2627
{
27-
}
28-
29-
/**
30-
* Add routes for the plugin.
31-
*
32-
* If your plugin has many routes and you would like to isolate them into a separate file,
33-
* you can create `$plugin/config/routes.php` and delete this method.
34-
*
35-
* @param \Cake\Routing\RouteBuilder $routes The route builder to update.
36-
* @return void
37-
*/
38-
public function routes(RouteBuilder $routes): void
39-
{
40-
$routes->plugin(
41-
'JsonApiException',
42-
['path' => '/json-api-exception'],
43-
function (RouteBuilder $builder) {
44-
// Add custom routes here
45-
46-
$builder->fallbacks();
47-
}
48-
);
49-
parent::routes($routes);
28+
Configure::write('Error.exceptionRenderer', 'JsonApiException\Error\JsonApiExceptionRenderer');
5029
}
5130

5231
/**

webroot/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)