Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try Catch for embeded media #9424

Merged
merged 7 commits into from
Jul 3, 2020
Merged

Try Catch for embeded media #9424

merged 7 commits into from
Jul 3, 2020

Conversation

rdigitalg
Copy link
Contributor

BUG Embedded media throws E_EMERGENCY error if url returns status codes other than 200.

If a media that is already embedded on the page from a url is removed from the server/url it throws an E_EMERGENCY error that breaks the whole page. Added a try / catch block to EmbedShortcodeProvider.php in function handle_shortcode() that will prevent this and just give a message based on the environment.
Dev environment will display the response message from the url.
Live environment will just display the text 'There was a problem loading the media.'

The text is wrapped in a div that has the class ss-media-exception so it can be styled by admin.

@Cheddam Cheddam linked an issue Mar 5, 2020 that may be closed by this pull request
6 tasks
Comment on lines 96 to 98
$attr = array(
'class' => 'ss-media-exception embed'
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
$attr = array(
'class' => 'ss-media-exception embed'
);
$attr = [
'class' => 'ss-media-exception embed'
];

$result = HTML::createTag(
'div',
$attr,
HTML::createTag('p', array(), 'There was a problem loading the media.')

Choose a reason for hiding this comment

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

If changing to the [] format from array() in @ScopeyNZ s suggestion, then probably should do the same here.

Suggested change
HTML::createTag('p', array(), 'There was a problem loading the media.')
HTML::createTag('p', [], 'There was a problem loading the media.')

Choose a reason for hiding this comment

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

Should the text be translateable?
https://docs.silverstripe.org/en/4/developer_guides/i18n/

Suggested change
HTML::createTag('p', array(), 'There was a problem loading the media.')
HTML::createTag('p', [], _t("Namespace.Entity", "There was a problem loading the media");

@ScopeyNZ - can you advise what the namespace & entity should be?

Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch. Normally the namespace is related to the class (and namespace) - the "entity" indicates the content of the message. Maybe something like EmbedShortcodeProvider.INVALID_URL?

Choose a reason for hiding this comment

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

EmbedShortcodeProvider.INVALID_URL works for me : )

Copy link
Member

Choose a reason for hiding this comment

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

It should be _t(__CLASS__ . '.INVALID_URL', 'There was a problem loading the media.') so we get the fully qualified class name, that’s the convention across core code 😄

$embed = $embed->getEmbed();
try {
$embed = $embed->getEmbed();
}

Choose a reason for hiding this comment

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

travis is failing because:
94 | ERROR | [x] Expected 1 space after closing brace; newline found

Choose a reason for hiding this comment

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

think the style is supposed to be:

Suggested change
}
try {
$embed = $embed->getEmbed();
} catch(\Embed\Exceptions\InvalidUrlException $e) {

https://www.php-fig.org/psr/psr-2/#56-try-catch

try {
$embed = $embed->getEmbed();
}
catch(\Embed\Exceptions\InvalidUrlException $e) {

Choose a reason for hiding this comment

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

95 | ERROR | [x] Expected 1 space(s) after CATCH keyword; 0 found

Suggested change
catch(\Embed\Exceptions\InvalidUrlException $e) {
catch (\Embed\Exceptions\InvalidUrlException $e) {

Copy link
Member

Choose a reason for hiding this comment

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

Could you change this to
use \Embed\Exceptions\InvalidUrlException; (top of the php file)
catch (InvalidUrlException $e) {

@asecondwill
Copy link

scrutinizer is flagging a decrease in quality, complaining that this can't be found:

SilverStripe\View\Shortcodes\Director

for php 7.1.

Am I reading that right? it has a tick, but an F sounds pretty bad. @rdigitalg have you tested against php 7.1 ?

@kinglozzer
Copy link
Member

@asecondwill I think it’s because it’s currently missing the namespace import

@rdigitalg
Copy link
Contributor Author

scrutinizer is flagging a decrease in quality, complaining that this can't be found:

SilverStripe\View\Shortcodes\Director

for php 7.1.

Am I reading that right? it has a tick, but an F sounds pretty bad. @rdigitalg have you tested against php 7.1 ?

only tested on php 7.0, ill run tests on other versions and report back

@asecondwill
Copy link

Thanks for changes @rdigitalg ,

@ScopeyNZ @kinglozzer @emteknetnz :

How is this looking now?

@asecondwill
Copy link

@kinglozzer - is this likely to get merged do you think?

Copy link
Member

@kinglozzer kinglozzer left a comment

Choose a reason for hiding this comment

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

I’m fine with merging this. I know that adding caching is mentioned in #9073, but no one has actually submitted a PR for that and this certainly doesn’t block that work

src/View/Shortcodes/EmbedShortcodeProvider.php Outdated Show resolved Hide resolved
@Cheddam
Copy link
Member

Cheddam commented Apr 20, 2020

Hey @asecondwill, sorry this is taking a while to get across the line! If you could squash your commits and give them a message matching our standard format - something like “FIX Handle failures in embedded media gracefully” - I’d be happy to merge this for you 🙂

@quamsta
Copy link
Contributor

quamsta commented Jun 17, 2020

Would be great if this could get merged in. Spent the morning fixing older blog posts containing broken GIFs that were bringing down a site's blog entirely.

@emteknetnz
Copy link
Member

Tested locally on a site using silverstripe-themes/simple:

SS_ENVIRONMENT_TYPE="dev"
image

SS_ENVIRONMENT_TYPE="live"
image

Youtube domain correct, invalid urlsegment (no preview, links to "video unavailable" page on youtube)
image

Valid embeds still function correctly on frontend. CMS still functions correctly.

Looks good to me

Thanks for your contribution

@emteknetnz emteknetnz merged commit 3bf89b2 into silverstripe:4 Jul 3, 2020
@brynwhyman
Copy link
Contributor

Hi, what are the chances of cherry picking this fix back to the 4.6 branch?

emteknetnz pushed a commit to creative-commoners/silverstripe-framework that referenced this pull request Jul 17, 2020
* Try Catch for embeded media
* added missing namespaces, translatable message INVALID_URL
* generate tag only once
* catch after closing bracket
* space after comma
* Update src/View/Shortcodes/EmbedShortcodeProvider.php
* Linting
Co-authored-by: [email protected] <[email protected]>
Co-authored-by: Steve Boyd <[email protected]>
Co-authored-by: Loz Calver <[email protected]>
@emteknetnz
Copy link
Member

emteknetnz commented Jul 17, 2020

@brynwhyman
Issue: #9601
PR: #9600

@asecondwill
Copy link

Thanks for merging this in, Ill test it this week on our site.

@quamsta
Copy link
Contributor

quamsta commented Jul 20, 2020

Same, thank you all!! <3

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

Successfully merging this pull request may close these issues.

Embedded content throws 500 during connection issues.
9 participants