Prevent default error website when getting error from a DataBase transaction

Permalink
Hello!

I want to know how to prevent the default error website to be shown and instead just get the code in order to return it with an `echo json_encode()`

Thank you

titanve
 
mlocati replied on at Permalink Reply
mlocati
Everything is explained here:http://documentation.concrete5.org/tutorials/override-almost-any-co...

BTW, here's what you can do:


1) Edit the file application/config/app.php:

<?php
return array(
    'providers' => array(
        'core_whoops' => '\Application\Src\Custom\CustomWhoopsServiceProvider',
    )
);


2) Create a new file at application/src/Custom/CustomWhoopsServiceProvider.php:

<?php
namespace Application\Src\Custom;
use Concrete\Core\Error\Handler\ErrorHandler;
use Concrete\Core\Error\Handler\JsonErrorHandler;
use Concrete\Core\Error\Run\PHP7CompatibleRun;
use Concrete\Core\Foundation\Service\Provider;
use Whoops\Handler\PlainTextHandler;
use Whoops\Run;
class CustomWhoopsServiceProvider extends Provider
{
    /**
     * {@inheritdoc}
     *
     * @see Provider::register()
     */


3) Create a new file at application/src/Custom/DatabaseExceptionHandler.php:

<?php
namespace Application\Src\Custom;
use Whoops\Handler\Handler;
class DatabaseExceptionHandler extends Handler
{
    public function handle()
    {
        $e = $this->getException();
        if (!($e instanceof \Doctrine\DBAL\DBALException)) {
            return Handler::DONE;
        }
        echo json_encode(['error' => $e->getMessage()]);
        return Handler::QUIT;
    }
}
titanve replied on at Permalink Reply
titanve
Dear Michele,

Thank you for your help. I will try what you suggest

Have a nice day

On Thu, Aug 18, 2016, 04:55 concrete5 Community <discussions@concretecms.com>
wrote: