Yii Modify exception return value to any format

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.