Skip to content

Commit

Permalink
add glued-lib error handling, fix exceptions and null returns on quer…
Browse files Browse the repository at this point in the history
…y and
  • Loading branch information
killua-eu committed Jan 23, 2024
1 parent 4f1ddca commit 1241867
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
17 changes: 12 additions & 5 deletions glued/Controllers/IfController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function __construct(ContainerInterface $c)

private function transform($data)
{
$objs = [];
$transformer = new ArrayTransformer();
$transformer
->set('domicile', 'CZ')
Expand Down Expand Up @@ -82,6 +83,9 @@ private function fetch(string $q) : mixed
$uri = 'https://ares.gov.cz/ekonomicke-subjekty-v-be/rest/ekonomicke-subjekty/vyhledat';
$content = '{"obchodniJmeno":"' . $q . '","pocet":10,"start":0,"razeni":[]}';
$key = hash('md5', $uri . $content);
if (mb_strlen($q, 'UTF-8') < 2) {
throw new \Exception('Query string too short', 400);
}

if ($this->fscache->has($key)) {
$response = $this->fscache->get($key);
Expand All @@ -91,6 +95,7 @@ private function fetch(string $q) : mixed
$f['save'] = $base = $this->settings['glued']['protocol'].$this->settings['glued']['hostname'].$this->settings['routes']['be_contacts_import_v1']['path'] . '/';
$f['save'] .= "$this->action/$fid";
}
//if ($final == 'null') { throw new \Exception('At least two characters must be provided', 400); }
return $final;
}

Expand Down Expand Up @@ -129,9 +134,10 @@ private function fetch(string $q) : mixed
$result = $sstmt->get_result();
$res = $result->fetch_all(MYSQLI_ASSOC);
$sstmt->close();
$this->fscache->set($key, $response, 3600);
*/

//$this->fscache->set($key, $response, 3600);
//if ($final == 'null') { throw new \Exception('At least two characters must be provided', 400); }
return $final;
}

Expand All @@ -154,10 +160,11 @@ private function map(&$i,$ip,&$o,$op) {
public function act_r1(Request $request, Response $response, array $args = []): Response {
$action = 'd65d2468-afe0-40c2-986c-e67047141013';
$data = $this->fetch($args['q']);
$objs = $data;
//$objs = $this->transform($data);

return $response->withJson($objs);
$res = [
'results' => count($data),
'data' => $data
];
return $response->withJson($res);
}


Expand Down
38 changes: 11 additions & 27 deletions glued/middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types=1);
use DI\Container;
use Glued\Lib\Middleware\TimerMiddleware;
use Middlewares\TrailingSlash;
use Glued\Lib\Middleware\TrailingSlash;
use Nyholm\Psr7\Response as Psr7Response;
use Slim\App;
use Slim\Exception\HttpNotFoundException;
Expand Down Expand Up @@ -32,10 +32,15 @@

// TrailingSlash(false) removes the trailing from requests, for example
// `https://example.com/user/` will change into https://example.com/user.
// Optionally, setting redirect(true) enforces a 301 redirect.
//$trailingSlash = new TrailingSlash(false);
//$trailingSlash->redirect();
//$app->add($trailingSlash);
// Setting redirect(true) enforces a 301 redirect. The second parameter
// in TrailingSlash controls the inclusion of the port the Location header
// of the redirect. Not including the port is wanted as this microservice
// is supposed to run behind the nginx+glued-core as its auth proxy, otherwise
// users would get redirected directly to the backend port.
// TODO: fix glued-lib trailingSlash to do only path-based redirects (no full url)
$trailingSlash = new TrailingSlash(false, false);
$trailingSlash->redirect();
$app->add($trailingSlash);


// RoutingMiddleware provides the FastRoute router. See
Expand All @@ -54,28 +59,7 @@

// Error handling middleware. This middleware must be added last. It will not handle
// any exceptions/errors for middleware added after it.
$jsonErrorHandler = function ($exception, $inspector) {
global $settings;
header("Content-Type: application/json");
$r['code'] = $exception->getCode();
$r['message'] = $exception->getMessage();
$r['title'] = $inspector->getExceptionName() ;
$r['file'] = $exception->getFile() . ' ' . $exception->getLine();
$short = explode('\\', $r['title']);
$short = (string) array_pop($short);
$r['hint'] = "No hints, sorry.";
$http = '500 Internal Server Error';

if ($short == "AuthJwtException") { $http = '401 Unauthorized'; $r['hint'] = "Login at ".$settings['oidc']['uri']['login']; }
if ($short == "AuthTokenException") { $http = '401 Unauthorized'; $r['hint'] = "Login at ".$settings['oidc']['uri']['login']; }
if ($short == "HttpNotFoundException") { $http = '404 Not fond'; }
if ($r['message'] == "MSSQL error.") { $r['hint'] = sqlsrv_errors(); }

header($_SERVER['SERVER_PROTOCOL'].' '.$http);
echo json_encode($r, JSON_UNESCAPED_SLASHES);
exit;
};

$jsonErrorHandler = require_once(__ROOT__ . '/vendor/vaizard/glued-lib/src/Includes/json_error_handler.php');
$app->add(new Zeuxisoo\Whoops\Slim\WhoopsMiddleware([
'enable' => true,
'editor' => 'phpstorm',
Expand Down

0 comments on commit 1241867

Please sign in to comment.