Skip to content

Commit b8fcc1c

Browse files
Remove obsolete methods: RemovePath and RemoveLocations (#7860)
Co-authored-by: Michael Staib <[email protected]>
1 parent df2f85d commit b8fcc1c

File tree

6 files changed

+46
-146
lines changed

6 files changed

+46
-146
lines changed

src/HotChocolate/Core/src/Abstractions/Error.cs

Lines changed: 25 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -84,70 +84,45 @@ public IError WithMessage(string message)
8484
/// <inheritdoc />
8585
public IError WithCode(string? code)
8686
{
87+
IReadOnlyDictionary<string, object?>? extensions;
88+
8789
if (string.IsNullOrEmpty(code))
8890
{
89-
return RemoveCode();
91+
extensions = Extensions;
92+
93+
if (Extensions?.ContainsKey(_code) == true)
94+
{
95+
var temp = new OrderedDictionary<string, object?>(Extensions);
96+
temp.Remove(_code);
97+
extensions = temp;
98+
}
99+
100+
return new Error(Message, null, Path, Locations, extensions, Exception);
90101
}
91102

92-
var extensions = Extensions is null
103+
extensions = Extensions is null
93104
? new OrderedDictionary<string, object?> { [_code] = code, }
94105
: new OrderedDictionary<string, object?>(Extensions) { [_code] = code, };
95106
return new Error(Message, code, Path, Locations, extensions, Exception);
96107
}
97108

98109
/// <inheritdoc />
99-
public IError RemoveCode()
100-
{
101-
var extensions = Extensions;
102-
103-
if (Extensions is { })
104-
{
105-
var temp = new OrderedDictionary<string, object?>(Extensions);
106-
temp.Remove(_code);
107-
extensions = temp;
108-
}
109-
110-
return new Error(Message, null, Path, Locations, extensions, Exception);
111-
}
112-
113-
/// <inheritdoc />
114-
public IError WithPath(Path? path)
115-
=> path is null
116-
? RemovePath()
117-
: new Error(Message, Code, path, Locations, Extensions, Exception);
118-
119-
/// <inheritdoc />
120-
public IError WithPath(IReadOnlyList<object>? path)
121-
=> WithPath(path is null ? null : Path.FromList(path));
110+
public IError WithPath(Path path)
111+
=> new Error(Message, Code, path, Locations, Extensions, Exception);
122112

123113
/// <inheritdoc />
124-
public IError RemovePath()
125-
=> new Error(Message, Code, null, Locations, Extensions, Exception);
114+
public IError WithPath(IReadOnlyList<object> path)
115+
=> WithPath(Path.FromList(path));
126116

127117
/// <inheritdoc />
128-
public IError WithLocations(IReadOnlyList<Location>? locations)
129-
=> locations is null
130-
? RemoveLocations()
131-
: new Error(Message, Code, Path, locations, Extensions, Exception);
118+
public IError WithLocations(IReadOnlyList<Location> locations)
119+
=> new Error(Message, Code, Path, locations, Extensions, Exception);
132120

133121
/// <inheritdoc />
134-
public IError RemoveLocations()
135-
=> new Error(Message, Code, Path, null, Extensions, Exception);
136-
137-
/// <inheritdoc />
138-
public IError WithExtensions(IReadOnlyDictionary<string, object?> extensions)
139-
{
140-
if (extensions is null)
141-
{
142-
throw new ArgumentNullException(nameof(extensions));
143-
}
144-
145-
return new Error(Message, Code, Path, Locations, extensions, Exception);
146-
}
147-
148-
/// <inheritdoc />
149-
public IError RemoveExtensions()
150-
=> new Error(Message, Code, Path, Locations, null, Exception);
122+
public IError WithExtensions(IReadOnlyDictionary<string, object?>? extensions)
123+
=> extensions is null
124+
? new Error(Message, Code, Path, Locations, null, Exception)
125+
: new Error(Message, Code, Path, Locations, extensions, Exception);
151126

152127
/// <inheritdoc />
153128
public IError SetExtension(string key, object? value)
@@ -176,7 +151,7 @@ public IError RemoveExtension(string key)
176151
nameof(key));
177152
}
178153

