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
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/View/Shortcodes/EmbedShortcodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,24 @@ public static function handle_shortcode($arguments, $content, $parser, $shortcod
}

// Process embed
$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

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) {

$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'
];

if (Director::isDev()) {
$result = HTML::createTag('div', $attr, $e->getMessage());
return $result;
}
$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 😄

);
emteknetnz marked this conversation as resolved.
Show resolved Hide resolved
return $result;
}

// Convert embed object into HTML
if ($embed && $embed instanceof Adapter) {
Expand Down