From d552cc93443c4889ecbde0bd1937d34d0eb9dd23 Mon Sep 17 00:00:00 2001 From: "Martin Hinshelwood nkdAgility.com" Date: Mon, 27 Jan 2025 14:33:54 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20(TfsEmbededImagesTool.cs):=20imp?= =?UTF-8?q?rove=20error=20handling=20and=20logging=20for=20dummy=20work=20?= =?UTF-8?q?item=20creation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add exception handling to provide more informative error messages when exceptions occur. Enhance logging to warn about invalid fields in the dummy work item and ensure that the work item is ready to save. This change helps in diagnosing issues related to work item creation by providing detailed logs and throwing exceptions when the dummy work item cannot be created due to save failures. --- .../Tools/TfsEmbededImagesTool.cs | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/MigrationTools.Clients.TfsObjectModel/Tools/TfsEmbededImagesTool.cs b/src/MigrationTools.Clients.TfsObjectModel/Tools/TfsEmbededImagesTool.cs index 5413ea558..58310cdbd 100644 --- a/src/MigrationTools.Clients.TfsObjectModel/Tools/TfsEmbededImagesTool.cs +++ b/src/MigrationTools.Clients.TfsObjectModel/Tools/TfsEmbededImagesTool.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq.Expressions; using System.Net; using System.Net.Http; using System.Net.Http.Headers; @@ -158,6 +159,9 @@ private string UploadedAndRetrieveAttachmentLinkUrl(string matchedSourceUri, str return attachRef.Url; } + catch (Exception ex) { + throw ex; + } finally { if (File.Exists(fullImageFilePath)) @@ -239,9 +243,33 @@ private WorkItem GetDummyWorkItem(WorkItemType type = null) _targetDummyWorkItem = type.NewWorkItem(); _targetDummyWorkItem.Title = TargetDummyWorkItemTitle; + + var fails = _targetDummyWorkItem.Validate(); + if (fails.Count > 0) + { + Log.LogWarning("Dummy Work Item is not ready to save as it has some invalid fields. This may not result in an error. Enable LogLevel as 'Debug' in the config to see more."); + Log.LogDebug("--------------------------------------------------------------------------------------------------------------------"); + Log.LogDebug("--------------------------------------------------------------------------------------------------------------------"); + foreach (Field f in fails) + { + Log.LogDebug("Invalid Field Object:\r\n{Field}", f.ToJson()); + } + Log.LogDebug("--------------------------------------------------------------------------------------------------------------------"); + Log.LogDebug("--------------------------------------------------------------------------------------------------------------------"); + } + Log.LogTrace("TfsEmbededImagesTool::GetDummyWorkItem::Save()"); + + _targetDummyWorkItem.Save(); - Log.LogDebug("EmbededImagesRepairEnricher: Dummy workitem {id} created on the target collection.", _targetDummyWorkItem.Id); - //_targetProject.Store.DestroyWorkItems(new List { _targetDummyWorkItem.Id }); + + if (_targetDummyWorkItem.Id == 0) + { + throw new Exception("The Dummy work Item cant be created due to a save failure. This is likley due to required fields on the Task or First work items type."); + } else + { + Log.LogDebug("TfsEmbededImagesTool: Dummy workitem {id} created on the target collection.", _targetDummyWorkItem.Id); + //_targetProject.Store.DestroyWorkItems(new List { _targetDummyWorkItem.Id }); + } } _DummyWorkItemCount++; return _targetDummyWorkItem;