VOOZH about

URL: https://deepwiki.com/hypervel/http/6-exception-handling

⇱ Exception Handling | hypervel/http | DeepWiki


Loading...
Menu

Exception Handling

The hypervel/http package provides a structured exception hierarchy for handling HTTP-related errors and file upload failures. Exceptions are organized into two primary categories: HTTP protocol exceptions (HTTP status codes) and file upload exceptions (PHP upload error codes).

This page provides an overview of the exception architecture and how exceptions integrate with the request processing pipeline. For detailed documentation:

  • HTTP Exceptions: See page 6.1 for PostTooLargeException and other HTTP-related exceptions
  • File Upload Exceptions: See page 6.2 for file upload-specific exceptions like FileNotFoundException, IniSizeFileException, etc.

For information about middleware that throws HTTP exceptions, see page 4. For file upload processing, see page 2.4.

Exception Architecture

The package defines two primary exception categories: HTTP exceptions for protocol-level errors and file upload exceptions for file handling failures. These exceptions integrate with the request processing pipeline to provide clear error reporting and appropriate HTTP status codes.


Sources: src/Exceptions/PostTooLargeException.php1-19 src/Exceptions/InvalidOptionException.php1-11 src/UploadedFile.php14-22

HTTP Exceptions Overview

HTTP exceptions represent protocol-level errors with appropriate HTTP status codes. The package provides the following HTTP exception types:

Exception ClassHTTP StatusPurposeThrown By
PostTooLargeException413Request payload exceeds post_max_sizeValidatePostSize middleware
InvalidOptionExceptionN/A (RuntimeException)Invalid configuration or option valuesConfiguration validation

Key Characteristics:

  • PostTooLargeException extends HttpException (from Hypervel\HttpMessage\Exceptions)
  • InvalidOptionException extends PHP's standard RuntimeException
  • HTTP exceptions are thrown early in the middleware pipeline before application code executes

For detailed documentation of HTTP exceptions including constructor signatures, usage patterns, and integration with middleware, see page 6.1.

Sources: src/Exceptions/PostTooLargeException.php1-19 src/Middleware/ValidatePostSize.php1-52

File Upload Exceptions Overview

The file upload exception system maps PHP's upload error codes to specific exception types. All file upload exceptions extend the base FileException class.

PHP Upload Error Code Mapping:

PHP ConstantException ClassTriggered When
UPLOAD_ERR_INI_SIZEIniSizeFileExceptionFile exceeds upload_max_filesize
UPLOAD_ERR_FORM_SIZEFormSizeFileExceptionFile exceeds form's MAX_FILE_SIZE
UPLOAD_ERR_PARTIALPartialFileExceptionUpload was interrupted
UPLOAD_ERR_NO_FILENoFileExceptionNo file was uploaded
UPLOAD_ERR_CANT_WRITECannotWriteFileExceptionCannot write file to disk
UPLOAD_ERR_NO_TMP_DIRNoTmpDirFileExceptionMissing temporary directory
UPLOAD_ERR_EXTENSIONExtensionFileExceptionPHP extension stopped upload

Additional File Exceptions:

  • FileNotFoundException: File path does not exist
  • FileException: Generic file operation failures (MIME detection, content reading, move operations, etc.)

Key Features:

For detailed documentation of each exception type including error messages, throw locations, and usage patterns, see page 6.2.

Sources: src/UploadedFile.php14-22 src/UploadedFile.php32-40 src/UploadedFile.php385-402

Exception Integration in Request Pipeline

Exceptions are integrated at multiple points in the request processing pipeline, providing early validation and clear error reporting.

Title: Request Pipeline Exception Points


Pipeline Stages:

  1. Request Size Validation (src/Middleware/ValidatePostSize.php17-26): Checks CONTENT_LENGTH header before request body is parsed
  2. Route Matching (CoreMiddleware): Validates route existence and HTTP method
  3. File Upload Processing (src/UploadedFile.php50-73): Validates file existence during instantiation
  4. File Operations (src/UploadedFile.php364-403): Maps PHP upload errors to specific exceptions during move operations

Sources: src/Middleware/ValidatePostSize.php17-26 src/UploadedFile.php50-73 src/UploadedFile.php364-403

Exception Usage Patterns

Middleware Exception Handling

The ValidatePostSize middleware demonstrates early exception throwing in the request pipeline:


The middleware calculates post_max_size by parsing the PHP INI value and converting metric suffixes (K, M, G) to bytes src/Middleware/ValidatePostSize.php31-51

Sources: src/Middleware/ValidatePostSize.php17-26 src/Middleware/ValidatePostSize.php31-51

File Move Exception Resolution

The UploadedFile::move() method uses a switch statement to map error codes to exceptions src/UploadedFile.php385-400:

Error CodeSwitch Case LineException TypeAdditional Data
UPLOAD_ERR_INI_SIZE386IniSizeFileExceptionMaximum filesize in KiB
UPLOAD_ERR_FORM_SIZE388FormSizeFileException-
UPLOAD_ERR_PARTIAL390PartialFileException-
UPLOAD_ERR_NO_FILE392NoFileException-
UPLOAD_ERR_CANT_WRITE394CannotWriteFileException-
UPLOAD_ERR_NO_TMP_DIR396NoTmpDirFileException-
UPLOAD_ERR_EXTENSION398ExtensionFileException-
Other402FileExceptionGeneric error message

Each exception receives its message from getErrorMessage() src/UploadedFile.php493-501 which formats the error template with the client filename and calculated maximum size for UPLOAD_ERR_INI_SIZE.

Sources: src/UploadedFile.php385-402 src/UploadedFile.php493-501

Error Message Generation

The getErrorMessage() method generates contextual error messages:


The method compares post_max_size and upload_max_filesize to determine the effective limit src/UploadedFile.php410-416 using the minimum of the two values or PHP_INT_MAX if either is not set.

Sources: src/UploadedFile.php493-501 src/UploadedFile.php410-416

Testing Exception Handling

The File test class src/Testing/File.php1-116 extends UploadedFile with the $test flag set to true src/Testing/File.php39 This affects validation behavior in isValid() src/UploadedFile.php272-277 which checks the test flag to determine whether to use parent::isValid() (which performs is_uploaded_file() checks) or simply verify the error code equals UPLOAD_ERR_OK.

This allows test files to bypass PHP's upload verification while still exercising exception-throwing code paths.

Sources: src/Testing/File.php27-40 src/UploadedFile.php272-277