protected function _actionException($context)
    {
        $exception = $context->data;

        //if AnException then conver it to AnException
        if ($exception instanceof AnException) {
            $exception = new RuntimeException($exception->getMessage(), $exception->getCode());
        }

        //if cli just print the error and exit
        if (PHP_SAPI == 'cli') {
            print "\n";
            print $exception."\n";
            print debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
            exit(0);
        }

        $code = $exception->getCode();

        //check if the error is code is valid
        if ($code < 400 || $code >= 600) {
            $code = AnHttpResponse::INTERNAL_SERVER_ERROR;
        }

        $context->response->status = $code;

        $config = array(
            'response' => $context->response,
            'request' => $context->request,
            'theme' => $this->_application->getTemplate()
        );

        //if ajax or the format is not html
        //then return the exception in json format
        if ($context->request->isAjax() || $context->request->getFormat() != 'html') {
            $context->request->setFormat('json');
        }

        $this->getService('com:application.controller.exception', $config)
        ->layout('error')
        ->render($exception);

        $this->send($context);
    }