mirror of
https://github.com/opensupports/opensupports.git
synced 2025-07-25 23:05:22 +02:00
Use custom exception on response
This commit is contained in:
parent
ace895a4a2
commit
ca80e3ed1f
@ -19,7 +19,7 @@ abstract class Controller {
|
|||||||
$this->validate();
|
$this->validate();
|
||||||
$this->handler();
|
$this->handler();
|
||||||
} catch (\Exception $exception) {
|
} catch (\Exception $exception) {
|
||||||
Response::respondError($exception->getMessage());
|
Response::respondError($exception->getMessage(), $exception);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
<?php
|
<?php
|
||||||
class RequestException extends Exception {}
|
class RequestException extends Exception {}
|
||||||
|
@ -1,28 +1,31 @@
|
|||||||
<?php
|
<?php
|
||||||
class Response {
|
class Response {
|
||||||
|
private static $response;
|
||||||
|
private static $responseException;
|
||||||
|
|
||||||
public static function respondError($errorMsg, $data = null) {
|
public static function respondError($errorMsg, $exception = null) {
|
||||||
$response = array(
|
self::$response = array(
|
||||||
'status' => 'fail',
|
'status' => 'fail',
|
||||||
'message' => $errorMsg,
|
'message' => $errorMsg,
|
||||||
'data' => $data
|
'data' => null
|
||||||
);
|
);
|
||||||
|
self::$responseException = $exception;
|
||||||
|
|
||||||
$app = \Slim\Slim::getInstance();
|
$app = \Slim\Slim::getInstance();
|
||||||
$app->response->headers->set('Content-Type', 'application/json');
|
$app->response->headers->set('Content-Type', 'application/json');
|
||||||
$app->response->setBody(json_encode($response));
|
$app->response->setBody(json_encode(self::$response));
|
||||||
$app->response->finalize();
|
$app->response->finalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function respondSuccess($data = null) {
|
public static function respondSuccess($data = null) {
|
||||||
$response = array(
|
self::$response = array(
|
||||||
'status' => 'success',
|
'status' => 'success',
|
||||||
'data' => $data
|
'data' => $data
|
||||||
);
|
);
|
||||||
|
|
||||||
$app = \Slim\Slim::getInstance();
|
$app = \Slim\Slim::getInstance();
|
||||||
$app->response->headers->set('Content-Type', 'application/json');
|
$app->response->headers->set('Content-Type', 'application/json');
|
||||||
$app->response->setBody(json_encode($response));
|
$app->response->setBody(json_encode(self::$response));
|
||||||
$app->response->finalize();
|
$app->response->finalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,17 +8,17 @@ class ResponseTest extends TestCase {
|
|||||||
public function testErrorResponseFormat() {
|
public function testErrorResponseFormat() {
|
||||||
//Mock data
|
//Mock data
|
||||||
$mockErrorValue = 'MOCK_ERROR_VALUE';
|
$mockErrorValue = 'MOCK_ERROR_VALUE';
|
||||||
$mockData = array('example' => true);
|
$mockException = array('example' => true);
|
||||||
$expectedArgument = json_encode(array(
|
$expectedArgument = json_encode(array(
|
||||||
'status' => 'fail',
|
'status' => 'fail',
|
||||||
'message' => $mockErrorValue,
|
'message' => $mockErrorValue,
|
||||||
'data' => $mockData
|
'data' => null
|
||||||
));
|
));
|
||||||
$responseInstance = \Slim\Slim::getInstance();
|
$responseInstance = \Slim\Slim::getInstance();
|
||||||
$responseInstance = $responseInstance->response;
|
$responseInstance = $responseInstance->response;
|
||||||
|
|
||||||
//Execute Response
|
//Execute Response
|
||||||
Response::respondError($mockErrorValue, $mockData);
|
Response::respondError($mockErrorValue, $mockException);
|
||||||
|
|
||||||
//Should have been called with expected format
|
//Should have been called with expected format
|
||||||
$this->assertTrue($responseInstance->setBody->hasBeenCalledWithArgs($expectedArgument));
|
$this->assertTrue($responseInstance->setBody->hasBeenCalledWithArgs($expectedArgument));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user