Skip to content

Commit

Permalink
Added an optional dictionary to the Error.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
florentmsl authored and amantinband committed Jan 1, 2024
1 parent 2b9a056 commit 928f4df
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions src/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,73 +25,90 @@ public readonly record struct Error
/// </summary>
public int NumericType { get; }

private Error(string code, string description, ErrorType type)
/// <summary>
/// Gets the dictionary.
/// </summary>
public Dictionary<string, object>? Dictionary { get; }

private Error(string code, string description, ErrorType type, Dictionary<string, object>? dictionary)
{
Code = code;
Description = description;
Type = type;
NumericType = (int)type;
Dictionary = dictionary;
}

/// <summary>
/// Creates an <see cref="Error"/> of type <see cref="ErrorType.Failure"/> from a code and description.
/// </summary>
/// <param name="code">The unique error code.</param>
/// <param name="description">The error description.</param>
/// <param name="dictionary">A dictionary which provides optional space for information.</param>
public static Error Failure(
string code = "General.Failure",
string description = "A failure has occurred.") =>
new(code, description, ErrorType.Failure);
string description = "A failure has occurred.",
Dictionary<string, object>? dictionary = null) =>
new(code, description, ErrorType.Failure);

/// <summary>
/// Creates an <see cref="Error"/> of type <see cref="ErrorType.Unexpected"/> from a code and description.
/// </summary>
/// <param name="code">The unique error code.</param>
/// <param name="description">The error description.</param>
/// <param name="dictionary">A dictionary which provides optional space for information.</param>
public static Error Unexpected(
string code = "General.Unexpected",
string description = "An unexpected error has occurred.") =>
new(code, description, ErrorType.Unexpected);
string description = "An unexpected error has occurred.",
Dictionary<string, object>? dictionary = null) =>
new(code, description, ErrorType.Unexpected);

/// <summary>
/// Creates an <see cref="Error"/> of type <see cref="ErrorType.Validation"/> from a code and description.
/// </summary>
/// <param name="code">The unique error code.</param>
/// <param name="description">The error description.</param>
/// <param name="dictionary">A dictionary which provides optional space for information.</param>
public static Error Validation(
string code = "General.Validation",
string description = "A validation error has occurred.") =>
new(code, description, ErrorType.Validation);
new(code, description, ErrorType.Validation);

/// <summary>
/// Creates an <see cref="Error"/> of type <see cref="ErrorType.Conflict"/> from a code and description.
/// </summary>
/// <param name="code">The unique error code.</param>
/// <param name="description">The error description.</param>
/// <param name="dictionary">A dictionary which provides optional space for information.</param>
public static Error Conflict(
string code = "General.Conflict",
string description = "A conflict error has occurred.") =>
new(code, description, ErrorType.Conflict);
string description = "A conflict error has occurred.",
Dictionary<string, object>? dictionary = null) =>
new(code, description, ErrorType.Conflict);

/// <summary>
/// Creates an <see cref="Error"/> of type <see cref="ErrorType.NotFound"/> from a code and description.
/// </summary>
/// <param name="code">The unique error code.</param>
/// <param name="description">The error description.</param>
/// <param name="dictionary">A dictionary which provides optional space for information.</param>
public static Error NotFound(
string code = "General.NotFound",
string description = "A 'Not Found' error has occurred.") =>
new(code, description, ErrorType.NotFound);
string description = "A 'Not Found' error has occurred.",
Dictionary<string, object>? dictionary = null) =>
new(code, description, ErrorType.NotFound);

/// <summary>
/// Creates an <see cref="Error"/> of type <see cref="ErrorType.Unauthorized"/> from a code and description.
/// </summary>
/// <param name="code">The unique error code.</param>
/// <param name="description">The error description.</param>
/// <param name="dictionary">A dictionary which provides optional space for information.</param>
public static Error Unauthorized(
string code = "General.Unauthorized",
string description = "An 'Unauthorized' error has occurred.") =>
new(code, description, ErrorType.Unauthorized);
string description = "An 'Unauthorized' error has occurred.",
Dictionary<string, object>? dictionary = null) =>
new(code, description, ErrorType.Unauthorized);

/// <summary>
/// Creates an <see cref="Error"/> with the given numeric <paramref name="type"/>,
Expand All @@ -100,9 +117,11 @@ public static Error Unauthorized(
/// <param name="type">An integer value which represents the type of error that occurred.</param>
/// <param name="code">The unique error code.</param>
/// <param name="description">The error description.</param>
/// <param name="dictionary">A dictionary which provides optional space for information.</param>
public static Error Custom(
int type,
string code,
string description) =>
new(code, description, (ErrorType)type);
string description,
Dictionary<string, object>? dictionary = null) =>
new(code, description, (ErrorType)type);
}

0 comments on commit 928f4df

Please sign in to comment.