0

I am working on a restApi and i am using this api for my mobile and web development.which is build in PHP.i need my error in json format so that i can send this error in a proper response format to android developer and they can also easily handle it.so we will not face any crashing issue in our mobile app.

this is what i need exactly.

PHP ERROR:

Parse error: syntax error, unexpected 'switch' (T_SWITCH) in /var/www/html/phishem/index.php on line 68

What need:

{error_code:"403",message:"Parse error: syntax error, unexpected 'switch' (T_SWITCH) in /var/www/html/phishem/index.php on line 68"}

So how i convert this php error in json response.

lofihelsinki
  • 2,491
  • 2
  • 23
  • 35
Shakti Sisodiya
  • 218
  • 2
  • 5
  • 14
  • Thats Error in your Php Code. Not an Error in Android – Tomin B Azhakathu Jan 11 '18 at 05:59
  • yes @TominB i know this. ultimately what i need. i need to send any proper json response to mobile application if any mysql error, php error , or any other error occurs in server side code. – Shakti Sisodiya Jan 11 '18 at 06:01
  • use try catch and use json_encode for the error – Tomin B Azhakathu Jan 11 '18 at 06:03
  • but have to do it in every methods. is there any setting in php configuration.that i can get error on a file.like: how log files writes the error. how laravel and other MVC frameworks manage errors. can we do this manually. – Shakti Sisodiya Jan 11 '18 at 06:07
  • A parse error means your code cannot compile. Accordingly, it doesn't run. The errors that report an unexpected [PHP keyword](http://php.net/manual/en/language.control-structures.php) usually signals a missing semicolon (`;`) before the reported keyword. Fix the syntax errors. The errors, warnings and notices reported by the PHP interpreter are of no use to the Android front-end. They signal problems in the PHP code. – axiac Jan 11 '18 at 07:49

3 Answers3

0

In api code you should haldle php warnings and errors, I don't think it is good programinig practice to handle php exceptions and errors in Front end(Android or IOS)

ShuBham GuPta
  • 194
  • 2
  • 9
  • nope we are not handling error mobile side. sometimes we faced the issue that if error in database query or whatever the type of error. obsly mobile application receive a bad json format. that time at least we can send a proper error message so that. we can save our app to crash. – Shakti Sisodiya Jan 11 '18 at 06:00
0

That kind of error is a Server Side Error, in which must be solve by handling the error instead of giving it as a proper response. Ideally, you must handle/catch all of the possible errors and give a proper response.

OR

You can also develop a service layer (also for a more secured architecture), that will pass through the request and response between the front-end side and the API side. In this case, you can parse a proper response message from the service layer if the server response is 500 which is Internal Server Error and return a JSON formatted message.

Tenten Ponce
  • 2,436
  • 1
  • 13
  • 40
0

You can catch fatal errors and output as JSON like this (in PHP):

function my_error_handler(){
    $error = error_get_last();
    header('Content-Type: application/json');
    echo json_encode($error);
}

register_shutdown_function('my_error_handler');

For non-fatal errors, you need to add this

set_error_handler('my_error_handler'); 

Additionally, you need to suppress the default error messaging:

error_reporting(0);

These will output the error as the following JSON

{ type : "8", "message" : "Undefined variable: a", "file" : "C:\WWW\index.php", "line" : "2" }

lofihelsinki
  • 2,491
  • 2
  • 23
  • 35