179-
if (Extensions is null)
154+
if (Extensions is null || !Extensions.ContainsKey(key))
180155
{
181156
return this;
182157
}
@@ -192,10 +167,6 @@ public IError RemoveExtension(string key)
192167
/// <inheritdoc />
193168
public IError WithException(Exception? exception)
194169
=> exception is null
195-
? RemoveException()
170+
? new Error(Message, Code, Path, Locations, Extensions)
196171
: new Error(Message, Code, Path, Locations, Extensions, exception);
197-
198-
/// <inheritdoc />
199-
public IError RemoveException()
200-
=> new Error(Message, Code, Path, Locations, Extensions);
201172
}

src/HotChocolate/Core/src/Abstractions/ErrorBuilder.cs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,21 @@ public IErrorBuilder SetCode(string? code)
6161
{
6262
if (string.IsNullOrEmpty(code))
6363
{
64-
return RemoveCode();
64+
_code = null;
65+
return this;
6566
}
6667

6768
_code = code;
6869
SetExtension("code", code);
6970
return this;
7071
}
7172

72-
public IErrorBuilder RemoveCode()
73-
{
74-
_code = null;
75-
return this;
76-
}
77-
7873
public IErrorBuilder SetPath(Path? path)
7974
{
8075
if (path is null)
8176
{
82-
return RemovePath();
77+
_path = null;
78+
return this;
8379
}
8480

8581
_path = path;
@@ -89,12 +85,6 @@ public IErrorBuilder SetPath(Path? path)
8985
public IErrorBuilder SetPath(IReadOnlyList<object>? path) =>
9086
SetPath(path is null ? null : Path.FromList(path));
9187

92-
public IErrorBuilder RemovePath()
93-
{
94-
_path = null;
95-
return this;
96-
}
97-
9888
public IErrorBuilder AddLocation(Location location)
9989
{
10090
if (_dirtyLocation && _locations is not null)
@@ -194,19 +184,14 @@ public IErrorBuilder SetException(Exception? exception)
194184
{
195185
if (exception is null)
196186
{
197-
return RemoveException();
187+
_exception = null;
188+
return this;
198189
}
199190

200191
_exception = exception;
201192
return this;
202193
}
203194

204-
public IErrorBuilder RemoveException()
205-
{
206-
_exception = null;
207-
return this;
208-
}
209-
210195
public IError Build()
211196
{
212197
if (string.IsNullOrEmpty(_message))

src/HotChocolate/Core/src/Abstractions/IError.cs

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,6 @@ public interface IError
7070
/// </returns>
7171
IError WithCode(string? code);
7272

73-
/// <summary>
74-
/// Creates a new error that contains all properties of this error
75-
/// but with <see cref="Code"/> removed.
76-
/// </summary>
77-
/// <returns>
78-
/// Returns a new error that contains all properties of this error
79-
/// but with <see cref="Code"/> removed.
80-
/// </returns>
81-
IError RemoveCode();
82-
8373
/// <summary>
8474
/// Creates a new error that contains all properties of this error
8575
/// but with the specified <paramref name="path" />.
@@ -91,7 +81,7 @@ public interface IError
9181
/// Returns a new error that contains all properties of this error
9282
/// but with the specified <paramref name="path" />.
9383
/// </returns>
94-
IError WithPath(Path? path);
84+
IError WithPath(Path path);
9585

9686
/// <summary>
9787
/// Creates a new error that contains all properties of this error
@@ -104,17 +94,7 @@ public interface IError
10494
/// Returns a new error that contains all properties of this error
10595
/// but with the specified <paramref name="path" />.
10696
/// </returns>
107-
IError WithPath(IReadOnlyList<object>? path);
108-
109-
/// <summary>
110-
/// Creates a new error that contains all properties of this error
111-
/// but with the <see cref="Path"/> removed.
112-
/// </summary>
113-
/// <returns>
114-
/// Returns a new error that contains all properties of this error
115-
/// but with the <see cref="Path"/> removed.
116-
/// </returns>
117-
IError RemovePath();
97+
IError WithPath(IReadOnlyList<object> path);
11898

11999
/// <summary>
120100
/// Creates a new error that contains all properties of this error
@@ -128,17 +108,7 @@ public interface IError
128108
/// Returns a new error that contains all properties of this error
129109
/// but with the specified <paramref name="locations" />.
130110
/// </returns>
131-
IError WithLocations(IReadOnlyList<Location>? locations);
132-
133-
/// <summary>
134-
/// Creates a new error that contains all properties of this error
135-
/// but with the <see cref="Locations"/> removed.
136-
/// </summary>
137-
/// <returns>
138-
/// Returns a new error that contains all properties of this error
139-
/// but with the <see cref="Locations"/> removed.
140-
/// </returns>
141-
IError RemoveLocations();
111+
IError WithLocations(IReadOnlyList<Location> locations);
142112

143113
/// <summary>
144114
/// Creates a new error that contains all properties of this error
@@ -151,17 +121,7 @@ public interface IError
151121
/// Returns a new error that contains all properties of this error
152122
/// but with the specified <paramref name="extensions" />.
153123
/// </returns>
154-
IError WithExtensions(IReadOnlyDictionary<string, object?> extensions);
155-
156-
/// <summary>
157-
/// Creates a new error that contains all properties of this error
158-
/// but with the <see cref="Extensions"/> removed.
159-
/// </summary>
160-
/// <returns>
161-
/// Returns a new error that contains all properties of this error
162-
/// but with the <see cref="Extensions"/> removed.
163-
/// </returns>
164-
IError RemoveExtensions();
124+
IError WithExtensions(IReadOnlyDictionary<string, object?>? extensions);
165125

166126
/// <summary>
167127
/// Creates a new error that contains all properties of this error
@@ -204,14 +164,4 @@ public interface IError
204164
/// but with the specified <paramref name="exception" />.
205165
/// </returns>
206166
IError WithException(Exception? exception);
207-
208-
/// <summary>
209-
/// Creates a new error that contains all properties of this error
210-
/// but removed the exception from it.
211-
/// </summary>
212-
/// <returns>
213-
/// Returns a new error that contains all properties of this error
214-
/// but without any exception details.
215-
/// </returns>
216-
IError RemoveException();
217167
}

src/HotChocolate/Core/src/Abstractions/IErrorBuilder.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,10 @@ public interface IErrorBuilder
88

99
IErrorBuilder SetCode(string? code);
1010

11-
IErrorBuilder RemoveCode();
12-
1311
IErrorBuilder SetPath(IReadOnlyList<object>? path);
1412

1513
IErrorBuilder SetPath(Path? path);
1614

17-
IErrorBuilder RemovePath();
18-
1915
IErrorBuilder AddLocation(Location location);
2016

2117
IErrorBuilder AddLocation(int line, int column);
@@ -28,8 +24,6 @@ public interface IErrorBuilder
2824

2925
IErrorBuilder SetException(Exception? exception);
3026

31-
IErrorBuilder RemoveException();
32-
3327
IErrorBuilder SetExtension(string key, object? value);
3428

3529
IErrorBuilder RemoveExtension(string key);

src/HotChocolate/Core/test/Abstractions.Tests/ErrorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void RemoveCode()
2222
IError error = new Error("123", code: "foo");
2323

2424
// act
25-
error = error.RemoveCode();
25+
error = error.WithCode(null);
2626

2727
// assert
2828
Assert.Null(error.Code);
@@ -59,7 +59,7 @@ public void RemoveException()
5959
Assert.NotNull(error.Exception);
6060

6161
// act
62-
error = error.RemoveException();
62+
error = error.WithException(null);
6363

6464
// assert
6565
Assert.Null(error.Exception);

0 commit comments

Comments
 (0)