Skip to content

Commit acb8a5c

Browse files
author
dan
committed
Added uncaught exception handler
1 parent 8c439f0 commit acb8a5c

File tree

3 files changed

+15
-38
lines changed

3 files changed

+15
-38
lines changed

API_Server/API_Server.php

+14-34
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ class WebService{
1414

1515
private $valid_key;
1616

17-
public function __construct($URL_client){
17+
public function __construct(){
1818
spl_autoload_register(array($this, 'class_loader'));
1919
}
2020

21-
2221
private function HTML_entities(&$array){
2322
foreach($array as $k=>$v){
2423
if(is_array($v)) {
@@ -47,13 +46,8 @@ private function write_debugger($data_array){
4746
fclose($h);
4847
}
4948

50-
51-
52-
//protected function shutdown_function(){
5349
function __destruct(){
5450
$error=error_get_last();
55-
//$this->write_debugger(array('fff'));
56-
//*
5751
if($error!==NULL){
5852
$error_codes=array(
5953
'1'=>"E_ERROR 1 A fatal run-time error, that can't be recovered from. The execution of the script is stopped immediately.",
@@ -74,9 +68,9 @@ function __destruct(){
7468
//32767 E_ALL 32767 All errors and warnings, except of level E_STRICT prior to PHP 5.4.0.
7569
);
7670
if(!headers_sent() && isset($error_codes[$error['type']])) {
77-
$this->write_debugger(array("FATAL_ERROR:", $er, $error));
71+
$this->write_debugger(array("FATAL_ERROR:", $error));
7872
$response=json_encode(array('ResponseCode'=>500,'Message'=>$error));
79-
$this->outPut($response);
73+
self::outPut($response);
8074
}else{
8175
$this->write_debugger(array("UNKNOWN ERROR:", $error, $_POST));
8276
}
@@ -102,6 +96,7 @@ public function process_request(){
10296
if(is_array($response)) {
10397
$this->HTML_entities($response);
10498
$response=json_encode($response);
99+
105100
}
106101
else{
107102
$ERROR['Message']='The result of the method is not an array';
@@ -114,10 +109,8 @@ public function process_request(){
114109
}
115110
}
116111
catch (Exception $e){
117-
//$this->write_debugger(array($e));
118112
$ERROR['Message']=$e->getMessage();
119113
}
120-
121114
}
122115
else{
123116
$ERROR['Message']='Endpoint:' . $_POST['endpoint'] . ', is invalid.';
@@ -129,30 +122,25 @@ public function process_request(){
129122
if($response==''){
130123
$response=json_encode($ERROR);
131124
}
132-
$this->outPut($response);
125+
126+
self::outPut($response);
133127
}
134128

135-
136-
protected function class_loader($class){
137-
/*
138-
$file='classes/' . $class . '.class.php';
139-
if(file_exists($file)){
140-
include_once $file;
141-
}else {
142-
include_once strtolower($file);
143-
}
144-
/**/
129+
public static function Uncaught_Exception($exception){
130+
$error=$exception->getMessage() . ' File: ' . $exception->getFile() . ' Line: ' . $exception->getLine();
131+
$response=json_encode(array('ResponseCode'=>500,'Message'=>$error));
132+
self::outPut($response);
145133
}
146134

147-
protected function outPut(&$response){
148-
/*
135+
protected function class_loader($class){}
136+
137+
protected static function outPut(&$response){
149138
ob_clean();
150139
ob_start();
151140
header('Content-Type: application/json');
152141
header('Content-Length: ' . strlen($response));
153142
print $response;
154143
ob_end_flush();
155-
/**/
156144
}
157145
}
158146

@@ -167,20 +155,12 @@ protected function class_loader($class){
167155
}
168156
}
169157

170-
protected function outPut(&$response){
171-
ob_clean();
172-
ob_start();
173-
header('Content-Type: application/json');
174-
header('Content-Length: ' . strlen($response));
175-
print $response;
176-
ob_end_flush();
177-
}
178158
}
179159

160+
set_exception_handler('WebService2::Uncaught_Exception');
180161

181162
$web_service=new WebService2();
182163

183164
$web_service->process_request();
184165

185-
//
186166
?>

API_Server/classes/demo.class.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
<?php
22

3-
4-
5-
63
class Demo {
74

85
private function error($id){
@@ -42,6 +39,7 @@ public function get_data($str){
4239
$returnarray=array('ResponseCode'=>200,
4340
'ResponseMessage'=>'',
4441
'Content'=>$this->$str);
42+
//throw new zc\Exception('sfsdfsdf');
4543
}
4644
return $returnarray;
4745
}

API_client.php

-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ public function Resquest($endpoint, $params, $const_param=null){
7272
if(!$result=@ json_decode($this->body, true)){
7373
$result['ResponseCode']=$this->status_code;
7474
$result['Meta']=$this->response_headers;
75-
//$this->write_log(array('dddd'));
7675
}
7776
}else {
7877
$result['Message']='Error Code: ' . curl_errno($ch) . ' Error: ' . curl_error($ch);

0 commit comments

Comments
 (0)