Skip to content

Commit

Permalink
Catch and ignore the Exception from ValidationAttributeHelper.
Browse files Browse the repository at this point in the history
  • Loading branch information
maliming committed Aug 21, 2023
1 parent 9bebe77 commit 07ec945
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ private void NormalizeMetadataDetail(DefaultMetadataDetails detail)
{
foreach (var validationAttribute in detail.ModelAttributes.Attributes.OfType<ValidationAttribute>())
{
NormalizeValidationAttrbute(validationAttribute);
NormalizeValidationAttribute(validationAttribute);
}
}

protected virtual void NormalizeValidationAttrbute(ValidationAttribute validationAttribute)
protected virtual void NormalizeValidationAttribute(ValidationAttribute validationAttribute)
{
if (validationAttribute.ErrorMessage == null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.ComponentModel.DataAnnotations;
using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace Volo.Abp.AspNetCore.Mvc.Validation;

public static class ValidationAttributeHelper
{
private static readonly PropertyInfo ValidationAttributeErrorMessageStringProperty = typeof(ValidationAttribute)
private readonly static PropertyInfo ValidationAttributeErrorMessageStringProperty = typeof(ValidationAttribute)
.GetProperty("ErrorMessageString", BindingFlags.Instance | BindingFlags.NonPublic)!;

private static readonly PropertyInfo ValidationAttributeCustomErrorMessageSetProperty = typeof(ValidationAttribute)
private readonly static PropertyInfo ValidationAttributeCustomErrorMessageSetProperty = typeof(ValidationAttribute)
.GetProperty("CustomErrorMessageSet", BindingFlags.Instance | BindingFlags.NonPublic)!;

public static void SetDefaultErrorMessage(ValidationAttribute validationAttribute)
Expand All @@ -24,7 +25,14 @@ public static void SetDefaultErrorMessage(ValidationAttribute validationAttribut
}
}

validationAttribute.ErrorMessage =
ValidationAttributeErrorMessageStringProperty.GetValue(validationAttribute) as string;
try
{
var errorMessageString = ValidationAttributeErrorMessageStringProperty.GetValue(validationAttribute) as string;
validationAttribute.ErrorMessage = errorMessageString;
}
catch (Exception e)
{

Check warning on line 34 in framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Validation/ValidationAttributeHelper.cs

View check run for this annotation

Codecov / codecov/patch

framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Validation/ValidationAttributeHelper.cs#L33-L34

Added lines #L33 - L34 were not covered by tests
// ignored
}

Check warning on line 36 in framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Validation/ValidationAttributeHelper.cs

View check run for this annotation

Codecov / codecov/patch

framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Validation/ValidationAttributeHelper.cs#L36

Added line #L36 was not covered by tests
}
}

0 comments on commit 07ec945

Please sign in to comment.