New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yii2 and Exceptions #3431

Closed
MUTOgen opened this Issue May 11, 2014 · 8 comments

Comments

Projects
None yet
2 participants
@MUTOgen
Contributor

MUTOgen commented May 11, 2014

I'm goind to build REST API for my app, based on Yii2. So, i have a problem with exceptions. For example, i need to throw 405 HTTP code when someone use wrong HTTP verb, but i wanna send back something like this:

{
"meta":
    {
        "error":
            {
                "code":405,
                "message":"Wrong method"
            }
     }, ............
}

So, i need to catch HttpException, recognize type of error and modify Response object. But how can i do this? In Yii there were onError and onException events. What about Yii2?

@cebe

This comment has been minimized.

Member

cebe commented May 11, 2014

you can extend the errorhandler or create your own ErrorAction class.

@cebe cebe closed this May 11, 2014

@MUTOgen

This comment has been minimized.

Contributor

MUTOgen commented May 11, 2014

I did this, but when i try to change errorHandler component in my API module i get nothing.

\Yii::$app->set('errorHandler',array(
                'class'=>'app\components\ApiErrorHandler',
                'errorAction'=>'site/error',
            ));

\yii\web\ErrorHandler still working. I need to re-init component somehow....

@cebe

This comment has been minimized.

Member

cebe commented May 11, 2014

you need to configure it in application config.

@MUTOgen

This comment has been minimized.

Contributor

MUTOgen commented May 11, 2014

@cebe I tried this but i get

ReflectionException: Class app\components\ApiErrorHandler does not exist in  ...\yiisoft\yii2\di\Container.php on line 408

with config:

'errorHandler' => [
            'class'=>'app\components\ApiErrorHandler',
            'errorAction' => 'site/error',
        ],

I think @app alias doesn't set yet.

@cebe cebe reopened this May 11, 2014

@cebe cebe added the type:bug label May 11, 2014

@cebe cebe added this to the 2.0 RC milestone May 11, 2014

@cebe cebe closed this in 8134af0 May 11, 2014

@cebe

This comment has been minimized.

Member

cebe commented May 11, 2014

checked in a fix. you should now be able to use your errorhandler in application config.

@MUTOgen

This comment has been minimized.

Contributor

MUTOgen commented May 11, 2014

nope.. adding

'class'=>'app\components\ApiErrorHandler',

turns my app to 500 error

@cebe

This comment has been minimized.

Member

cebe commented May 11, 2014

without stacktrace and error message I can not help you...

@MUTOgen

This comment has been minimized.

Contributor

MUTOgen commented May 11, 2014

now it's ok
i fixed my custom ErrorHandler
configuring by $config is ok, but it will be better to change handler on fly... #3436

Alex-Code added a commit to Alex-Code/yii2 that referenced this issue May 12, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment