[Fixes #1919] Added path info to RazorPage exceptions

This commit is contained in:
Ajay Bhargav Baaskaran 2015-04-15 15:11:11 -07:00
parent 21031a3aa8
commit f878ca5b15
7 changed files with 75 additions and 49 deletions

View File

@ -91,7 +91,7 @@ namespace Microsoft.AspNet.Mvc.Razor
} }
/// <summary> /// <summary>
/// A layout page cannot be rendered after '{0}' has been invoked. /// Layout page '{0}' cannot be rendered after '{1}' has been invoked.
/// </summary> /// </summary>
internal static string LayoutCannotBeRendered internal static string LayoutCannotBeRendered
{ {
@ -99,11 +99,11 @@ namespace Microsoft.AspNet.Mvc.Razor
} }
/// <summary> /// <summary>
/// A layout page cannot be rendered after '{0}' has been invoked. /// Layout page '{0}' cannot be rendered after '{1}' has been invoked.
/// </summary> /// </summary>
internal static string FormatLayoutCannotBeRendered(object p0) internal static string FormatLayoutCannotBeRendered(object p0, object p1)
{ {
return string.Format(CultureInfo.CurrentCulture, GetString("LayoutCannotBeRendered"), p0); return string.Format(CultureInfo.CurrentCulture, GetString("LayoutCannotBeRendered"), p0, p1);
} }
/// <summary> /// <summary>
@ -171,19 +171,19 @@ namespace Microsoft.AspNet.Mvc.Razor
} }
/// <summary> /// <summary>
/// You cannot flush while inside a writing scope. /// The {0} operation cannot be performed while inside a writing scope in '{1}'.
/// </summary> /// </summary>
internal static string RazorPage_YouCannotFlushWhileInAWritingScope internal static string RazorPage_CannotFlushWhileInAWritingScope
{ {
get { return GetString("RazorPage_YouCannotFlushWhileInAWritingScope"); } get { return GetString("RazorPage_CannotFlushWhileInAWritingScope"); }
} }
/// <summary> /// <summary>
/// You cannot flush while inside a writing scope. /// The {0} operation cannot be performed while inside a writing scope in '{1}'.
/// </summary> /// </summary>
internal static string FormatRazorPage_YouCannotFlushWhileInAWritingScope() internal static string FormatRazorPage_CannotFlushWhileInAWritingScope(object p0, object p1)
{ {
return GetString("RazorPage_YouCannotFlushWhileInAWritingScope"); return string.Format(CultureInfo.CurrentCulture, GetString("RazorPage_CannotFlushWhileInAWritingScope"), p0, p1);
} }
/// <summary> /// <summary>
@ -203,7 +203,7 @@ namespace Microsoft.AspNet.Mvc.Razor
} }
/// <summary> /// <summary>
/// {0} can only be called from a layout page. /// {0} invocation in '{1}' is invalid. {0} can only be called from a layout page.
/// </summary> /// </summary>
internal static string RazorPage_MethodCannotBeCalled internal static string RazorPage_MethodCannotBeCalled
{ {
@ -211,11 +211,11 @@ namespace Microsoft.AspNet.Mvc.Razor
} }
/// <summary> /// <summary>
/// {0} can only be called from a layout page. /// {0} invocation in '{1}' is invalid. {0} can only be called from a layout page.
/// </summary> /// </summary>
internal static string FormatRazorPage_MethodCannotBeCalled(object p0) internal static string FormatRazorPage_MethodCannotBeCalled(object p0, object p1)
{ {
return string.Format(CultureInfo.CurrentCulture, GetString("RazorPage_MethodCannotBeCalled"), p0); return string.Format(CultureInfo.CurrentCulture, GetString("RazorPage_MethodCannotBeCalled"), p0, p1);
} }
/// <summary> /// <summary>
@ -251,7 +251,7 @@ namespace Microsoft.AspNet.Mvc.Razor
} }
/// <summary> /// <summary>
/// The section named '{0}' has already been rendered. /// {0} invocation in '{1}' is invalid. The section '{2}' has already been rendered.
/// </summary> /// </summary>
internal static string SectionAlreadyRendered internal static string SectionAlreadyRendered
{ {
@ -259,15 +259,15 @@ namespace Microsoft.AspNet.Mvc.Razor
} }
/// <summary> /// <summary>
/// The section named '{0}' has already been rendered. /// {0} invocation in '{1}' is invalid. The section '{2}' has already been rendered.
/// </summary> /// </summary>
internal static string FormatSectionAlreadyRendered(object p0) internal static string FormatSectionAlreadyRendered(object p0, object p1, object p2)
{ {
return string.Format(CultureInfo.CurrentCulture, GetString("SectionAlreadyRendered"), p0); return string.Format(CultureInfo.CurrentCulture, GetString("SectionAlreadyRendered"), p0, p1, p2);
} }
/// <summary> /// <summary>
/// Section '{0}' is not defined. /// Section '{0}' is not defined in path '{1}'.
/// </summary> /// </summary>
internal static string SectionNotDefined internal static string SectionNotDefined
{ {
@ -275,11 +275,11 @@ namespace Microsoft.AspNet.Mvc.Razor
} }
/// <summary> /// <summary>
/// Section '{0}' is not defined. /// Section '{0}' is not defined in path '{1}'.
/// </summary> /// </summary>
internal static string FormatSectionNotDefined(object p0) internal static string FormatSectionNotDefined(object p0, object p1)
{ {
return string.Format(CultureInfo.CurrentCulture, GetString("SectionNotDefined"), p0); return string.Format(CultureInfo.CurrentCulture, GetString("SectionNotDefined"), p0, p1);
} }
/// <summary> /// <summary>

