The format of the modifiable exception returned by Yii is very limited and inflexible, where we need to modify the framework's code to implement it.
Since it is an exception, then we need to modify the response file:/home/gcheng/workshop/basic/vendor/yiisoft/yii2/web/errorhandler
Our goal is to change the exception format to the following format:
{" Exception": [ { "code": -3, "name": "Decryption Error" } ]}
So relative to the original format we want to remove the default Code,name and type
Throws an exception using the following format, and throws an exception that inherits from Userexception (Yii is used to throw user-customized exceptions)
throw new Userexception (Yii:: $app->zykexception->errormessage (-6));
In the array here is the string: {"Exception": [{"Code": -3, "name": "Decryption Error"}]}, the reason for passing the string instead of passing the array directly is that the message of the exception must be of type string
if ($exception instanceof userexception) { $array = Json_decode ($exception->getmessage (), true); } else { $array = [ ' name ' = = ($exception instanceof Exception | | $exception instanceof errorexception)? $ Exception->getname (): ' Exception ', ' message ' = = $exception->getmessage (), ' Code ' = ' $ ' Exception->getcode (), ]; }
Yii Modify exception return value to any format