Skip to content

Commit

Permalink
Improve error message for SchemaValidationException
Browse files Browse the repository at this point in the history
  • Loading branch information
hazzik authored and Alex Zaytsev committed Jan 26, 2022
1 parent 375c032 commit d1ff749
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void SchemaExport_Validate_CausesValidateExceptionAsync()
Assert.That(
() => validator.ValidateAsync(),
Throws.TypeOf<SchemaValidationException>()
.And.Message.EqualTo("Schema validation failed: see list of validation errors")
.And.Message.StartsWith("Schema validation failed: see list of validation errors")
.And.Property("ValidationErrors").Contains("Missing table: Home_Validate"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task ShouldBeInvalidIfTimestampTzIsExpectedAndGotTimestampAsync()
var validator = new Tool.hbm2ddl.SchemaValidator(expected);

var error = Assert.ThrowsAsync<SchemaValidationException>(() => validator.ValidateAsync());
Assert.That(error, Has.Message.EqualTo("Schema validation failed: see list of validation errors"));
Assert.That(error, Has.Message.StartsWith("Schema validation failed: see list of validation errors"));
Assert.That(
error,
Has.Property("ValidationErrors").Some.Contains("Wrong column type").IgnoreCase
Expand Down Expand Up @@ -65,7 +65,7 @@ public async Task ShouldBeInvalidIfTimestampIsExpectedAndGotTimestampTzAsync()
var validator = new Tool.hbm2ddl.SchemaValidator(expected);

var error = Assert.ThrowsAsync<SchemaValidationException>(() => validator.ValidateAsync());
Assert.That(error, Has.Message.EqualTo("Schema validation failed: see list of validation errors"));
Assert.That(error, Has.Message.StartsWith("Schema validation failed: see list of validation errors"));
Assert.That(
error,
Has.Property("ValidationErrors").Some.Contains("Wrong column type").IgnoreCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void ShouldNotVerifyModifiedTableAsync()
Assert.That(
() => validatorV2.ValidateAsync(),
Throws.TypeOf<SchemaValidationException>()
.And.Message.EqualTo("Schema validation failed: see list of validation errors")
.And.Message.StartsWith("Schema validation failed: see list of validation errors")
.And.Property("ValidationErrors").Some.Contains("Missing column: Name in ").IgnoreCase.And.Contains("Version").IgnoreCase);
}

Expand All @@ -103,7 +103,7 @@ public void ShouldNotVerifyMultiModifiedTableAsync()

var error = Assert.ThrowsAsync<SchemaValidationException>(() => validator.ValidateAsync());
Assert.That(error,
Has.Message.EqualTo("Schema validation failed: see list of validation errors")
Has.Message.StartsWith("Schema validation failed: see list of validation errors")
.And.Property("ValidationErrors").Some.Contains("Missing column: Name in ").IgnoreCase.And.Contains("Version").IgnoreCase);
Assert.That(error,
Has.Property("ValidationErrors").Some.Contains("Missing column: Title in ").IgnoreCase.And.Contains("Version").IgnoreCase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void SchemaExport_Validate_CausesValidateException()
Assert.That(
() => validator.Validate(),
Throws.TypeOf<SchemaValidationException>()
.And.Message.EqualTo("Schema validation failed: see list of validation errors")
.And.Message.StartsWith("Schema validation failed: see list of validation errors")
.And.Property("ValidationErrors").Contains("Missing table: Home_Validate"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void ShouldBeInvalidIfTimestampTzIsExpectedAndGotTimestamp()
var validator = new Tool.hbm2ddl.SchemaValidator(expected);

var error = Assert.Throws<SchemaValidationException>(() => validator.Validate());
Assert.That(error, Has.Message.EqualTo("Schema validation failed: see list of validation errors"));
Assert.That(error, Has.Message.StartsWith("Schema validation failed: see list of validation errors"));
Assert.That(
error,
Has.Property("ValidationErrors").Some.Contains("Wrong column type").IgnoreCase
Expand Down Expand Up @@ -54,7 +54,7 @@ public void ShouldBeInvalidIfTimestampIsExpectedAndGotTimestampTz()
var validator = new Tool.hbm2ddl.SchemaValidator(expected);

var error = Assert.Throws<SchemaValidationException>(() => validator.Validate());
Assert.That(error, Has.Message.EqualTo("Schema validation failed: see list of validation errors"));
Assert.That(error, Has.Message.StartsWith("Schema validation failed: see list of validation errors"));
Assert.That(
error,
Has.Property("ValidationErrors").Some.Contains("Wrong column type").IgnoreCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void ShouldNotVerifyModifiedTable()
Assert.That(
() => validatorV2.Validate(),
Throws.TypeOf<SchemaValidationException>()
.And.Message.EqualTo("Schema validation failed: see list of validation errors")
.And.Message.StartsWith("Schema validation failed: see list of validation errors")
.And.Property("ValidationErrors").Some.Contains("Missing column: Name in ").IgnoreCase.And.Contains("Version").IgnoreCase);
}

Expand All @@ -92,7 +92,7 @@ public void ShouldNotVerifyMultiModifiedTable()

var error = Assert.Throws<SchemaValidationException>(() => validator.Validate());
Assert.That(error,
Has.Message.EqualTo("Schema validation failed: see list of validation errors")
Has.Message.StartsWith("Schema validation failed: see list of validation errors")
.And.Property("ValidationErrors").Some.Contains("Missing column: Name in ").IgnoreCase.And.Contains("Version").IgnoreCase);
Assert.That(error,
Has.Property("ValidationErrors").Some.Contains("Missing column: Title in ").IgnoreCase.And.Contains("Version").IgnoreCase);
Expand Down
9 changes: 7 additions & 2 deletions src/NHibernate/SchemaValidationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.Security;
using System.Text;

namespace NHibernate
{
Expand Down Expand Up @@ -36,8 +37,12 @@ public override string Message
var message = base.Message;
if (ValidationErrors == null || ValidationErrors.Count == 0)
return message;
var errors = "ValidationErrors:" + string.Join(Environment.NewLine + "- ", ValidationErrors);
return message + Environment.NewLine + errors;

var sb = new StringBuilder(message).AppendLine().AppendLine("Validation errors:");
foreach (var error in ValidationErrors)
sb.Append('-').AppendLine(error);

return sb.ToString();
}
}
}
Expand Down

0 comments on commit d1ff749

Please sign in to comment.