B
B
bernex2016-02-28 12:15:41
PHP
bernex, 2016-02-28 12:15:41

How to properly handle errors for an API response?

The question arose, for a successful answer is always good and convenient to give?
For example, interrupt at:
throw new Exception(77, 'Не верный формат!')
and the answer will be:

{success: false, message: 'Не верный формат!', errorCode: 77}

or directly in the controller:
return [ "success" => false, "message" => 'Не верный формат!', "errorCode" => 77];

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Nikolaev, 2016-02-28
@gromdron

And if rest is used, then you can return just text and a 400 error.

I
Ilya Bobkov, 2016-02-28
@heksen

I have so:

function error($string)
{
 $arr = array('answer' => 'error', 'string' => $string);
 echo json_encode($arr);
 exit;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question