diff --git a/src/HotChocolate/Core/src/Abstractions/Error.cs b/src/HotChocolate/Core/src/Abstractions/Error.cs index a42e81955c4..f1d2086627d 100644 --- a/src/HotChocolate/Core/src/Abstractions/Error.cs +++ b/src/HotChocolate/Core/src/Abstractions/Error.cs @@ -84,70 +84,45 @@ public IError WithMessage(string message) /// public IError WithCode(string? code) { + IReadOnlyDictionary? extensions; + if (string.IsNullOrEmpty(code)) { - return RemoveCode(); + extensions = Extensions; + + if (Extensions?.ContainsKey(_code) == true) + { + var temp = new OrderedDictionary(Extensions); + temp.Remove(_code); + extensions = temp; + } + + return new Error(Message, null, Path, Locations, extensions, Exception); } - var extensions = Extensions is null + extensions = Extensions is null ? new OrderedDictionary { [_code] = code, } : new OrderedDictionary(Extensions) { [_code] = code, }; return new Error(Message, code, Path, Locations, extensions, Exception); } /// - public IError RemoveCode() - { - var extensions = Extensions; - - if (Extensions is { }) - { - var temp = new OrderedDictionary(Extensions); - temp.Remove(_code); - extensions = temp; - } - - return new Error(Message, null, Path, Locations, extensions, Exception); - } - - /// - public IError WithPath(Path? path) - => path is null - ? RemovePath() - : new Error(Message, Code, path, Locations, Extensions, Exception); - - /// - public IError WithPath(IReadOnlyList? path) - => WithPath(path is null ? null : Path.FromList(path)); + public IError WithPath(Path path) + => new Error(Message, Code, path, Locations, Extensions, Exception); /// - public IError RemovePath() - => new Error(Message, Code, null, Locations, Extensions, Exception); + public IError WithPath(IReadOnlyList path) + => WithPath(Path.FromList(path)); /// - public IError WithLocations(IReadOnlyList? locations) - => locations is null - ? RemoveLocations() - : new Error(Message, Code, Path, locations, Extensions, Exception); + public IError WithLocations(IReadOnlyList locations) + => new Error(Message, Code, Path, locations, Extensions, Exception); /// - public IError RemoveLocations() - => new Error(Message, Code, Path, null, Extensions, Exception); - - /// - public IError WithExtensions(IReadOnlyDictionary extensions) - { - if (extensions is null) - { - throw new ArgumentNullException(nameof(extensions)); - } - - return new Error(Message, Code, Path, Locations, extensions, Exception); - } - - /// - public IError RemoveExtensions() - => new Error(Message, Code, Path, Locations, null, Exception); + public IError WithExtensions(IReadOnlyDictionary? extensions) + => extensions is null + ? new Error(Message, Code, Path, Locations, null, Exception) + : new Error(Message, Code, Path, Locations, extensions, Exception); /// public IError SetExtension(string key, object? value) @@ -176,7 +151,7 @@ public IError RemoveExtension(string key) nameof(key)); } - if (Extensions is null) + if (Extensions is null || !Extensions.ContainsKey(key)) { return this; } @@ -192,10 +167,6 @@ public IError RemoveExtension(string key) /// public IError WithException(Exception? exception) => exception is null - ? RemoveException() + ? new Error(Message, Code, Path, Locations, Extensions) : new Error(Message, Code, Path, Locations, Extensions, exception); - - /// - public IError RemoveException() - => new Error(Message, Code, Path, Locations, Extensions); } diff --git a/src/HotChocolate/Core/src/Abstractions/ErrorBuilder.cs b/src/HotChocolate/Core/src/Abstractions/ErrorBuilder.cs index 5b7f1ec10e1..29d0053cace 100644 --- a/src/HotChocolate/Core/src/Abstractions/ErrorBuilder.cs +++ b/src/HotChocolate/Core/src/Abstractions/ErrorBuilder.cs @@ -61,7 +61,8 @@ public IErrorBuilder SetCode(string? code) { if (string.IsNullOrEmpty(code)) { - return RemoveCode(); + _code = null; + return this; } _code = code; @@ -69,17 +70,12 @@ public IErrorBuilder SetCode(string? code) return this; } - public IErrorBuilder RemoveCode() - { - _code = null; - return this; - } - public IErrorBuilder SetPath(Path? path) { if (path is null) { - return RemovePath(); + _path = null; + return this; } _path = path; @@ -89,12 +85,6 @@ public IErrorBuilder SetPath(Path? path) public IErrorBuilder SetPath(IReadOnlyList? path) => SetPath(path is null ? null : Path.FromList(path)); - public IErrorBuilder RemovePath() - { - _path = null; - return this; - } - public IErrorBuilder AddLocation(Location location) { if (_dirtyLocation && _locations is not null) @@ -194,19 +184,14 @@ public IErrorBuilder SetException(Exception? exception) { if (exception is null) { - return RemoveException(); + _exception = null; + return this; } _exception = exception; return this; } - public IErrorBuilder RemoveException() - { - _exception = null; - return this; - } - public IError Build() { if (string.IsNullOrEmpty(_message)) diff --git a/src/HotChocolate/Core/src/Abstractions/IError.cs b/src/HotChocolate/Core/src/Abstractions/IError.cs index 83b9cff07b6..1dd86f4725b 100644 --- a/src/HotChocolate/Core/src/Abstractions/IError.cs +++ b/src/HotChocolate/Core/src/Abstractions/IError.cs @@ -70,16 +70,6 @@ public interface IError /// IError WithCode(string? code); - /// - /// Creates a new error that contains all properties of this error - /// but with removed. - /// - /// - /// Returns a new error that contains all properties of this error - /// but with removed. - /// - IError RemoveCode(); - /// /// Creates a new error that contains all properties of this error /// but with the specified . @@ -91,7 +81,7 @@ public interface IError /// Returns a new error that contains all properties of this error /// but with the specified . /// - IError WithPath(Path? path); + IError WithPath(Path path); /// /// Creates a new error that contains all properties of this error @@ -104,17 +94,7 @@ public interface IError /// Returns a new error that contains all properties of this error /// but with the specified . /// - IError WithPath(IReadOnlyList? path); - - /// - /// Creates a new error that contains all properties of this error - /// but with the removed. - /// - /// - /// Returns a new error that contains all properties of this error - /// but with the removed. - /// - IError RemovePath(); + IError WithPath(IReadOnlyList path); /// /// Creates a new error that contains all properties of this error @@ -128,17 +108,7 @@ public interface IError /// Returns a new error that contains all properties of this error /// but with the specified . /// - IError WithLocations(IReadOnlyList? locations); - - /// - /// Creates a new error that contains all properties of this error - /// but with the removed. - /// - /// - /// Returns a new error that contains all properties of this error - /// but with the removed. - /// - IError RemoveLocations(); + IError WithLocations(IReadOnlyList locations); /// /// Creates a new error that contains all properties of this error @@ -151,17 +121,7 @@ public interface IError /// Returns a new error that contains all properties of this error /// but with the specified . /// - IError WithExtensions(IReadOnlyDictionary extensions); - - /// - /// Creates a new error that contains all properties of this error - /// but with the removed. - /// - /// - /// Returns a new error that contains all properties of this error - /// but with the removed. - /// - IError RemoveExtensions(); + IError WithExtensions(IReadOnlyDictionary? extensions); /// /// Creates a new error that contains all properties of this error @@ -204,14 +164,4 @@ public interface IError /// but with the specified . /// IError WithException(Exception? exception); - - /// - /// Creates a new error that contains all properties of this error - /// but removed the exception from it. - /// - /// - /// Returns a new error that contains all properties of this error - /// but without any exception details. - /// - IError RemoveException(); } diff --git a/src/HotChocolate/Core/src/Abstractions/IErrorBuilder.cs b/src/HotChocolate/Core/src/Abstractions/IErrorBuilder.cs index 083241af9ef..fb5d884704e 100644 --- a/src/HotChocolate/Core/src/Abstractions/IErrorBuilder.cs +++ b/src/HotChocolate/Core/src/Abstractions/IErrorBuilder.cs @@ -8,14 +8,10 @@ public interface IErrorBuilder IErrorBuilder SetCode(string? code); - IErrorBuilder RemoveCode(); - IErrorBuilder SetPath(IReadOnlyList? path); IErrorBuilder SetPath(Path? path); - IErrorBuilder RemovePath(); - IErrorBuilder AddLocation(Location location); IErrorBuilder AddLocation(int line, int column); @@ -28,8 +24,6 @@ public interface IErrorBuilder IErrorBuilder SetException(Exception? exception); - IErrorBuilder RemoveException(); - IErrorBuilder SetExtension(string key, object? value); IErrorBuilder RemoveExtension(string key); diff --git a/src/HotChocolate/Core/test/Abstractions.Tests/ErrorTests.cs b/src/HotChocolate/Core/test/Abstractions.Tests/ErrorTests.cs index 6afcc79b2b0..ed2ebf7fdad 100644 --- a/src/HotChocolate/Core/test/Abstractions.Tests/ErrorTests.cs +++ b/src/HotChocolate/Core/test/Abstractions.Tests/ErrorTests.cs @@ -22,7 +22,7 @@ public void RemoveCode() IError error = new Error("123", code: "foo"); // act - error = error.RemoveCode(); + error = error.WithCode(null); // assert Assert.Null(error.Code); @@ -59,7 +59,7 @@ public void RemoveException() Assert.NotNull(error.Exception); // act - error = error.RemoveException(); + error = error.WithException(null); // assert Assert.Null(error.Exception); diff --git a/src/HotChocolate/Core/test/Execution.Tests/Processing/VariableCoercionHelperTests.cs b/src/HotChocolate/Core/test/Execution.Tests/Processing/VariableCoercionHelperTests.cs index 603b4c914f9..2ab724dd061 100644 --- a/src/HotChocolate/Core/test/Execution.Tests/Processing/VariableCoercionHelperTests.cs +++ b/src/HotChocolate/Core/test/Execution.Tests/Processing/VariableCoercionHelperTests.cs @@ -244,7 +244,7 @@ public void Coerce_Nullable_String_Variable_With_Default_Where_Plain_Value_Is_Pr Assert.Equal("abc", t.Key); Assert.Equal("String", Assert.IsType(t.Value.Type).Name); Assert.Equal("xyz", t.Value.Value); - t.Value.ValueLiteral!.ToString().MatchSnapshot(); + t.Value.ValueLiteral.ToString().MatchSnapshot(); }); } @@ -396,7 +396,7 @@ public void Coerce_Nullable_ReviewInput_Variable_With_Dictionary() Assert.Equal("abc", t.Key); Assert.Equal("ReviewInput", Assert.IsType(t.Value.Type).Name); Assert.Equal(5, Assert.IsType(t.Value.Value).Stars); - t.Value.ValueLiteral!.ToString().MatchSnapshot(); + t.Value.ValueLiteral.ToString().MatchSnapshot(); }); } @@ -434,7 +434,7 @@ public void Coerce_Nullable_ReviewInput_Variable_With_Review_Object() Assert.Equal("abc", t.Key); Assert.Equal("ReviewInput", Assert.IsType(t.Value.Type).Name); Assert.Equal(5, Assert.IsType(t.Value.Value).Stars); - t.Value.ValueLiteral!.ToString().MatchSnapshot(); + t.Value.ValueLiteral.ToString().MatchSnapshot(); }); } @@ -532,7 +532,7 @@ void Action() => helper.CoerceVariableValues( // assert Assert.Throws(Action) - .Errors.Select(t => t.RemoveException()) + .Errors.Select(t => t.WithException(null)) .ToList() .MatchSnapshot(); } @@ -628,7 +628,7 @@ void Action() => helper.CoerceVariableValues( // assert Assert.Throws(Action) - .Errors.Select(t => t.RemoveException()) + .Errors.Select(t => t.WithException(null)) .ToList() .MatchSnapshot(); } @@ -690,7 +690,7 @@ enum TestEnum { Assert.Equal("abc", t.Key); Assert.Equal( "[ { enum: Foo }, { enum: Bar } ]", - t.Value.ValueLiteral!.ToString()); + t.Value.ValueLiteral.ToString()); }); } @@ -751,7 +751,7 @@ enum TestEnum { Assert.Equal("abc", t.Key); Assert.Equal( "[ { enum: Foo }, { enum: Bar } ]", - t.Value.ValueLiteral!.ToString()); + t.Value.ValueLiteral.ToString()); }); } @@ -809,7 +809,7 @@ enum TestEnum { t => { Assert.Equal("abc", t.Key); - Assert.Equal("{ enum: Foo, enum2: Bar }", t.Value.ValueLiteral!.ToString()); + Assert.Equal("{ enum: Foo, enum2: Bar }", t.Value.ValueLiteral.ToString()); }); } @@ -867,7 +867,7 @@ enum TestEnum { t => { Assert.Equal("abc", t.Key); - Assert.Equal("{ enum: Foo, enum2: Bar }", t.Value.ValueLiteral!.ToString()); + Assert.Equal("{ enum: Foo, enum2: Bar }", t.Value.ValueLiteral.ToString()); }); }