Skip to main content

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:
  1. Invalid API Types
  2. Invalid Environment Names
  3. Disabled API Types

Invalid API Types

Thrown when a requested API type is unknown or disabled in configuration.

Source Location

Class: OpenApiServices
Method: validateApiTypes()
Location: src/Services/OpenApiServices.php:147-161
Code:

When Thrown

Programmatic API:
Artisan Command:
Output:
HTTP Controller: Location: src/Controllers/OpenApiController.php:86-90
HTTP Request:
HTTP Response (422):

Available API Types

To avoid this exception, use only enabled API types from your configuration:
Valid API types: api, mobile
Invalid API types: admin (disabled), invalid (doesn’t exist)

Invalid Environment Names

Thrown when a requested environment is not defined in configuration.

Source Location

Class: EnvironmentGenerator
Method: getEnvironmentConfig()
Location: src/Services/EnvironmentGenerator.php:120-131
Code:

When Thrown

Programmatic API:
Artisan Command: The command validates and falls back to artisan with a warning:
Output:
Location: src/Commands/GenerateOpenApiSpec.php:96-101 HTTP Controller:
HTTP Response (404):
Location: src/Controllers/OpenApiController.php:216-221

Available Environments

Define environments in your configuration:
Valid environments: base, artisan, local, production
Invalid environments: staging, development, qa (unless you add them)

Generic Exceptions

Thrown for unexpected errors during generation. Exception Class: \Exception

Common Scenarios

  1. Invalid JSON templates
  2. Missing configuration keys
  3. File system errors
  4. Reflection errors (when inspecting controllers/models)

Source Locations

Artisan Command: Location: src/Commands/GenerateOpenApiSpec.php:161-170
HTTP Controller: Location: src/Controllers/OpenApiController.php:91-96

Example Scenarios

Missing Configuration:
File System Errors:
Reflection Errors: If a controller class cannot be inspected (rare):

Error Handling Best Practices

Programmatic Usage


Artisan Command Handling

The command already handles exceptions gracefully:
Output:
With Verbose Flag:
Output includes stack trace for debugging.

Validation Before Generation

Validate API Types:
Validate Environments:

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:
Solution: Enable the API type in configuration:

Problem: Environment Not Found

Error:
Solution: Add the environment to configuration:

Problem: File Write Error

Error:
Solution: Ensure directory exists and is writable:
Or specify a writable output path:

Public API

Programmatic generation API

Artisan Commands

CLI command reference

Configuration

Configure API types and environments

Testing

Test your implementation