View File

@ -608,7 +608,7 @@ namespace Microsoft.AspNet.Mvc.Razor
{ {
if (RenderBodyDelegate == null) if (RenderBodyDelegate == null)
{ {
var message = Resources.FormatRazorPage_MethodCannotBeCalled(nameof(RenderBody)); var message = Resources.FormatRazorPage_MethodCannotBeCalled(nameof(RenderBody), Path);
throw new InvalidOperationException(message); throw new InvalidOperationException(message);
} }
@ -704,7 +704,7 @@ namespace Microsoft.AspNet.Mvc.Razor
{ {
if (_renderedSections.Contains(sectionName)) if (_renderedSections.Contains(sectionName))
{ {
var message = Resources.FormatSectionAlreadyRendered(sectionName); var message = Resources.FormatSectionAlreadyRendered(nameof(RenderSectionAsync), Path, sectionName);
throw new InvalidOperationException(message); throw new InvalidOperationException(message);
} }
@ -721,7 +721,7 @@ namespace Microsoft.AspNet.Mvc.Razor
else if (required) else if (required)
{ {
// If the section is not found, and it is not optional, throw an error. // If the section is not found, and it is not optional, throw an error.
throw new InvalidOperationException(Resources.FormatSectionNotDefined(sectionName)); throw new InvalidOperationException(Resources.FormatSectionNotDefined(sectionName, Path));
} }
else else
{ {
@ -748,14 +748,15 @@ namespace Microsoft.AspNet.Mvc.Razor
// change. // change.
if (_writerScopes.Count > 0) if (_writerScopes.Count > 0)
{ {
throw new InvalidOperationException(Resources.RazorPage_YouCannotFlushWhileInAWritingScope); throw new InvalidOperationException(
Resources.FormatRazorPage_CannotFlushWhileInAWritingScope(nameof(FlushAsync), Path));
} }
// Calls to Flush are allowed if the page does not specify a Layout or if it is executing a section in the // Calls to Flush are allowed if the page does not specify a Layout or if it is executing a section in the
// Layout. // Layout.
if (!IsLayoutBeingRendered && !string.IsNullOrEmpty(Layout)) if (!IsLayoutBeingRendered && !string.IsNullOrEmpty(Layout))
{ {
var message = Resources.FormatLayoutCannotBeRendered(nameof(FlushAsync)); var message = Resources.FormatLayoutCannotBeRendered(Path, nameof(FlushAsync));
throw new InvalidOperationException(message); throw new InvalidOperationException(message);
} }
@ -817,7 +818,7 @@ namespace Microsoft.AspNet.Mvc.Razor
{ {
if (PreviousSectionWriters == null) if (PreviousSectionWriters == null)
{ {
throw new InvalidOperationException(Resources.FormatRazorPage_MethodCannotBeCalled(methodName)); throw new InvalidOperationException(Resources.FormatRazorPage_MethodCannotBeCalled(methodName, Path));
} }
} }
} }

View File

@ -179,7 +179,7 @@ namespace Microsoft.AspNet.Mvc.Razor
// the body content. Throwing this exception wouldn't return a 500 (since content has already been // the body content. Throwing this exception wouldn't return a 500 (since content has already been
// written), but a diagnostic component should be able to capture it. // written), but a diagnostic component should be able to capture it.
var message = Resources.FormatLayoutCannotBeRendered("FlushAsync"); var message = Resources.FormatLayoutCannotBeRendered(Path, nameof(Razor.RazorPage.FlushAsync));
throw new InvalidOperationException(message); throw new InvalidOperationException(message);
} }

View File

@ -133,7 +133,7 @@
<value>The layout view '{0}' could not be located. The following locations were searched:{1}</value> <value>The layout view '{0}' could not be located. The following locations were searched:{1}</value>
</data> </data>
<data name="LayoutCannotBeRendered" xml:space="preserve"> <data name="LayoutCannotBeRendered" xml:space="preserve">
<value>A layout page cannot be rendered after '{0}' has been invoked.</value> <value>Layout page '{0}' cannot be rendered after '{1}' has been invoked.</value>
</data> </data>
<data name="MvcRazorCodeParser_CannotHaveModelAndInheritsKeyword" xml:space="preserve"> <data name="MvcRazorCodeParser_CannotHaveModelAndInheritsKeyword" xml:space="preserve">
<value>The 'inherits' keyword is not allowed when a '{0}' keyword is used.</value> <value>The 'inherits' keyword is not allowed when a '{0}' keyword is used.</value>
@ -147,14 +147,14 @@
<data name="RazorPage_ThereIsNoActiveWritingScopeToEnd" xml:space="preserve"> <data name="RazorPage_ThereIsNoActiveWritingScopeToEnd" xml:space="preserve">
<value>There is no active writing scope to end.</value> <value>There is no active writing scope to end.</value>
</data> </data>
<data name="RazorPage_YouCannotFlushWhileInAWritingScope" xml:space="preserve"> <data name="RazorPage_CannotFlushWhileInAWritingScope" xml:space="preserve">
<value>You cannot flush while inside a writing scope.</value> <value>The {0} operation cannot be performed while inside a writing scope in '{1}'.</value>
</data> </data>
<data name="RazorPage_NullModelMetadata" xml:space="preserve"> <data name="RazorPage_NullModelMetadata" xml:space="preserve">
<value>The {0} was unable to provide metadata for expression '{1}'.</value> <value>The {0} was unable to provide metadata for expression '{1}'.</value>
</data> </data>
<data name="RazorPage_MethodCannotBeCalled" xml:space="preserve"> <data name="RazorPage_MethodCannotBeCalled" xml:space="preserve">
<value>{0} can only be called from a layout page.</value> <value>{0} invocation in '{1}' is invalid. {0} can only be called from a layout page.</value>
</data> </data>
<data name="RenderBodyNotCalled" xml:space="preserve"> <data name="RenderBodyNotCalled" xml:space="preserve">
<value>{0} has not been called for the page at '{1}'.</value> <value>{0} has not been called for the page at '{1}'.</value>
@ -163,10 +163,10 @@
<value>Section '{0}' is already defined.</value> <value>Section '{0}' is already defined.</value>
</data> </data>
<data name="SectionAlreadyRendered" xml:space="preserve"> <data name="SectionAlreadyRendered" xml:space="preserve">
<value>The section named '{0}' has already been rendered.</value> <value>{0} invocation in '{1}' is invalid. The section '{2}' has already been rendered.</value>
</data> </data>
<data name="SectionNotDefined" xml:space="preserve"> <data name="SectionNotDefined" xml:space="preserve">
<value>Section '{0}' is not defined.</value> <value>Section '{0}' is not defined in path '{1}'.</value>
</data> </data>
<data name="SectionsNotRendered" xml:space="preserve"> <data name="SectionsNotRendered" xml:space="preserve">
<value>The following sections have been defined but have not been rendered by the page at '{0}': '{1}'.</value> <value>The following sections have been defined but have not been rendered by the page at '{0}': '{1}'.</value>

View File

@ -165,7 +165,8 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
}); });
var client = server.CreateClient(); var client = server.CreateClient();
var expectedMessage = "A layout page cannot be rendered after 'FlushAsync' has been invoked."; var expectedMessage = "Layout page '/Views/FlushPoint/PageWithFlushBeforeLayout.cshtml'" +
" cannot be rendered after 'FlushAsync' has been invoked.";
// Act // Act
var stream = await client.GetStreamAsync("http://localhost/FlushPoint/PageWithFlushBeforeLayout"); var stream = await client.GetStreamAsync("http://localhost/FlushPoint/PageWithFlushBeforeLayout");

