Skip to content

Commit

Permalink
Merge pull request #17423 from abpframework/ValidationAttributeHelper
Browse files Browse the repository at this point in the history
Catch and ignore the `Exception` from `ValidationAttributeHelper`.
  • Loading branch information
realLiangshiwei authored Aug 21, 2023
2 parents 453b1eb + 07ec945 commit ed6d9f4
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)
{
// ignored
}
}
}

0 comments on commit ed6d9f4

Please sign in to comment.