I’m using Aspose.Tasks 24.4 on .NET. When creating a project through code and running recalculate I get a TaskValidationException with the name of task and error. The problem is that I need to get the task that resulted to the error, but ChildTaskCollector does not return any task with warning set to true. It seems that after validation the task that returned the error is not set as Warning: true. For this reason I cannot get the task and it’s info, which I need to be able to resolve this issue. Also is there a way to know if there were multiple errors? As it seems to throw the first error that comes out. For example, if there are two tasks that scheduled start date is later than scheduled finish date I would love to be able to get both of the tasks through checking if the Warning was set to true. Or maybe there would be a way to have an WarningException or something to inform what’s wrong per task?
When you encounter a TaskValidationException
during project recalculation in Aspose.Tasks, it can be challenging to identify which tasks are causing the errors, especially if the ChildTaskCollector
does not return tasks marked with warnings. Here are some strategies to help you retrieve the tasks that resulted in validation errors:
-
Handling Exceptions: When you catch the
TaskValidationException
, you can inspect the exception message to get details about the error. This message typically includes the name of the task and the specific validation issue. -
Iterating Through Tasks: After catching the exception, you can iterate through all tasks in the project to check their properties. For example, you can verify the start and finish dates of each task to identify any discrepancies that may have led to the validation error.
-
Multiple Errors: Unfortunately, the current implementation may only throw the first encountered validation error. To handle multiple errors, you might need to implement a custom validation routine that checks each task’s properties before calling the
Recalculate
method. This way, you can collect all tasks that have issues and report them accordingly.
Here is a sample code snippet that demonstrates how to catch the exception and iterate through tasks:
try
{
var project = new Project();
var task = project.RootTask.Children.Add("Task");
// Set incorrect dates to trigger validation error
task.Set(Tsk.Start, new DateTime(2023, 6, 19));
task.Set(Tsk.Finish, new DateTime(2023, 6, 18));
// Run project recalculation with validation
project.Recalculate(true);
}
catch (TaskValidationException ex)
{
Console.WriteLine($"Validation Error: {ex.Message}");
// Iterate through tasks to find issues
foreach (var t in project.RootTask.Children)
{
if (t.Get(Tsk.Start) > t.Get(Tsk.Finish))
{
Console.WriteLine($"Task '{t.Get(Tsk.Name)}' has invalid dates.");
}
}
}
This approach allows you to identify tasks with validation issues and handle them accordingly. If you need to track multiple errors, consider implementing a more comprehensive validation check before recalculation.
If you have further questions or need additional assistance, feel free to ask!
Shouldn’t the Task.Warning be set to true if an error occurs during recalculation? What does validation exactly do then if it does not set the Warning field to true then what is the purpose of it? I need a way to get the exact Task that is failing and are a lot of fields to check so creating a custom logic is not convenient in this case.
@KornelijusS ,
Aspose.Tasks’s API is based on MS Project’s model and Warning is a counterpart of MS Project’s Warning task field.
The value of the field is read when project is read from MPP format.
It would be incorrect to modify Warning property because it could overwrite MS Project’s values.
Also TaskValidationException designates another (more serious) type of problem. It is thrown when inconsistency in task dates is detected. For example, when Start is later than Finish,
Scheduled Start if later than Scheduled Finish, etc.
We can add TaskUid to TaskValidationException as a solution.
That would be great if you could add that. As any way to identify the task that is throwing the error would be great. Also thank you for the reply.
@KornelijusS
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): TASKSNET-11278
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.