Skip to content

Commit

Permalink
added slim error handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Jun 29, 2017
1 parent e5353c3 commit 24451a8
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/SAREhub/Microt/App/ErrorHandlerProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Copyright 2017 SARE S.A
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

namespace SAREhub\Microt\App;

use Pimple\Container;
use SAREhub\Microt\Logger\LoggerProvider;
use SAREhub\Microt\Util\JsonResponse;

class ErrorHandlerProvider implements ServiceProvider {

const RUNTIME_ERROR_HANDLER_ENTRY = 'phpErrorHandler';
const ERROR_HANDLER = 'errorHandler';

public function register(Container $c) {
$c[self::RUNTIME_ERROR_HANDLER_ENTRY] = $c[self::ERROR_HANDLER] = function ($c) {
return function ($rq, $resp, \Throwable $e) use ($c) {
$c[LoggerProvider::ENTRY]->error($e->getMessage(), ['exception' => $e]);
return JsonResponse::wrap($c['response'])->internalServerError('exception occur');
};
};
}
}
38 changes: 38 additions & 0 deletions src/SAREhub/Microt/App/MethodNotAllowedHandlerProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Copyright 2017 SARE S.A
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

namespace SAREhub\Microt\App;


use Pimple\Container;
use SAREhub\Microt\Util\JsonResponse;

class MethodNotAllowedHandlerProvider implements ServiceProvider {

const ENTRY = 'notAllowedHandler';

public function register(Container $c) {
$c[self::ENTRY] = function ($c) {
return function ($rq, $resp, $methods) use ($c) {
return JsonResponse::wrap($c['response'])
->error('method not allowed', ['allowedMethods' => $methods], 405)
->withHeader('Allow', implode(', ', $methods));
};
};
}
}
38 changes: 38 additions & 0 deletions src/SAREhub/Microt/App/NotFoundHandlerProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Copyright 2017 SARE S.A
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

namespace SAREhub\Microt\App;


use Pimple\Container;
use SAREhub\Microt\Util\JsonResponse;
use Slim\Http\Request;
use Slim\Http\Response;

class NotFoundHandlerProvider implements ServiceProvider {

const ENTRY = 'notFoundHandler';

public function register(Container $c) {
$c[self::ENTRY] = function () {
return function (Request $rq, Response $resp) {
return JsonResponse::wrap($resp)->notFound('route not found check api documentation');
};
};
}
}

0 comments on commit 24451a8

Please sign in to comment.