Home>

https://book.cakephp.org/3/en/development/errors.html#id7
I want to check if errors other than Warning can be detected with the error handler added based on the official document

The role of error handlers

Notify the system administrator when an exception other than 404 described in error.log occurs in the system.

The following error handler is created to notify you when there is a system error

I apologize for being mixed up and down, but I understand that the types of errors that can be detected are within this error level and are configured to be accepted by logs in the app.php file.
First of all, is this understanding correct?
https://book.cakephp.org/3/en/core-libraries/logging.html

  

Emergency: System is unusable
  Alert: Need to act now
  Critical: Critical condition
  Error: error condition
  Warning: Warning condition
  Notice: Normal but critical condition
  Info: Information message
  Debug: Debug level message

Applicable source code
<? php
namespace App \ Error;
use Cake \ Error \ BaseErrorHandler;
use Cake \ Core \ Configure;
use Cake \ Controller \ ComponentRegistry;
use App \ Controller \ Component \ MailComponent;
class AppError extends BaseErrorHandler
{
    public function _displayError ($error, $debug)
    {
        $level = $error ["error"];
        $systemError = array (
            'level' =>$error ["error"],
            'description' =>$error ["description"],
            'file' =>$error ["file"],
            'line' =>$error ["line"]
        );
        if ($level! = 'Warning'&&$level! = 'Notice'&&$level! = 'Debug') {
            $mail = new MailComponent (new ComponentRegistry ());
            $mail->sendSystemError ($systemError);
        }
    }
    public function _displayException ($exception)
    {
        $systemException = $exception->getMessage ();
        $mail = new MailComponent (new ComponentRegistry ());
        $mail->sendSystemException ($systemException);
    }
}

Attempting to generate an error by creating and accessing such a controller
If i make a request with an account that does not exist in the external API, Error.log will appear as Error, so I thought it would be possible to get the content that appears in error.log with an error handler

However, such errors cannot be detected by error handlers

I don't know how to intentionally generate an error other than Warning, and I can't verify that it can actually be detected. What should be the verification method in this case?

<? php
namespace App \ Controller \ Admin;
use App \ Controller \ AppController;
use Cake \ Core \ Configure;
use Cake \ Chronos \ Chronos;
use Cake \ I18n \ Time;
use Cake \ Core \ Exception \ Exception;
use Cake \ Http \ Exception \ InternalErrorException;
use Cake \ Datasource \ Exception \ RecordNotFoundException;
class DebugsController extends AppController {
...
  public function send_error () {
    \ Payjp \ Payjp :: setApiKey ("testtest");
    \ Payjp \ Charge :: create (array (
        "card" =>"tok_XXXXXX",
        "amount" =>3500,
        "currency" =>"jpy"
    ));
  }

}
...

Sorry for the hard-to-read document
Isn't it an implementation for the requirements I want to meet fundamentally? Or if you have any need for this information, thank you for pointing it out

  • Answer # 1

    If you want to generate an error in php, usetrigger_error.

    PHP: trigger_error-Manual

    trigger_error ('NOTICE error');// default is E_USER_NOTICE level
    trigger_error ('WARNING error', E_USER_WARNING);
    trigger_error ('ERROR error', E_USER_ERROR);
    Supplement

    Using components to send email is redundant. Use mailer classes.

    Creating reusable emails-Email-3.8

    Rather than overriding

    _displayError,_displayException, overridehandleError,handleExceptionI think it ’s smarter to read

    If you just want to be notified by email in case of an error, you can use the fusic/encount plugin.