Skip to content

Commit

Permalink
Remove obsolete methods: RemovePath and RemoveLocations (#7860)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Staib <[email protected]>
  • Loading branch information
JesseXia and michaelstaib authored Feb 4, 2025
1 parent df2f85d commit b8fcc1c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 146 deletions.
79 changes: 25 additions & 54 deletions src/HotChocolate/Core/src/Abstractions/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,70 +84,45 @@ public IError WithMessage(string message)
/// <inheritdoc />
public IError WithCode(string? code)
{
IReadOnlyDictionary<string, object?>? extensions;

if (string.IsNullOrEmpty(code))
{
return RemoveCode();
extensions = Extensions;

if (Extensions?.ContainsKey(_code) == true)
{
var temp = new OrderedDictionary<string, object?>(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<string, object?> { [_code] = code, }
: new OrderedDictionary<string, object?>(Extensions) { [_code] = code, };
return new Error(Message, code, Path, Locations, extensions, Exception);
}

/// <inheritdoc />
public IError RemoveCode()
{
var extensions = Extensions;

if (Extensions is { })
{
var temp = new OrderedDictionary<string, object?>(Extensions);
temp.Remove(_code);
extensions = temp;
}

return new Error(Message, null, Path, Locations, extensions, Exception);
}

/// <inheritdoc />
public IError WithPath(Path? path)
=> path is null
? RemovePath()
: new Error(Message, Code, path, Locations, Extensions, Exception);

/// <inheritdoc />
public IError WithPath(IReadOnlyList<object>? path)
=> WithPath(path is null ? null : Path.FromList(path));
public IError WithPath(Path path)
=> new Error(Message, Code, path, Locations, Extensions, Exception);

/// <inheritdoc />
public IError RemovePath()
=> new Error(Message, Code, null, Locations, Extensions, Exception);
public IError WithPath(IReadOnlyList<object> path)
=> WithPath(Path.FromList(path));

/// <inheritdoc />
public IError WithLocations(IReadOnlyList<Location>? locations)
=> locations is null
? RemoveLocations()
: new Error(Message, Code, Path, locations, Extensions, Exception);
public IError WithLocations(IReadOnlyList<Location> locations)
=> new Error(Message, Code, Path, locations, Extensions, Exception);

/// <inheritdoc />
public IError RemoveLocations()
=> new Error(Message, Code, Path, null, Extensions, Exception);

/// <inheritdoc />
public IError WithExtensions(IReadOnlyDictionary<string, object?> extensions)
{
if (extensions is null)
{
throw new ArgumentNullException(nameof(extensions));
}

return new Error(Message, Code, Path, Locations, extensions, Exception);
}

/// <inheritdoc />
public IError RemoveExtensions()
=> new Error(Message, Code, Path, Locations, null, Exception);
public IError WithExtensions(IReadOnlyDictionary<string, object?>? extensions)
=> extensions is null
? new Error(Message, Code, Path, Locations, null, Exception)
: new Error(Message, Code, Path, Locations, extensions, Exception);

/// <inheritdoc />
public IError SetExtension(string key, object? value)
Expand Down Expand Up @@ -176,7 +151,7 @@ public IError RemoveExtension(string key)
nameof(key));
}

if (Extensions is null)
if (Extensions is null || !Extensions.ContainsKey(key))
{
return this;
}
Expand All @@ -192,10 +167,6 @@ public IError RemoveExtension(string key)
/// <inheritdoc />
public IError WithException(Exception? exception)
=> exception is null
? RemoveException()
? new Error(Message, Code, Path, Locations, Extensions)
: new Error(Message, Code, Path, Locations, Extensions, exception);

/// <inheritdoc />
public IError RemoveException()
=> new Error(Message, Code, Path, Locations, Extensions);
}
27 changes: 6 additions & 21 deletions src/HotChocolate/Core/src/Abstractions/ErrorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,21 @@ public IErrorBuilder SetCode(string? code)
{
if (string.IsNullOrEmpty(code))
{
return RemoveCode();
_code = null;
return this;
}

_code = code;
SetExtension("code", 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;
Expand All @@ -89,12 +85,6 @@ public IErrorBuilder SetPath(Path? path)
public IErrorBuilder SetPath(IReadOnlyList<object>? 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)
Expand Down Expand Up @@ -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))
Expand Down
58 changes: 4 additions & 54 deletions src/HotChocolate/Core/src/Abstractions/IError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,6 @@ public interface IError
/// </returns>
IError WithCode(string? code);

/// <summary>
/// Creates a new error that contains all properties of this error
/// but with <see cref="Code"/> removed.
/// </summary>
/// <returns>
/// Returns a new error that contains all properties of this error
/// but with <see cref="Code"/> removed.
/// </returns>
IError RemoveCode();

/// <summary>
/// Creates a new error that contains all properties of this error
/// but with the specified <paramref name="path" />.
Expand All @@ -91,7 +81,7 @@ public interface IError
/// Returns a new error that contains all properties of this error
/// but with the specified <paramref name="path" />.
/// </returns>
IError WithPath(Path? path);
IError WithPath(Path path);

/// <summary>
/// Creates a new error that contains all properties of this error
Expand All @@ -104,17 +94,7 @@ public interface IError
/// Returns a new error that contains all properties of this error
/// but with the specified <paramref name="path" />.
/// </returns>
IError WithPath(IReadOnlyList<object>? path);

/// <summary>
/// Creates a new error that contains all properties of this error
/// but with the <see cref="Path"/> removed.
/// </summary>
/// <returns>
/// Returns a new error that contains all properties of this error
/// but with the <see cref="Path"/> removed.
/// </returns>
IError RemovePath();
IError WithPath(IReadOnlyList<object> path);

/// <summary>
/// Creates a new error that contains all properties of this error
Expand All @@ -128,17 +108,7 @@ public interface IError
/// Returns a new error that contains all properties of this error
/// but with the specified <paramref name="locations" />.
/// </returns>
IError WithLocations(IReadOnlyList<Location>? locations);

/// <summary>
/// Creates a new error that contains all properties of this error
/// but with the <see cref="Locations"/> removed.
/// </summary>
/// <returns>
/// Returns a new error that contains all properties of this error
/// but with the <see cref="Locations"/> removed.
/// </returns>
IError RemoveLocations();
IError WithLocations(IReadOnlyList<Location> locations);

/// <summary>
/// Creates a new error that contains all properties of this error
Expand All @@ -151,17 +121,7 @@ public interface IError
/// Returns a new error that contains all properties of this error
/// but with the specified <paramref name="extensions" />.
/// </returns>
IError WithExtensions(IReadOnlyDictionary<string, object?> extensions);

/// <summary>
/// Creates a new error that contains all properties of this error
/// but with the <see cref="Extensions"/> removed.
/// </summary>
/// <returns>
/// Returns a new error that contains all properties of this error
/// but with the <see cref="Extensions"/> removed.
/// </returns>
IError RemoveExtensions();
IError WithExtensions(IReadOnlyDictionary<string, object?>? extensions);

/// <summary>
/// Creates a new error that contains all properties of this error
Expand Down Expand Up @@ -204,14 +164,4 @@ public interface IError
/// but with the specified <paramref name="exception" />.
/// </returns>
IError WithException(Exception? exception);

/// <summary>
/// Creates a new error that contains all properties of this error
/// but removed the exception from it.
/// </summary>
/// <returns>
/// Returns a new error that contains all properties of this error
/// but without any exception details.
/// </returns>
IError RemoveException();
}
6 changes: 0 additions & 6 deletions src/HotChocolate/Core/src/Abstractions/IErrorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@ public interface IErrorBuilder

IErrorBuilder SetCode(string? code);

IErrorBuilder RemoveCode();

IErrorBuilder SetPath(IReadOnlyList<object>? path);

IErrorBuilder SetPath(Path? path);

IErrorBuilder RemovePath();

IErrorBuilder AddLocation(Location location);

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

IErrorBuilder SetException(Exception? exception);

IErrorBuilder RemoveException();

IErrorBuilder SetExtension(string key, object? value);

IErrorBuilder RemoveExtension(string key);
Expand Down
4 changes: 2 additions & 2 deletions src/HotChocolate/Core/test/Abstractions.Tests/ErrorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -59,7 +59,7 @@ public void RemoveException()
Assert.NotNull(error.Exception);

// act
error = error.RemoveException();
error = error.WithException(null);

// assert
Assert.Null(error.Exception);
Expand Down
Loading

0 comments on commit b8fcc1c

Please sign in to comment.