Skip to content

Commit

Permalink
chore: align with upstream exception handling (#2975)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored Aug 1, 2024
1 parent 48cd7d4 commit 5651c72
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

| | Linux | macOS | Windows |
| :--- | :---: | :---: | :---: |
| Chromium <!-- GEN:chromium-version -->128.0.6613.7<!-- GEN:stop --> ||||
| Chromium <!-- GEN:chromium-version -->128.0.6613.18<!-- GEN:stop --> ||||
| WebKit <!-- GEN:webkit-version -->18.0<!-- GEN:stop --> ||||
| Firefox <!-- GEN:firefox-version -->128.0<!-- GEN:stop --> ||||

Expand Down
2 changes: 1 addition & 1 deletion src/Common/Version.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<AssemblyVersion>1.45.0</AssemblyVersion>
<PackageVersion>$(AssemblyVersion)</PackageVersion>
<DriverVersion>1.46.0-beta-1722359450000</DriverVersion>
<DriverVersion>1.46.0-beta-1722491095000</DriverVersion>
<ReleaseVersion>$(AssemblyVersion)</ReleaseVersion>
<FileVersion>$(AssemblyVersion)</FileVersion>
<NoDefaultExcludes>true</NoDefaultExcludes>
Expand Down
52 changes: 45 additions & 7 deletions src/Playwright.Tests/PageEvaluateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -547,24 +547,62 @@ public async Task ShouldNotLeakHandles()
}

[PlaywrightTest("page-evaluate.spec.ts", "should evaluate exception with a function on the stack")]
[Ignore("todo https://github.com/microsoft/playwright-dotnet/issues/2947")]
public async Task ShouldEvaluateExceptionWithAFunctionOnTheStack()
{
var exception = await Page.EvaluateAsync<PlaywrightException>(@"() => {
var exception = await Page.EvaluateAsync<Exception>(@"() => {
return (function functionOnStack() {
return new Error('error message');
})();
}");
Assert.Equals("error message", exception.Message);
StringAssert.Contains("functionOnStack", exception.StackTrace);
StringAssert.Contains("Error: error message", exception.Message);
StringAssert.Contains("functionOnStack", exception.Message);
}

[PlaywrightTest("page-evaluate.spec.ts", "should evaluate exception")]
[Ignore("todo https://github.com/microsoft/playwright-dotnet/issues/2947")]
public async Task ShouldEvaluateException()
{
string exception = await Page.EvaluateAsync<string>(@"() => new Error('error message')");
StringAssert.Contains("Error: error message", exception);
var exception = await Page.EvaluateAsync<object>(@"() => {
function innerFunction() {
const e = new Error('error message');
e.name = 'foobar';
return e;
}
return innerFunction();
}");
Assert.IsInstanceOf<Exception>(exception);
StringAssert.Contains("foobar: error message", (exception as Exception).Message);
StringAssert.Contains("innerFunction", (exception as Exception).Message);
StringAssert.Contains("foobar: error message", exception.ToString());
}

[PlaywrightTest("page-evaluate.spec.ts", "should pass exception argument")]
public async Task ShouldPassExceptionArgument()
{
Exception InnerFunction()
{
try
{
// We need to throw so a stack gets assigned
throw new PlaywrightException("error message");
}
catch (Exception e)
{
return e;
}
}
var exception = await Page.EvaluateAsync<ErrorStruct>(@"e => {
return { message: e.message, name: e.name, stack: e.stack };
}", InnerFunction());
StringAssert.Contains("error message", exception.Message);
StringAssert.Contains("PlaywrightException: error message", exception.Stack);
Assert.AreEqual("PlaywrightException", exception.Name);
}

public struct ErrorStruct
{
public string Message { get; set; }
public string Name { get; set; }
public string Stack { get; set; }
}

[PlaywrightTest("page-evaluate.spec.ts", "should evaluate date")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ internal static object Serialize(object value, List<EvaluateArgumentGuidElement>
return new { bi = bigInteger.ToString(CultureInfo.InvariantCulture) };
}

if (value is Exception exception)
{
return new Dictionary<string, object>
{
["e"] = new Dictionary<string, object>
{
["n"] = exception.GetType().Name,
["m"] = exception.Message,
["s"] = exception.StackTrace ?? string.Empty,
},
};
}

if (value is Regex regex)
{
return new { r = new { p = regex.ToString(), f = regex.Options.GetInlineFlags() } };
Expand Down Expand Up @@ -330,6 +343,11 @@ private static object ParseEvaluateResultToExpando(JsonElement result, IDictiona
return BigInteger.Parse(bigInt.ToObject<string>(), CultureInfo.InvariantCulture);
}

if (result.TryGetProperty("e", out var error))
{
return new Exception(error.GetProperty("s").ToString());
}

if (result.TryGetProperty("r", out var regex))
{
return new Regex(regex.GetProperty("p").ToString(), RegexOptionsExtensions.FromInlineFlags(regex.GetProperty("f").ToString()));
Expand Down

0 comments on commit 5651c72

Please sign in to comment.