processwire / processwire-requests

ProcessWire feature requests.
40 stars 0 forks source link

Gracefully handling errors in ProcessWire Custom Page Classes #515

Open klor opened 10 months ago

klor commented 10 months ago

Short description of the enhancement

Enhance error handling mechanisms in ProcessWire's custom page classes to degrade gracefully and provide comprehensive, developer-friendly error messages.

Optional: Steps that explain the enhancement

  1. Integrate a sophisticated error detection system within the custom page class architecture that identifies and catches common PHP errors.
  2. Ensure the system degrades gracefully by providing detailed, contextual error messages that assist in debugging without exposing sensitive system details.
  3. Enhance the development experience by optionally allowing more verbose error reporting in development environments while ensuring minimal impact in production.

Current vs. suggested behavior

Currently, errors in custom page classes result in a stark Server Error 500 with no helpful output for debugging. The suggested behavior is for ProcessWire to gracefully degrade by catching these errors and providing a useful error message that includes specifics about the nature and location of the error, thereby aiding in a quick resolution.

Code example that currently results in error 500:

<?php namespace ProcessWire;
class FooPage extends Page {
    public function my_func() {
        $a = 1  // missing semi-colon causes error 500
    }
}

Why would the enhancement be useful to users?

Implementing a graceful degradation process for errors in custom page classes would make the development process with ProcessWire much more efficient and less frustrating. Developers would spend less time deciphering the cause of generic server errors and more time on productive development, improving both the developer experience and the stability of applications built with ProcessWire.

Toutouwai commented 10 months ago

PW doesn't need to reinvent the wheel and become a PHP debugger when we have an excellent debugging tool created by @adrianbj that's specifically tailored to the PW environment and provides all the things you're requesting and much more.

2024-01-07_100157

ryancramerdesign commented 10 months ago

@klor Any classes, whether custom Page classes, modules, or anything should will behave the same with regards to errors, as they all are handled by PW's error handler. If you want verbose errors you need to have debug mode enabled by setting $config->debug=true; in your /site/config.php file. When enabled, an error in a custom Page class (or anything else) will output the file name, line number, and error message. When you don't have debug mode enabled, PW intentionally hides any details about the error for security purposes, returning only a 500 response and instead logging the error. Like Toutouwai mentioned above, using TracyDebugger will provide even more detail, which is useful in a local development environment.