Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

convert out of range exception to http exception #60

Merged

Conversation

makasim
Copy link
Contributor

@makasim makasim commented Apr 26, 2013

I found that useful to convert OutOfRangeCurrentPageException to NotFoundHttpException.

For example a user can change the page number in the url, he will get 500 page before now it would 404 which is more descriptive.

@makasim
Copy link
Contributor Author

makasim commented May 14, 2013

do you plan to merge it? If so I close this PR and a change locally in a project. In case you ok to merge it let'me know what is left.

@makasim
Copy link
Contributor Author

makasim commented Jun 26, 2013

Are you gonna merge it? if not we find another way to solve this problem in the project code. You can just leave few words about PR.

@Baachi
Copy link

Baachi commented Jun 26, 2013

<3

@makasim
Copy link
Contributor Author

makasim commented Oct 15, 2013

any news?

@richsage
Copy link
Contributor

This would be a BC break, and it also does not allow for your own business logic to do other tasks based on this specific exception. Thanks for the contribution though :-)

@richsage richsage closed this Oct 15, 2013
@makasim
Copy link
Contributor Author

makasim commented Oct 16, 2013

@richsage

This would be a BC break

yeah I agree. We could avoid BC breaks. For example the listener could not be enabled(I enabled it in my project code) or I can make it configurable at bundle extension level. It will be disabled by default so any BC breaks.

and it also does not allow for your own business logic to do other tasks based on this specific exception.

that's not really true. Cuz it is easy to add a listener with a higher priority and do what ever you want.

I really think this logic should be inside the bundle. Without the listener it a bit broken. For example I copied the link http://example.com/users?products=10 and send it via email to my friend. He opens this link few days later. If amount of products has been changed during that time he endup with 500 error. No one like 500 and it is clear that 404 is better.

If it is not provided by the bundle I would have to copy\past that listener from one project to another. This is what I and my friends already do.

@richsage
Copy link
Contributor

I think if you're able to make the change configurable (and disabled by default as you suggested, I'd be more inclined to include it. Could you adjust accordingly?

@richsage richsage reopened this Oct 16, 2013
@makasim
Copy link
Contributor Author

makasim commented Oct 16, 2013

@richsage sure, I would do it next days

@richsage
Copy link
Contributor

Thanks :-)

@makasim
Copy link
Contributor Author

makasim commented Oct 17, 2013

@richsage updated.

@@ -42,6 +42,13 @@ Add the WhiteOctoberPagerfantaBundle to your application kernel:
);
}

That's would also be good to set these options to true:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a bit more explanation of what the configuration options actually do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@richsage done, please review

@Koc
Copy link

Koc commented Dec 9, 2013

@makasim what about option for doing redirects to the 1st/last page?

@makasim
Copy link
Contributor Author

makasim commented Dec 10, 2013

@Koc that's a good idea indeed but implementation could be tricky. The route could be defined by a developer and be very custom.

@Koc
Copy link

Koc commented Dec 10, 2013

We can use all current matched route and get parameters and replace only page number in one of them.

@makasim
Copy link
Contributor Author

makasim commented Dec 10, 2013

@Koc sounds good

@makasim
Copy link
Contributor Author

makasim commented Jan 2, 2014

@richsage done, please review

@pablodip
Copy link
Contributor

pablodip commented Jan 2, 2014

It looks nice, a couple of suggestions:

  • A more descriptive name would probably include exception and http, something like convert_not_valid_max_per_page_exception_to_http_not_found_exception. If it's too long, it could be in an index convert_exceptions, and inside max_per_page_to_http_not_found.
  • Having tests would be nice! ;)

@makasim
Copy link
Contributor Author

makasim commented Jan 2, 2014

Having tests would be nice! ;)

yes it is a good idea, though it could blow the PR since we have to setup test env and staff like that. in any case we would try to add some tests.

If it's too long, it could be in an index convert_exceptions, and inside max_per_page_to_http_not_found.

sounds as a good idea. let'd do this

@pablodip
Copy link
Contributor

pablodip commented Jan 2, 2014

