From 9c252131f7ca0a64ec066e39fa6afdbfb99651d7 Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 12 Jun 2019 08:55:03 +0200 Subject: [PATCH] Better exception message in NuCache --- .../PublishedCache/NuCache/ContentStore.cs | 56 ++++++++----------- .../NuCache/PublishedContent.cs | 2 +- 2 files changed, 23 insertions(+), 35 deletions(-) diff --git a/src/Umbraco.Web/PublishedCache/NuCache/ContentStore.cs b/src/Umbraco.Web/PublishedCache/NuCache/ContentStore.cs index f3c3c2bf7a..f1f8f137fb 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/ContentStore.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/ContentStore.cs @@ -642,13 +642,22 @@ namespace Umbraco.Web.PublishedCache.NuCache var id = content.FirstChildContentId; while (id > 0) { - if (!_contentNodes.TryGetValue(id, out var link) || link.Value == null) - throw new Exception("panic: failed to get child " + id); + var link = GetLinkedNode(id, "child"); ClearBranchLocked(link.Value); id = link.Value.NextSiblingContentId; } } + // gets the link node + // throws (panic) if not found, or no value + private LinkedNode GetLinkedNode(int id, string description) + { + if (_contentNodes.TryGetValue(id, out var link) && link.Value != null) + return link; + + throw new Exception($"panic: failed to get {description} with id={id}"); + } + private LinkedNode GetParentLink(ContentNode content) { _contentNodes.TryGetValue(content.ParentContentId, out var link); // else null @@ -659,16 +668,9 @@ namespace Umbraco.Web.PublishedCache.NuCache private void RemoveNodeLocked(ContentNode content) { - LinkedNode parentLink; - if (content.ParentContentId < 0) - { - parentLink = _root; - } - else - { - if (!_contentNodes.TryGetValue(content.ParentContentId, out parentLink) || parentLink.Value == null) - throw new Exception("panic: failed to get parent " + content.ParentContentId); - } + var parentLink = content.ParentContentId < 0 + ? _root + : GetLinkedNode(content.ParentContentId, "parent"); var parent = parentLink.Value; @@ -679,12 +681,10 @@ namespace Umbraco.Web.PublishedCache.NuCache } else { - if (!_contentNodes.TryGetValue(parent.FirstChildContentId, out var link) || link.Value == null) - throw new Exception("panic: failed to get first child " + parent.FirstChildContentId); + var link = GetLinkedNode(parent.FirstChildContentId, "first child"); while (link.Value.NextSiblingContentId != content.Id) - if (!_contentNodes.TryGetValue(parent.NextSiblingContentId, out link) || link.Value == null) - throw new Exception("panic: failed to get next sibling " + parent.NextSiblingContentId); + link = GetLinkedNode(parent.NextSiblingContentId, "next child"); var prevChild = GenCloneLocked(link); prevChild.NextSiblingContentId = content.NextSiblingContentId; @@ -726,17 +726,9 @@ namespace Umbraco.Web.PublishedCache.NuCache private void AddNodeLocked(ContentNode content) { - LinkedNode parentLink; - - if (content.ParentContentId < 0) - { - parentLink = _root; - } - else - { - if (!_contentNodes.TryGetValue(content.ParentContentId, out parentLink) || parentLink.Value == null) - throw new Exception("panic: failed to get parent " + content.ParentContentId); - } + var parentLink = content.ParentContentId < 0 + ? _root + : GetLinkedNode(content.ParentContentId, "parent"); var parent = parentLink.Value; @@ -747,9 +739,7 @@ namespace Umbraco.Web.PublishedCache.NuCache return; } - if (!_contentNodes.TryGetValue(parent.FirstChildContentId, out var prevChildLink) || prevChildLink.Value == null) - throw new Exception("panic: failed to get first child " + parent.FirstChildContentId); - + var prevChildLink = GetLinkedNode(parent.FirstChildContentId, "first child"); var prevChild = prevChildLink.Value; if (prevChild.SortOrder > content.SortOrder) @@ -763,8 +753,7 @@ namespace Umbraco.Web.PublishedCache.NuCache while (prevChild.NextSiblingContentId > 0) { - if (!_contentNodes.TryGetValue(prevChild.NextSiblingContentId, out var link) || link.Value == null) - throw new Exception("panic: failed to get next child " + prevChild.NextSiblingContentId); + var link = GetLinkedNode(prevChild.NextSiblingContentId, "next child"); var nextChild = link.Value; if (nextChild.SortOrder > content.SortOrder) @@ -872,8 +861,7 @@ namespace Umbraco.Web.PublishedCache.NuCache while (id > 0) { - if (!_contentNodes.TryGetValue(id, out var link) || link == null) - throw new Exception("panic: failed to get sibling " + id); + var link = GetLinkedNode(id, "sibling"); yield return link.Value; id = link.Value.NextSiblingContentId; } diff --git a/src/Umbraco.Web/PublishedCache/NuCache/PublishedContent.cs b/src/Umbraco.Web/PublishedCache/NuCache/PublishedContent.cs index 1995e8b424..20372c074d 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/PublishedContent.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/PublishedContent.cs @@ -278,7 +278,7 @@ namespace Umbraco.Web.PublishedCache.NuCache { var content = getById(publishedSnapshot, IsPreviewing, id); if (content == null) - throw new Exception("panic: failed to get content"); + throw new Exception($"panic: failed to get content with id={id}"); yield return content;