This exactly illustrates the danger of having "standard" exceptions.
Those 3-4 different exceptions are _not_ technically the same, because
they are produced by different libraries and in different areas of the
code. We cannot provide a generic response to every type of exception.
If anything it should be a base interface, not a base exception.
I also applied a pattern similar to that, but realized afterwards that
there were some issues with this approach.
Consider this scenario:
If there's a UserModel that throws a NotFound exception, then it makes a
lot of sense for a controller to catch that and emit a 404 when
retrieving information about a user.
Lets consider there's also a BlogPost model that relies on the UserModel
when creating new posts... This could be a:
PUT /posts/new-post
If a new post is created, referencing an author that doesn't exist in
the UserModel, and this exception is not caught, it would bubble to the
controller as a 404, when it should really have been a 409 Conflict.
Likewise, if I depend on a library that retrieves information for me,
it's possible that a 404 is completely unexpected, and the only
appropriate response is actually a 500.
A better design is that *every* exception is turned into a 500 Internal
Server Error, *unless* it's caught by the top-most controller, which
understands enough of the context to turn it into a different status code.
Evert
If the client can determine the uri of the new entity, it's reasonable
to use PUT instead. It has much better defined semantics than POST.
This is true, but that's also the purpose of an exception... It's an
exceptional case that somebody failed to handle. It's reasonable to
throw a NotFound exception, but if it was not caught in this context,
presenting it as a 404 to a user is not the appropriate result.
True. Emphasis on *should have*.
I should only catch exceptions if I can reasonably recover from this
exceptional situation, and opt into doing that. If you force a user to
catch every exception, you are mis-using exceptions as a means for
flow-control.
This is a common and often discussed anti-pattern.
There's some room for interpretation when picking appropriate HTTP
status codes, and it's not terribly interesting for this discussion. For
the sake of this discussion, you can replace it for whatever HTTP status
you think is appropriate.
Suffice it to say that I think you're incorrect ;)
On Thu, 2 Jul 2015 10:23:37 -0700 (PDT)
Mathieu Dumoulin <craz...@crazycoders.net> wrote:
> >
> > If anything it should be a base interface, not a base exception.
> >
>
> For the sake of being logic and respecting design principles to the
> core, yes, it should be an interface. But the problem would be that
> you'd have to reimplement every damn method for exceptions everytime.
Derailing slightly; why would that be?
Some weird cornercase where php cant catch exceptions by interface?
Because it seems to me that this would solve at least 70% of _my_
objections to it. The rest of my objections are that your proposed
exceptions are too specific.
A specific exceptions could implement more than one of these
interfaces; for instance a DirectoryNotReadableException might be both
a RunTimeException and a DirectoryException; or a
BadMethodCallException is a LogicException normally but a
RunTimeException when the method name is generated dynamically.
Converting a library would be as easy as going through all library
exceptions and slapping the appropriate interface on them.
--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/2cc39ae5-ba91-4ede-ab00-c86fc5ce0781%40googlegroups.com.
class WidgetNotFormedCorrectlyException extends Exception implements
NotFoundException, WidgetException, InvalidArgumentException,
CrellsWidgetLibraryException {}
On Jul 4, 2015, at 4:12 AM, Mathieu Dumoulin <craz...@crazycoders.net> wrote:
> The problem with this idea Tim is that although it matches more my original idea, it creates a gap between having a base of exceptions (or one/several basic generic enabled exceptions) and having a contract list.
I’m trying to understand the meaning of “a contract list”. It could be:
+ When writing a “throw” expression, one wants to browse the standardized list and pick a relevant class/interface (if there is a relevant one). This seems equally doable whether the standardized list takes the form of classes or interfaces.
+ When consuming a single function, one wants a good summary of its contract (e.g. “@throws” documentation which lists out interfaces that downstream is likely to consume). If the mental-model is focused on tags/interfaces rather than concrete implementations, then the current “@throws” idiom doesn't provide a proper listing of contract details. This should be more like:
- Current: @throws MyNodeAccessDeniedException
- Better: @throws MeInterface | AccessDeniedInterface
> I would love to see generics implemented into PHP. Back when i was doing a lot of .NET a few years ago, i loved the generic concept, it opens up lots of cool features about type flexibility and type hinting security. Considering how fast type based changes flow into PHP, i don't expect this to come to PHP before i die :P
Agree - PHP core isn’t going to change any time soon.
> Back to the problem at hand, maybe the PSR could suggest a link to a symfony command that generates exceptions for us based on the interface list we provide it. It would give us flexiblity to update our existing exceptions, do interfaced based catching, and create new exceptions with their tests (Maybe?) directly from the command line which would aleviate a lot of issues. Since exceptions usually follow mostly all the same pattern, the PSR could also define the best way to setup exceptions and generate the boilerplate for them?
FWIW, on second viewing, one can do an in-situ declaration with arbitrary combinations of tags without changing the core language — eg quick proof-of-concept:
https://gist.github.com/totten/65f254e4b81cc02a24d8#file-dyn-tag-exc-php-L40
Ultimately, I suppose these are two orthogonal problems with orthogonal solutions:
A. Problem: The taxonomy for exceptions/interfaces/traits/tags.
- Example Solutions: SPL Exceptions and your draft PSR.
- Why Standardize: Reduce learning costs for developers who work with multiple frameworks. Allow cross-cutting “catch” statements (where “try/catch” uses a common interface supported by multiple libraries).
B. Problem: The technique for implementing one-off concrete classes based on the various interfaces/traits.
- Example Solutions: An eval() wrapper or code-scanner (like in my POC). A CLI code-generator (eg `app/console generate:exception FooBundle AccessDeniedException`).
- Why Standardize: Reduce learning costs for developers who work with multiple frameworks. Avoid duplicate code-scanners in build process (ie one scanner can serve all packages/libraries/bundles).
The two problems could coupled or decoupled.
--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/618c0335-d596-4097-9a57-1e601b874636%40googlegroups.com.
I think a better solution would be that you attempt a PHP RFC. If it passes, then someone (not FIG) could create a shim package for those that are not on PHP7.In general though, I'm not really interested in this idea. Sorry.
--
Jeremy Lindblom (@jeremeamia)President of the Seattle PHP User Group (@seaphp)Organizer of the Pacific Northwest PHP conference (pnwphp.com)On Wed, Jul 8, 2015 at 10:28 AM, Mathieu Dumoulin <craz...@crazycoders.net> wrote:--Regarding what Antony Ferrara said, i think there is still room to create a PSR along side a PHP 7.1 RFC (or whatever the process may be) because not everyone will move to PHP7 and thus, this will either force developpers to not adopt the new exception base or wait a very long time to be adopted.I think it would be a good think to setup a PSR package that makes exceptions available under the \php namespace like Anthony proposed so that anyone not in PHP 7.1 will still benefit from these exceptions. Adding a version constraint for this package shouldn't be too hard IMO.What do you think?
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/618c0335-d596-4097-9a57-1e601b874636%40googlegroups.com.--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/CALDVupJxAfuLcO7YVzx9adQFCVP5xTtyUS9xah%3DoJZWSYE1E4A%40mail.gmail.com.