openai-php / client

⚡️ OpenAI PHP is a supercharged community-maintained PHP API client that allows you to interact with OpenAI API.
MIT License
4.57k stars 466 forks source link

Add @throws annotation #287

Open VincentLanglet opened 7 months ago

VincentLanglet commented 7 months ago

What:

Description:

HI @gehrisandro @nunomaduro, I recently used this library and got an API exception in production. Currently there is no @throws phpdoc above method to help the developer to be aware exception are thrown and which can be caught. @throws annotation allow tools like PHPStorm or PHPStan to report not caught exception. I tried to improve the documentation then.

Thanks.

Related:

pkly commented 5 months ago

I just added this library to our project and noticed exception throws were missing on higher level interfaces but were added on request interfaces which were used.

This would be a nice addition, but I would instead like to request specific exceptions that are thrown by default implementations to be specified on the interfaces instead of the interface. Though obviously the interface to be shared across the exceptions is a nice idea.

VincentLanglet commented 5 months ago

but I would instead like to request specific exceptions that are thrown by default implementations to be specified on the interfaces instead of the interface.

The interface should be about contract and not about "default implementations". This allow different implementations.

Rather than saying exactly the exception thrown in the default implementation (which is difficult because it can change often) it's generally better to throw an Interface, saying "All theses exceptions could be thrown or not" and if someone else implements the interface he can decide which exceptions from the interface he throws.

VincentLanglet commented 5 months ago

@gehrisandro @nunomaduro I rebased the PR if you have time to take a look :)

Thanks a lot.

pkly commented 5 months ago

In that case all of the exceptions should be behind interfaces, though that is rather... annoying.

VincentLanglet commented 5 months ago

In that case all of the exceptions should be behind interfaces, though that is rather... annoying.

You can catch Interfaces, that the same. You can also write

catch (ExceptionA) {
  // dothings
} catch (ExceptionInterface) {
  // dothings
}

And that way more useful, than a PHPdoc ExceptionA|ExceptionB|ExceptionC|....

Most of the biggest PHP works with interfaces.

pkly commented 5 months ago

My point being biggest frameworks use separate interfaces for specific error types, not a generic one.

hn-0 commented 1 month ago

Nice PR,, hope it gets approved soon.

VincentLanglet commented 3 weeks ago

Hi @gehrisandro, would it be possible to have some feedback on this PR ?

The @throws annotation would be helpful for static analysis. This PR is mainly phpdoc, and it's validated by the PHPStan analysis with this config https://github.com/openai-php/client/pull/287/files#diff-6f19df6a6307a48db0940e6897591fb08776d2db8a6134737aa708defdb5c92dR8.

Thanks a lot.