The preface
Whether micro blog or Taobao, they have their own error return value format specification, as well as error code description, so not only convenient to use the mobile phone, give a person the feeling of clarity, lofty. If you have a problem, go to the parent company. To this end, I imitated the error return value format of Taobao, customized my own error code according to the standard of Weibo error code, and then tested it on Restful API. Below I will implement the idea and test results to share with you.
Implementation approach
I use the abstract factory pattern to implement such an error return value. This pattern was chosen because it provides an interface to create a series of related or interdependent objects, which is close to what I need.
The code analysis
1. With the path common\ Hint, I created an error folder to store my error prompt files. There are several files in this folder:
Hint.php entry file. Define an abstract class with only one method in it.
interface Hint {
function Error($_errors,$code);
}Copy the code
3, the template.php implementation hints this interface. The format of the error return value is defined here.
class Template implements Hint{ function Error($_errors,$code) { if (empty($_errors)) { print_r(json_encode([])); } else { $errors['error']['name'] = 'Not Found'; $errors['error']['message'] = $_errors; $errors['error']['error_code'] = $code; print_r(json_encode($errors)); }}}Copy the code
Createmsg. PHP create a createMsg abstract class. Abstract the creation of an object as an interface.
interface createMsg {
function Msg();
}Copy the code
5. Implement the createMsg interface using the FactoryMsg class. Returns the instantiated Template.
class FactoryMsg implements createMsg{ function Msg() { return new Template; }}Copy the code
Errormsg. PHP passes a parameter to the Error method in Template.
Public static function Info($_errors) {$Factory = new FactoryMsg; $result = strstr($_errors,Yii::t('yii','Not exist')); $result1 = STRSTR ($_errors,Yii::t(' Yii ','Null')); $result1 = STRSTR ($_errors,Yii::t(' Yii ','Null')); $result2 = STRSTR ($_errors,Yii::t(' Yii ','Fail')); $result3 = STRSTR ($_errors,Yii::t(' Yii ','Not right')); $result4 = STRSTR ($_errors,Yii::t(' Yii ','Robc')); 20005 // Data does not exist 20001 if(! empty($result)){ $M = $Factory->Msg(); $M->Error($_errors,'20001'); die; } // Parameter cannot be empty 20002 if(! empty($result1)){ $M = $Factory->Msg(); $M->Error($_errors,'20002'); die; } // Add, update, delete failed 20003 if(! empty($result2)){ $M = $Factory->Msg(); $M->Error($_errors,'20003'); die; } //XX not correct 20004 if(! empty($result3)){ $M = $Factory->Msg(); $M->Error($_errors,'20004'); die; } //XX no permission 20005 if(! empty($result4)){ $M = $Factory->Msg(); $M->Error($_errors,'20005'); die; $M = $Factory->Msg(); $M->Error($_errors,'21000'); }}Copy the code
7. Call method.
use common\hint\error\ErrorMsg;
ErrorMsg::Info(Yii::t('yii','failure'));Copy the code
8. Test results.
{" error ": {" name" : "Not Found", "message" : "fail", "error_code" : "20003"}}Copy the code
To complete. The whole implementation process I use the form of language package, which is conducive to the later multi-language switch.
Q&A
1, the use of this string fuzzy search is very general, can not meet the specific error type return corresponding to the specific code requirements. If you have better suggestions, we welcome them.
$result = strstr($_errors,Yii::t('yii','Not exist'));Copy the code
2. The implementation process did not consider the problem of multi-language switching in the future, and then directly used the traditional way to send hints. ErrorMsg::Info(” operation failed “); You can’t switch languages that way. It is recommended that you use the language package to pass the parameter.
The relevant data
1, weibo open platform: open.weibo.com/wiki/Error_… 2, taobao open platform: open.taobao.com/doc2/apiDet… 3, PHP simple factory schema, factory method schema and abstract factory schema comparison: www.phpddt.com/php/php-fac…