Overview
The Laravel OpenAPI Generator raises standard PHP and Laravel exceptions for invalid input, missing configuration, and generation errors. Understanding these exceptions helps you implement robust error handling in your application.The package does not define custom exception classes. It uses PHP’s built-in
InvalidArgumentException and generic Exception for error conditions.Exception Types
InvalidArgumentException
Thrown when invalid input is provided to generation methods. Exception Class:\InvalidArgumentException
Common Scenarios:
- Invalid API Types
- Invalid Environment Names
- Disabled API Types
Invalid API Types
Thrown when a requested API type is unknown or disabled in configuration.Source Location
Class:OpenApiServicesMethod:
validateApiTypes()Location:
src/Services/OpenApiServices.php:147-161
Code:
When Thrown
Programmatic API:src/Controllers/OpenApiController.php:86-90
Available API Types
To avoid this exception, use only enabled API types from your configuration:api, mobileInvalid API types:
admin (disabled), invalid (doesn’t exist)
Invalid Environment Names
Thrown when a requested environment is not defined in configuration.Source Location
Class:EnvironmentGeneratorMethod:
getEnvironmentConfig()Location:
src/Services/EnvironmentGenerator.php:120-131
Code:
When Thrown
Programmatic API:artisan with a warning:
src/Commands/GenerateOpenApiSpec.php:96-101
HTTP Controller:
src/Controllers/OpenApiController.php:216-221
Available Environments
Define environments in your configuration:base, artisan, local, productionInvalid environments:
staging, development, qa (unless you add them)
Generic Exceptions
Thrown for unexpected errors during generation. Exception Class:\Exception
Common Scenarios
- Invalid JSON templates
- Missing configuration keys
- File system errors
- Reflection errors (when inspecting controllers/models)
Source Locations
Artisan Command: Location:src/Commands/GenerateOpenApiSpec.php:161-170
src/Controllers/OpenApiController.php:91-96
Example Scenarios
Missing Configuration:Error Handling Best Practices
Programmatic Usage
Artisan Command Handling
The command already handles exceptions gracefully:Validation Before Generation
Validate API Types:Testing Exception Handling
Feature Test Example
HTTP Status Codes
When using HTTP routes, exceptions map to appropriate status codes:
Location:
src/Controllers/OpenApiController.php
Debugging Exceptions
Enable Verbose Logging
Check Logs
Use Telescope
If Laravel Telescope is installed, view exceptions in the dashboard:Common Solutions
Problem: Unknown API Type
Error:Problem: Environment Not Found
Error:Problem: File Write Error
Error:Related Documentation
Public API
Programmatic generation API
Artisan Commands
CLI command reference
Configuration
Configure API types and environments
Testing
Test your implementation