View File

@ -107,6 +107,7 @@ namespace Microsoft.AspNet.Mvc.Razor
var viewContext = CreateViewContext(); var viewContext = CreateViewContext();
var page = CreatePage(async v => var page = CreatePage(async v =>
{ {
v.Path = "/Views/TestPath/Test.cshtml";
v.StartTagHelperWritingScope(); v.StartTagHelperWritingScope();
await v.FlushAsync(); await v.FlushAsync();
}); });
@ -116,7 +117,8 @@ namespace Microsoft.AspNet.Mvc.Razor
() => page.ExecuteAsync()); () => page.ExecuteAsync());
// Assert // Assert
Assert.Equal("You cannot flush while inside a writing scope.", ex.Message); Assert.Equal("The FlushAsync operation cannot be performed while " +
"inside a writing scope in '/Views/TestPath/Test.cshtml'.", ex.Message);
} }
[Fact] [Fact]
@ -229,6 +231,7 @@ namespace Microsoft.AspNet.Mvc.Razor
Exception ex = null; Exception ex = null;
var page = CreatePage(v => var page = CreatePage(v =>
{ {
v.Path = "/Views/TestPath/Test.cshtml";
ex = Assert.Throws<InvalidOperationException>(() => v.RenderSection("bar")); ex = Assert.Throws<InvalidOperationException>(() => v.RenderSection("bar"));
}); });
@ -236,8 +239,9 @@ namespace Microsoft.AspNet.Mvc.Razor
await page.ExecuteAsync(); await page.ExecuteAsync();
// Assert // Assert
Assert.Equal("RenderSection can only be called from a layout page.", Assert.Equal("RenderSection invocation in '/Views/TestPath/Test.cshtml' is invalid. " +
ex.Message); "RenderSection can only be called from a layout page.",
ex.Message);
} }
[Fact] [Fact]
@ -246,6 +250,7 @@ namespace Microsoft.AspNet.Mvc.Razor
// Arrange // Arrange
var page = CreatePage(v => var page = CreatePage(v =>
{ {
v.Path = "/Views/TestPath/Test.cshtml";
v.RenderSection("bar"); v.RenderSection("bar");
}); });
page.PreviousSectionWriters = new Dictionary<string, RenderAsyncDelegate> page.PreviousSectionWriters = new Dictionary<string, RenderAsyncDelegate>
@ -257,18 +262,23 @@ namespace Microsoft.AspNet.Mvc.Razor
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => page.ExecuteAsync()); var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => page.ExecuteAsync());
// Assert // Assert
Assert.Equal("Section 'bar' is not defined.", ex.Message); Assert.Equal("Section 'bar' is not defined in path '/Views/TestPath/Test.cshtml'.", ex.Message);
} }
[Fact] [Fact]
public void IsSectionDefined_ThrowsIfPreviousSectionWritersIsNotRegistered() public void IsSectionDefined_ThrowsIfPreviousSectionWritersIsNotRegistered()
{ {
// Arrange // Arrange
var page = CreatePage(v => { }); var page = CreatePage(v =>
{
v.Path = "/Views/TestPath/Test.cshtml";
});
// Act and Assert // Act and Assert
page.ExecuteAsync();
ExceptionAssert.Throws<InvalidOperationException>(() => page.IsSectionDefined("foo"), ExceptionAssert.Throws<InvalidOperationException>(() => page.IsSectionDefined("foo"),
"IsSectionDefined can only be called from a layout page."); "IsSectionDefined invocation in '/Views/TestPath/Test.cshtml' is invalid." +
" IsSectionDefined can only be called from a layout page.");
} }
[Fact] [Fact]
@ -326,6 +336,7 @@ namespace Microsoft.AspNet.Mvc.Razor
var expected = new HelperResult(action: null); var expected = new HelperResult(action: null);
var page = CreatePage(v => var page = CreatePage(v =>
{ {
v.Path = "/Views/TestPath/Test.cshtml";
v.RenderSection("header"); v.RenderSection("header");
v.RenderSection("header"); v.RenderSection("header");
}); });
@ -338,7 +349,8 @@ namespace Microsoft.AspNet.Mvc.Razor
var ex = await Assert.ThrowsAsync<InvalidOperationException>(page.ExecuteAsync); var ex = await Assert.ThrowsAsync<InvalidOperationException>(page.ExecuteAsync);
// Assert // Assert
Assert.Equal("The section named 'header' has already been rendered.", ex.Message); Assert.Equal("RenderSectionAsync invocation in '/Views/TestPath/Test.cshtml' is invalid." +
" The section 'header' has already been rendered.", ex.Message);
} }
[Fact] [Fact]
@ -348,6 +360,7 @@ namespace Microsoft.AspNet.Mvc.Razor
var expected = new HelperResult(action: null); var expected = new HelperResult(action: null);
var page = CreatePage(async v => var page = CreatePage(async v =>
{ {
v.Path = "/Views/TestPath/Test.cshtml";
await v.RenderSectionAsync("header"); await v.RenderSectionAsync("header");
await v.RenderSectionAsync("header"); await v.RenderSectionAsync("header");
}); });
@ -360,7 +373,8 @@ namespace Microsoft.AspNet.Mvc.Razor
var ex = await Assert.ThrowsAsync<InvalidOperationException>(page.ExecuteAsync); var ex = await Assert.ThrowsAsync<InvalidOperationException>(page.ExecuteAsync);
// Assert // Assert
Assert.Equal("The section named 'header' has already been rendered.", ex.Message); Assert.Equal("RenderSectionAsync invocation in '/Views/TestPath/Test.cshtml' is invalid." +
" The section 'header' has already been rendered.", ex.Message);
} }
[Fact] [Fact]
@ -370,6 +384,7 @@ namespace Microsoft.AspNet.Mvc.Razor
var expected = new HelperResult(action: null); var expected = new HelperResult(action: null);
var page = CreatePage(async v => var page = CreatePage(async v =>
{ {
v.Path = "/Views/TestPath/Test.cshtml";
v.RenderSection("header"); v.RenderSection("header");
await v.RenderSectionAsync("header"); await v.RenderSectionAsync("header");
}); });
@ -382,7 +397,8 @@ namespace Microsoft.AspNet.Mvc.Razor
var ex = await Assert.ThrowsAsync<InvalidOperationException>(page.ExecuteAsync); var ex = await Assert.ThrowsAsync<InvalidOperationException>(page.ExecuteAsync);
// Assert // Assert
Assert.Equal("The section named 'header' has already been rendered.", ex.Message); Assert.Equal("RenderSectionAsync invocation in '/Views/TestPath/Test.cshtml' is invalid." +
" The section 'header' has already been rendered.", ex.Message);
} }
[Fact] [Fact]
@ -392,6 +408,7 @@ namespace Microsoft.AspNet.Mvc.Razor
var expected = new HelperResult(action: null); var expected = new HelperResult(action: null);
var page = CreatePage(async v => var page = CreatePage(async v =>
{ {
v.Path = "/Views/TestPath/Test.cshtml";
await v.RenderSectionAsync("header"); await v.RenderSectionAsync("header");
}); });
@ -399,7 +416,8 @@ namespace Microsoft.AspNet.Mvc.Razor
var ex = await Assert.ThrowsAsync<InvalidOperationException>(page.ExecuteAsync); var ex = await Assert.ThrowsAsync<InvalidOperationException>(page.ExecuteAsync);
// Assert // Assert
Assert.Equal("RenderSectionAsync can only be called from a layout page.", ex.Message); Assert.Equal("RenderSectionAsync invocation in '/Views/TestPath/Test.cshtml' is invalid. " +
"RenderSectionAsync can only be called from a layout page.", ex.Message);
} }
[Fact] [Fact]
@ -566,11 +584,13 @@ namespace Microsoft.AspNet.Mvc.Razor
public async Task FlushAsync_ThrowsIfTheLayoutHasBeenSet() public async Task FlushAsync_ThrowsIfTheLayoutHasBeenSet()
{ {
// Arrange // Arrange
var expected = @"A layout page cannot be rendered after 'FlushAsync' has been invoked."; var expected = "Layout page '/Views/TestPath/Test.cshtml' cannot be rendered" +
" after 'FlushAsync' has been invoked.";
var writer = new Mock<TextWriter>(); var writer = new Mock<TextWriter>();
var context = CreateViewContext(writer.Object); var context = CreateViewContext(writer.Object);
var page = CreatePage(async p => var page = CreatePage(async p =>
{ {
p.Path = "/Views/TestPath/Test.cshtml";
p.Layout = "foo"; p.Layout = "foo";
await p.FlushAsync(); await p.FlushAsync();
}, context); }, context);

View File

@ -961,9 +961,11 @@ namespace Microsoft.AspNet.Mvc.Razor
public async Task RenderAsync_ThrowsIfLayoutIsSpecifiedWhenNotBuffered() public async Task RenderAsync_ThrowsIfLayoutIsSpecifiedWhenNotBuffered()
{ {
// Arrange // Arrange
var expected = @"A layout page cannot be rendered after 'FlushAsync' has been invoked."; var expected = "Layout page '/Views/TestPath/Test.cshtml' cannot be rendered" +
" after 'FlushAsync' has been invoked.";
var page = new TestableRazorPage(v => var page = new TestableRazorPage(v =>
{ {
v.Path = "/Views/TestPath/Test.cshtml";
v.WriteLiteral("before-flush" + Environment.NewLine); v.WriteLiteral("before-flush" + Environment.NewLine);
v.FlushAsync().Wait(); v.FlushAsync().Wait();
v.Layout = "test-layout"; v.Layout = "test-layout";
@ -986,9 +988,11 @@ namespace Microsoft.AspNet.Mvc.Razor
public async Task RenderAsync_ThrowsIfFlushWasInvokedInsideRenderedSectionAndLayoutWasSet() public async Task RenderAsync_ThrowsIfFlushWasInvokedInsideRenderedSectionAndLayoutWasSet()
{ {
// Arrange // Arrange
var expected = @"A layout page cannot be rendered after 'FlushAsync' has been invoked."; var expected = "Layout page '/Views/TestPath/Test.cshtml' cannot be rendered" +
" after 'FlushAsync' has been invoked.";
var page = new TestableRazorPage(v => var page = new TestableRazorPage(v =>
{ {
v.Path = "/Views/TestPath/Test.cshtml";
v.HtmlEncoder = new HtmlEncoder(); v.HtmlEncoder = new HtmlEncoder();
v.DefineSection("foo", async writer => v.DefineSection("foo", async writer =>
{ {