forked from matecat/MateCat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.php
78 lines (67 loc) · 3.12 KB
/
router.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
use API\V2\Exceptions\AuthenticationError;
use API\V2\Exceptions\AuthorizationError;
use API\V2\Exceptions\ExternalServiceException;
use API\V2\Exceptions\ValidationError;
use API\V2\Json\Error;
use Exceptions\NotFoundException;
use Exceptions\ValidationError as Model_ValidationError;
require_once './inc/Bootstrap.php';
Bootstrap::start();
$klein = new \Klein\Klein();
function route( $path, $method, $controller, $action ) {
global $klein;
$klein->respond( $method, $path, function () use ( $controller, $action ) {
$reflect = new ReflectionClass( $controller );
$instance = $reflect->newInstanceArgs( func_get_args() );
$instance->respond( $action );
} );
}
$klein->onError( function ( \Klein\Klein $klein, $err_msg, $err_type, Exception $exception ) {
// TODO: still need to catch fatal errors here with 500 code
$klein->response()->noCache();
try {
throw $exception;
} catch( InvalidArgumentException $e ){
$klein->response()->code( 400 );
$klein->response()->json( ( new Error( [ $e ] ) )->render() );
} catch ( AuthenticationError $e ) {
$klein->response()->code( 401 );
$klein->response()->json( ( new Error( [ $e ] ) )->render() );
} catch ( Model_ValidationError $e ) {
$klein->response()->code( 400 );
$klein->response()->json( ( new Error( [ $e ] ) )->render() );
} catch ( ValidationError $e ) {
$klein->response()->code( 400 );
$klein->response()->json( ( new Error( [ $e ] ) )->render() );
} catch ( AuthorizationError $e ) {
$klein->response()->code( 403 );
$klein->response()->json( ( new Error( [ $e ] ) )->render() );
} catch ( DomainException $e ) {
$klein->response()->code( 403 );
$klein->response()->json( ( new Error( [ $e ] ) )->render() );
} catch ( NotFoundException $e ) {
\Log::doJsonLog( 'Record Not found error for URI: ' . $_SERVER[ 'REQUEST_URI' ] );
$klein->response()->code( 404 );
$klein->response()->json( ( new Error( [ $e ] ) )->render() );
} catch ( ExternalServiceException $e ) {
$klein->response()->code( 503 );
$klein->response()->json( ( new Error( [ $e ] ) )->render() );
} catch ( \PDOException $e ) {
$klein->response()->code( 503 );
// $klein->response()->json( ( new Error( [ $e ] ) )->render() );
\Utils::sendErrMailReport( $exception->getMessage() . "" . $exception->getTraceAsString(), 'Generic error' );
\Log::doJsonLog( [ "error" => $exception->getMessage(), "trace" => $exception->getTrace() ] );
} catch ( Exception $e ){
$klein->response()->code( 500 );
\Utils::sendErrMailReport( $exception->getMessage() . "" . $exception->getTraceAsString(), 'Generic error' );
\Log::doJsonLog( [ "error" => $exception->getMessage(), "trace" => $exception->getTrace() ] );
}
} );
require './lib/Routes/api_v1_routes.php';
require './lib/Routes/api_v2_routes.php';
require './lib/Routes/api_v3_routes.php';
require './lib/Routes/gdrive_routes.php';
require './lib/Routes/utils_routes.php';
Features::loadRoutes( $klein );
$klein->dispatch();