Skip to content

Commit b5aebd6

Browse files
Handle if errors prop exsits even if we have an response exception (#3928)
Co-authored-by: Pascal Senn <senn.pasc@gmail.com>
1 parent 649ee26 commit b5aebd6

File tree

108 files changed

+1089
-169
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+1089
-169
lines changed

src/StrawberryShake/CodeGeneration/src/CodeGeneration.CSharp/Generators/JsonResultBuilderGenerator.cs

+57-40
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,33 @@ private void AddBuildMethod(
299299

300300
buildMethod.AddEmptyLine();
301301

302-
302+
const string errorsElement = nameof(errorsElement);
303303

304304
buildMethod.AddEmptyLine();
305305
buildMethod.AddCode(
306306
IfBuilder.New()
307307
.SetCondition("response.Exception is null")
308308
.AddCode(CreateBuildDataSerialization())
309-
.AddElse(CreateDataError("response.Exception"))
310-
);
309+
.AddElse(
310+
IfBuilder
311+
.New()
312+
.SetCondition(
313+
ConditionBuilder
314+
.New()
315+
.Set("response.Body != null")
316+
.And("response.Body.RootElement.TryGetProperty(" +
317+
$"\"errors\", out {TypeNames.JsonElement} {errorsElement})"))
318+
.AddCode(
319+
AssignmentBuilder
320+
.New()
321+
.SetLefthandSide("errors")
322+
.SetRighthandSide(MethodCallBuilder
323+
.Inline()
324+
.SetMethodName(TypeNames.ParseError)
325+
.AddArgument(errorsElement))
326+
)
327+
.AddElse(CreateDataError("response.Exception")))
328+
);
311329

312330
buildMethod.AddEmptyLine();
313331
buildMethod.AddCode(
@@ -326,45 +344,44 @@ private void AddBuildMethod(
326344
private TryCatchBuilder CreateBuildDataSerialization()
327345
{
328346
return TryCatchBuilder
329-
.New()
330-
.AddTryCode(
331-
IfBuilder
332-
.New()
333-
.SetCondition(
334-
ConditionBuilder
335-
.New()
336-
.Set("response.Body != null"))
337-
.AddCode(
338-
IfBuilder
339-
.New()
340-
.SetCondition(
341-
ConditionBuilder
342-
.New()
343-
.Set("response.Body.RootElement.TryGetProperty(" +
344-
$"\"data\", out {TypeNames.JsonElement} " +
345-
"dataElement) && dataElement.ValueKind == " +
346-
$"{TypeNames.JsonValueKind}.Object"))
347-
.AddCode("data = BuildData(dataElement);"))
348-
.AddCode(
349-
IfBuilder
350-
.New()
351-
.SetCondition(
352-
ConditionBuilder
353-
.New()
354-
.Set(
355-
"response.Body.RootElement.TryGetProperty(" +
356-
$"\"errors\", out {TypeNames.JsonElement} " +
357-
"errorsElement)"))
358-
.AddCode($"errors = {TypeNames.ParseError}(errorsElement);")))
359-
.AddCatchBlock(
360-
CatchBlockBuilder
361-
.New()
362-
.SetExceptionVariable("ex")
363-
.AddCode(CreateDataError()));
347+
.New()
348+
.AddTryCode(
349+
IfBuilder
350+
.New()
351+
.SetCondition(
352+
ConditionBuilder
353+
.New()
354+
.Set("response.Body != null"))
355+
.AddCode(
356+
IfBuilder
357+
.New()
358+
.SetCondition(
359+
ConditionBuilder
360+
.New()
361+
.Set("response.Body.RootElement.TryGetProperty(" +
362+
$"\"data\", out {TypeNames.JsonElement} " +
363+
"dataElement) && dataElement.ValueKind == " +
364+
$"{TypeNames.JsonValueKind}.Object"))
365+
.AddCode("data = BuildData(dataElement);"))
366+
.AddCode(
367+
IfBuilder
368+
.New()
369+
.SetCondition(
370+
ConditionBuilder
371+
.New()
372+
.Set(
373+
"response.Body.RootElement.TryGetProperty(" +
374+
$"\"errors\", out {TypeNames.JsonElement} " +
375+
"errorsElement)"))
376+
.AddCode($"errors = {TypeNames.ParseError}(errorsElement);")))
377+
.AddCatchBlock(
378+
CatchBlockBuilder
379+
.New()
380+
.SetExceptionVariable("ex")
381+
.AddCode(CreateDataError()));
364382
}
365383

366-
private static AssignmentBuilder CreateDataError(
367-
string exception = "ex")
384+
private static AssignmentBuilder CreateDataError(string exception = "ex")
368385
{
369386
string dict = TypeNames.Dictionary.WithGeneric(
370387
TypeNames.String,

src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/Integration/AnyScalarDefaultSerializationTest.Client.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,14 @@ public GetJsonBuilder(global::StrawberryShake.IEntityStore entityStore, global::
413413
}
414414
else
415415
{
416-
errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
416+
if (response.Body != null && response.Body.RootElement.TryGetProperty("errors", out global::System.Text.Json.JsonElement errorsElement))
417+
{
418+
errors = global::StrawberryShake.Json.JsonErrorParser.ParseErrors(errorsElement);
419+
}
420+
else
421+
{
422+
errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
423+
}
417424
}
418425

419426
return new global::StrawberryShake.OperationResult<IGetJsonResult>(data?.Result, data?.Info, _resultDataFactory, errors);

src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/Integration/EntityIdOrDataTest.Client.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,14 @@ public GetFooBuilder(global::StrawberryShake.IEntityStore entityStore, global::S
968968
}
969969
else
970970
{
971-
errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
971+
if (response.Body != null && response.Body.RootElement.TryGetProperty("errors", out global::System.Text.Json.JsonElement errorsElement))
972+
{
973+
errors = global::StrawberryShake.Json.JsonErrorParser.ParseErrors(errorsElement);
974+
}
975+
else
976+
{
977+
errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
978+
}
972979
}
973980

974981
return new global::StrawberryShake.OperationResult<IGetFooResult>(data?.Result, data?.Info, _resultDataFactory, errors);

src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/Integration/MultiProfileTest.Client.cs

+24-3
Original file line numberDiff line numberDiff line change
@@ -2255,7 +2255,14 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
22552255
}
22562256
else
22572257
{
2258-
errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
2258+
if (response.Body != null && response.Body.RootElement.TryGetProperty("errors", out global::System.Text.Json.JsonElement errorsElement))
2259+
{
2260+
errors = global::StrawberryShake.Json.JsonErrorParser.ParseErrors(errorsElement);
2261+
}
2262+
else
2263+
{
2264+
errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
2265+
}
22592266
}
22602267

22612268
return new global::StrawberryShake.OperationResult<IGetHeroResult>(data?.Result, data?.Info, _resultDataFactory, errors);
@@ -2444,7 +2451,14 @@ public OnReviewSubBuilder(global::StrawberryShake.IEntityStore entityStore, glob
24442451
}
24452452
else
24462453
{
2447-
errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
2454+
if (response.Body != null && response.Body.RootElement.TryGetProperty("errors", out global::System.Text.Json.JsonElement errorsElement))
2455+
{
2456+
errors = global::StrawberryShake.Json.JsonErrorParser.ParseErrors(errorsElement);
2457+
}
2458+
else
2459+
{
2460+
errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
2461+
}
24482462
}
24492463

24502464
return new global::StrawberryShake.OperationResult<IOnReviewSubResult>(data?.Result, data?.Info, _resultDataFactory, errors);
@@ -2557,7 +2571,14 @@ public CreateReviewMutBuilder(global::StrawberryShake.IEntityStore entityStore,
25572571
}
25582572
else
25592573
{
2560-
errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
2574+
if (response.Body != null && response.Body.RootElement.TryGetProperty("errors", out global::System.Text.Json.JsonElement errorsElement))
2575+
{
2576+
errors = global::StrawberryShake.Json.JsonErrorParser.ParseErrors(errorsElement);
2577+
}
2578+
else
2579+
{
2580+
errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
2581+
}
25612582
}
25622583

25632584
return new global::StrawberryShake.OperationResult<ICreateReviewMutResult>(data?.Result, data?.Info, _resultDataFactory, errors);

src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/Integration/StarWarsGetFriendsNoStoreTest.Client.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,14 @@ public GetHeroBuilder(global::StrawberryShake.IOperationResultDataFactory<global
950950
}
951951
else
952952
{
953-
errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
953+
if (response.Body != null && response.Body.RootElement.TryGetProperty("errors", out global::System.Text.Json.JsonElement errorsElement))
954+
{
955+
errors = global::StrawberryShake.Json.JsonErrorParser.ParseErrors(errorsElement);
956+
}
957+
else
958+
{
959+
errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
960+
}
954961
}
955962

956963
return new global::StrawberryShake.OperationResult<IGetHeroResult>(data?.Result, data?.Info, _resultDataFactory, errors);

src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/Integration/StarWarsGetFriendsTest.Client.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,14 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
11941194
}
11951195
else
11961196
{
1197-
errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
1197+
if (response.Body != null && response.Body.RootElement.TryGetProperty("errors", out global::System.Text.Json.JsonElement errorsElement))
1198+
{
1199+
errors = global::StrawberryShake.Json.JsonErrorParser.ParseErrors(errorsElement);
1200+
}
1201+
else
1202+
{
1203+
errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
1204+
}
11981205
}
11991206

12001207
return new global::StrawberryShake.OperationResult<IGetHeroResult>(data?.Result, data?.Info, _resultDataFactory, errors);

src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/Integration/StarWarsGetHeroTest.Client.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,14 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
694694
}
695695
else
696696
{
697-
errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
697+
if (response.Body != null && response.Body.RootElement.TryGetProperty("errors", out global::System.Text.Json.JsonElement errorsElement))
698+
{
699+
errors = global::StrawberryShake.Json.JsonErrorParser.ParseErrors(errorsElement);
700+
}
701+
else
702+
{
703+
errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
704+
}
698705
}
699706

700707
return new global::StrawberryShake.OperationResult<IGetHeroResult>(data?.Result, data?.Info, _resultDataFactory, errors);

src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/Integration/StarWarsIntrospectionTest.Client.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -4252,7 +4252,14 @@ public IntrospectionQueryBuilder(global::StrawberryShake.IEntityStore entityStor
42524252
}
42534253
else
42544254
{
4255-
errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
4255+
if (response.Body != null && response.Body.RootElement.TryGetProperty("errors", out global::System.Text.Json.JsonElement errorsElement))
4256+
{
4257+
errors = global::StrawberryShake.Json.JsonErrorParser.ParseErrors(errorsElement);
4258+
}
4259+
else
4260+
{
4261+
errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
4262+
}
42564263
}
42574264

42584265
return new global::StrawberryShake.OperationResult<IIntrospectionQueryResult>(data?.Result, data?.Info, _resultDataFactory, errors);

src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/Integration/StarWarsTypeNameOnInterfacesTest.Client.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,14 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
699699
}
700700
else
701701
{
702-
errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
702+
if (response.Body != null && response.Body.RootElement.TryGetProperty("errors", out global::System.Text.Json.JsonElement errorsElement))
703+
{
704+
errors = global::StrawberryShake.Json.JsonErrorParser.ParseErrors(errorsElement);
705+
}
706+
else
707+
{
708+
errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
709+
}
703710
}
704711

705712
return new global::StrawberryShake.OperationResult<IGetHeroResult>(data?.Result, data?.Info, _resultDataFactory, errors);

0 commit comments

Comments
 (0)