There is already a kernel (https://github.com/whiteoctober/WhiteOctoberPagerfantaBundle/tree/master/TestsProject/app), but I think we would need to tests both cases (converting and not converting, so different configuration). This could be done maybe through environments?

@makasim
Copy link
Contributor Author

makasim commented Jan 2, 2014

This could be done maybe through environments?

@pablodip maybe we can pass (somehow) an config as array to AppKernel? this way a concrete test can adjust configuration to its needs.

@makasim
Copy link
Contributor Author

makasim commented Jan 2, 2014

@pablodip squashed&rebased against the latest master. Now we working on @pablodip your comments.

@@ -57,6 +57,14 @@
<tag name="twig.extension" />
<argument type="service" id="service_container" />
</service>

<service id="pagerfanta.convert_not_valid_max_per_page_to_not_found_listener" class="WhiteOctober\PagerfantaBundle\EventListener\ConvertNotValidMaxPerPageToNotFoundListener">
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding public=false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, listeners should be public since they lazy loaded,

starting from symfony 2.4 it throw exception with a warning that listener must be public

@makasim
Copy link
Contributor Author

makasim commented Jan 3, 2014

the plan is to introduce next concept(this one would allow to extend it with what @Koc proposed later):

white_october_pagerfanta:
  exceptional_cases:
    page_out_of_range: none|404
    page_invalid: none|404
    # ...

and listener:

<?php

class ConvertExceptionalCaseTo404Listner 
{
    public function setConvertPageOutOfRange($boolean);

    public function setConvertPageInvalid($boolean);

    // ...
}

and in the DI extnesion:

<?php

if ($config['white_october_pagerfanta']['exceptionnal_cases']['page_out_of_range'] == '404') {
    $listenerDefenition->addMethodCall('setConvertPageOutOfRange', array(true));
}

@pablodip
Copy link
Contributor

pablodip commented Jan 3, 2014

white_october_pagerfanta:
  convert_exceptions:
    out_of_range_page_to_http_not_found: true|false

invalid_page? What's that exception? I think we should explicitly indicate pagerfanta's exceptions.

@makasim
Copy link
Contributor Author

makasim commented Jan 3, 2014

invalid_page? What's that exception? I think we should explicitly indicate pagerfanta's exceptions.

@pablodip this is for cases when page is not a number.

@makasim
Copy link
Contributor Author

makasim commented Jan 3, 2014

@pablodip there are lots of exceptions which start from not. I dont like having config options which starts from not. Though that's ok for exceptions.

@pablodip
Copy link
Contributor

pablodip commented Jan 3, 2014

I would personally specify the exceptions:

white_october_pagerfanta:
  convert_exceptions:
    out_of_range_page_to_http_not_found: true|false
    not_valid_current_page_to_http_not_found: true|false
    ...

By the way, why is it difficult to implement the redirection? It would be something similar to what we already do when paginating, no?

white_october_pagerfanta:
  exceptions_strategy:
    out_of_range_page: false|to_http_not_found|redirect_to_page_1
    not_valid_current_page_to_http_not_found: false|to_http_not_found|redirect_to_page_1
    ...

Something like this seems nice, but it's up to you as you're implementing it! ;)

@makasim
Copy link
Contributor Author

makasim commented Jan 3, 2014

@pablodip done, please review

@Skadabr
Copy link
Contributor

Skadabr commented Jan 3, 2014

testOutOfRangeException() and testWrongMaxPerPageException()
print message in stderr
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php#L99
I have no idea how it catch in tests.

@makasim
Copy link
Contributor Author

makasim commented Jan 3, 2014

I have no idea how it catch in tests.

it could be solved by injecting a logger service. for that we would need a monolog bundle setuped and configured. I dont think its a good idea "just to hide a message".

->arrayNode('exceptions_strategy')
->addDefaultsIfNotSet()
->children()
->scalarNode('out_of_range_page')->defaultValue(self::EXCEPTION_STRATEGY_NONE)->end()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default to false or none?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, sorry, my fault, I didn't see that! ;)

@pablodip
Copy link
Contributor

pablodip commented Jan 3, 2014

It seems ok for me. I have the suggestions I've told you, but they're suggestions for names or how to get the exceptions when testing. I's also make unit tests for the events. Anyway I'm willing to merge. So just let me know if you wanna do some of the suggestions or prefer to merge like this ;)

@makasim
Copy link
Contributor Author

makasim commented Jan 3, 2014

@pablodip yeap, we address your suggestions tomorrow. let's wait till than, thanks for the review

@pablodip
Copy link
Contributor

pablodip commented Jan 3, 2014

Nice, thanks! ;)

@makasim
Copy link
Contributor Author

makasim commented Jan 4, 2014

@pablodip done for tests names but we decided to keep action as is. It is a functional test so it would be good to test all things at once.

pablodip added a commit that referenced this pull request Jan 4, 2014
…p-not-found

convert out of range exception to http exception
@pablodip pablodip merged commit 9446f5b into sampart:master Jan 4, 2014
@pablodip
Copy link
Contributor

pablodip commented Jan 4, 2014

Thanks! ;)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants