How to make custom error handler for module?

Hello,

I need to make a custom error handler for an API module under the basic template, I tried the following:

<?php

namespace app\modules\api;

/**
 * api module definition class
 */
class Api extends \yii\base\Module {
	/**
	 * {@inheritdoc}
	 */
	public $controllerNamespace  = 'app\modules\api\controllers';
	public $enableCsrfValidation = false;

	/**
	 * {@inheritdoc}
	 */
	public function init () {
		parent::init();


		$ApiConfig = [
			'components' => [
				'errorHandler' => [
					//'class' => 'yii\web\ErrorHandler',
					'class'       => yii\web\ErrorHandler::class,
					'errorAction' => 'api/v1/default/error'
				],
				'request'      => [
					'class'                  => \yii\web\Request::class,
					'cookieValidationKey'    => 'xx2QZdKBHravCmHvTOnUzRvThAR8PbPV42',
					'parsers'                => [
						'application/json' => 'yii\web\JsonParser',
					],
					'enableCsrfValidation'   => false,
					'enableCookieValidation' => false,
				],
				'response'     => [
					'class'   => \yii\web\Response::class,
					//'class' => '\yii\web\Response',
					'format'  => \yii\web\Response::FORMAT_JSON,
					'charset' => 'UTF-8',
				],
			],
			'bootstrap'  => [
				[
					'class'   => 'yii\filters\ContentNegotiator',
					'formats' => [
						'application/json' => \yii\web\Response::FORMAT_JSON,
					],
					['log'],

				],
			],
		];
		if ( YII_ENV_DEV ) {
			$ApiConfig [ 'bootstrap' ] [] = 'debug';
			$ApiConfig [ 'modules' ] [ 'debug' ] = [
				'class' => 'yii\debug\Module'
				// uncomment the following to add your IP if you are not connecting from localhost.
				// 'allowedIPs' => ['127.0.0.1', '::1'],
			];

			$ApiConfig [ 'bootstrap' ] [] = 'gii';
			$ApiConfig [ 'modules' ] [ 'gii' ] = [
				'class' => 'yii\gii\Module'
				// uncomment the following to add your IP if you are not connecting from localhost.
				// 'allowedIPs' => ['127.0.0.1', '::1'],
			];
		}
		\Yii::configure(\Yii::$app, $ApiConfig);
		// initialize the module with the configuration loaded from config.php
		//\Yii::configure($this, require __DIR__ . '/config.php');

		$handler = new \yii\web\ErrorHandler(['errorAction' => 'api/v1/default/error']);
		\Yii::$app->set('errorHandler', $handler);
		$handler->register();
	}

	public function beforeAction ($action) {
		//$e = \Yii::$app->getErrorHandler();
		//\Yii::error(print_r($e), "test_app");
		//return parent::beforeAction($action); // TODO: Change the autogenerated stub
		if ( parent::beforeAction($action) ) {

		}
		return true;
	}


}

None of the above worked, so how to solve it?

Thanks,

1 Like

Errorhandler is an application thing and there can only be one (at least if you don’t want to get into really weird complex stuff with PHP error handling).

The errorhandler is registered in PHP to handle PHP errors and uncaught exceptions. This is done on application bootstrap, so a module can not replace the errorhandler.

I mean to make a method to handle the exception for a module. Sometime the exception showing unwanted details.

errors should be hidden in production env yii provides env constants for that if you still like to inspect the error stack you can do a try/catch

I think things like the yii\web\ErrorHandler should remain generic. When you say that you want a special handling of exceptions for a specific module then that behavior is not generic. So you could instead catch those exceptions in the module and provide your special handling there, i.e. do the work as part of your application code rather than as a customization of the framework.