Skip to content

Exception Handling

Maicon edited this page Sep 7, 2023 · 2 revisions

Exception Handling in PotatoService

In PotatoService, exceptions are not just error indicators but also powerful tools to communicate the nature of the problem to both developers and users. Properly categorized and handled exceptions can ensure a better user experience and easier debugging.

Categorizing Exceptions:

PotatoService primarily classifies exceptions into two categories:

  • BusinessException: Represents exceptions that arise from business rules or validations. These are usually not critical but indicate that some operation or input didn't meet the expected criteria.
    • HTTP Code: 400 (Bad Request)
  • ServerException: Represents critical errors, possibly arising from server issues, database problems, or other severe disruptions.
    • HTTP Code: 500 (Internal Server Error)

BusinessException Class:

The BusinessException class serves as a blueprint for business rule exceptions. The class offers a default error message and allows for both customization and internationalization. Error messages can be explicit strings or codes, which can be particularly useful for front-end applications that interpret and display user-friendly messages based on these codes. Furthermore, when an error message or code is provided, the system leverages the gettext translation system to ensure that the returned message is localized based on the user's language preference. If a specific message isn't provided, the class name is used as the error message for clarity.

Here's a glimpse of the BusinessException class:

class BusinessException extends \Exception {
    public $error_message = "BusinessException";

    public function __construct($message = "", $code = 400, Throwable $previous = null) {
        $message = empty($message) ? $this->getClassName() : $message;
        parent::__construct(_($message), $code, $previous);
    }

    private function getClassName() {
        $classMap = explode("\\", get_called_class());
        return end($classMap);
    }
}

Exception Handling Mechanism:

Whenever an exception is thrown, it's caught and processed by the handler_exception function. The handling varies based on the environment:

  • CLI: If the script is being run from the command line, the error code and message are printed directly to the terminal.

  • Web: For web requests, a structured JSON response is returned, providing detailed information about the exception, including the timestamp, exception class, HTTP status, request path, and the error message.

This handling ensures that irrespective of the context, meaningful error feedback is always provided.

Conclusion:

Exception handling in PotatoService is designed with both developers and users in mind. By categorizing exceptions and providing detailed feedback, it ensures smoother debugging and a better user experience. It's also extensible, allowing developers to define custom exceptions for more granular error feedback.