Skip to content

Commit

Permalink
ODS-5070 - Handle TargetInvocationException so correct validation res…
Browse files Browse the repository at this point in the history
…ults gets returned from api (#394)
  • Loading branch information
bptillman authored and vimayya committed Jul 7, 2024
1 parent cfb1c9a commit aa99ee3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
14 changes: 11 additions & 3 deletions Application/EdFi.Ods.Common/Extensions/ValidatorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Reflection;
using log4net;

namespace EdFi.Ods.Common.Extensions
{
public static class ObjectValidatorExtensions
public static class ValidatorExtensions
{
private static readonly ILog _logger = LogManager.GetLogger(typeof(ValidatorExtensions));

public static ICollection<ValidationResult> ValidateObject(this IEnumerable<IObjectValidator> validators, object @object)
{
var result = new List<ValidationResult>();
Expand All @@ -24,9 +27,14 @@ public static ICollection<ValidationResult> ValidateObject(this IEnumerable<IObj
{
result.AddRange(validator.ValidateObject(@object));
}
catch (TargetInvocationException ex)
{
_logger.Error($"Validation exception [{ex.GetType()}]: {ex.StackTrace}",ex);
result.Add(new ValidationResult(ex.InnerException.Message));
}
catch (Exception ex)
{
Trace.TraceError("Validation exception [{0}]: {1}", ex.GetType(), ex.StackTrace);
_logger.Error($"Validation exception [{ex.GetType()}]: {ex.StackTrace}", ex);
result.Add(new ValidationResult(ex.Message));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-License-Identifier: Apache-2.0
// Licensed to the Ed-Fi Alliance under one or more agreements.
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
// See the LICENSE and NOTICES files in the project root for more information.

using System;
using System.Linq;
using EdFi.Ods.Api.Validation;
using EdFi.Ods.Common;
using EdFi.Ods.Common.Attributes;
using EdFi.Ods.Common.Extensions;
using EdFi.Ods.Common.Validation;
using NUnit.Framework;
using Shouldly;

namespace EdFi.Ods.Tests.EdFi.Ods.Common.Extensions
{
public class ValidationExtensionsTests
{
[TestFixture]
public class When_class_with_required_non_default_throws_exception_during_validation
{
[Test]
public void Should_still_capture_validation_message()
{
var testClass = new ClassThatWillThrowValidationException { Value = "NewValueHere" };

var validators = new IEntityValidator[] { new DataAnnotationsEntityValidator() };
var results = validators.ValidateObject(testClass);

results.IsValid().ShouldBe(false);
results.Count.ShouldBe(1);
results.First().ErrorMessage.ShouldBe("Test validation failure message.");
}
}
}

class ClassThatWillThrowValidationException
{
private string _value;

[RequiredWithNonDefault]
public string Value
{
get => throw new Exception("Test validation failure message.");
set => _value = value;
}
}
}

0 comments on commit aa99ee3

Please sign in to comment.