Skip to content

PSR-18: Network / Request exception inheritance #155

Closed
@davidgrayston

Description

@davidgrayston
Q A
Bug? possibly
New Feature? no
Version 2.0.0

This could be the intended behaviour, so please close if so.

Actual Behavior

NetworkException extends RequestException

Expected Behavior

According to https://www.php-fig.org/psr/psr-18/meta/ I wouldn't expect NetworkException to extend RequestException

The domain exceptions NetworkExceptionInterface and RequestExceptionInterface define a contract very similar to each other. The chosen approach is to not let them extend each other because inheritance does not make sense in the domain model. A RequestExceptionInterface is simply not a NetworkExceptionInterface

Steps to Reproduce

try {
    throw new \Http\Client\Exception\NetworkException(
        'some network problem',
        $someRequest
    );
} catch (\Psr\Http\Client\RequestExceptionInterface $e) {
    // Exception is caught here. 
    echo 'caught request exception';
} catch (\Psr\Http\Client\NetworkExceptionInterface $e) {
    // Expect to catch here.
    echo 'caught network exception';
}

Possible Solutions

  • Change NetworkException so that it doesn't extend RequestException, then do one of the following:
    • Implement ::getRequest() in each (or use a trait)
    • Extend a class such as "RequestAwareException" that provides ::getRequest() method
  • Document the current structure (instruct to catch \Psr\Http\Client\NetworkExceptionInterface before \Psr\Http\Client\RequestExceptionInterface)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions