Skip to content

Commit

Permalink
Merge pull request #5 from unic/feature/fix-null-ref-in-GetTargetUrl
Browse files Browse the repository at this point in the history
Prevent null-refs for missing referenced items
  • Loading branch information
schwindelig authored Apr 1, 2020
2 parents 4d691a5 + 1e35b98 commit 27c0851
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions be/src/Unic.UrlMapper2/code/Services/RedirectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,19 @@ protected virtual void PerformRedirect(Redirect redirect, HttpContextBase httpCo
}

var redirectItem = this.GetRedirectItem(redirect);
if (redirectItem == null)
if (redirectItem is null)
{
this.logger.Error($"Failed to perform redirect. Item with ID {redirect.ItemId} could not be found", this);
return;
}

var targetUrl = this.GetTargetUrl(redirectItem, additionalTargetData);
if (string.IsNullOrWhiteSpace(targetUrl))
{
this.logger.Error($"Failed to determine target url for redirect item {redirectItem.ID}", this);
return;
}

this.logger.Debug($"Performing {redirect.RedirectType} redirect to {targetUrl}", this);

switch (redirect.RedirectType)
Expand Down Expand Up @@ -152,7 +158,10 @@ protected virtual string GetTargetUrl(Item item, string additionalTargetData)
switch (linkField.LinkType)
{
case "internal":
targetUrl = this.linkManager.GetItemUrl(linkField.TargetItem, this.GetUrlOptions());
var internalTargetItem = linkField.TargetItem;
if (internalTargetItem is null) return default;

targetUrl = this.linkManager.GetItemUrl(internalTargetItem, this.GetUrlOptions());
break;
case "external":
case "mailto":
Expand All @@ -161,7 +170,10 @@ protected virtual string GetTargetUrl(Item item, string additionalTargetData)
targetUrl = linkField.Url;
break;
case "media":
var media = new MediaItem(linkField.TargetItem);
var mediaTargetItem = linkField.TargetItem;
if (mediaTargetItem is null) return default;

var media = new MediaItem(mediaTargetItem);
targetUrl = Sitecore.StringUtil.EnsurePrefix('/', this.mediaManager.GetMediaUrl(media));
break;
case "":
Expand Down

0 comments on commit 27c0851

Please sign in to comment.