Created
July 30, 2025 13:45
-
-
Save cybergitt/3522037e74ee29a28af054c10c670ec5 to your computer and use it in GitHub Desktop.
Error Model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Microsoft.AspNetCore.Http; | |
| namespace BAS.Application.Common.Errors | |
| { | |
| public class Error(string code, string message, int? status) : IEquatable<Error> | |
| { | |
| public static readonly Error None = new(string.Empty, string.Empty, null); | |
| public static readonly Error NullValue = new("Error.NullValue", "The specified result value is null.", StatusCodes.Status204NoContent); | |
| public string Code { get; } = code; | |
| public string Message { get; } = message; | |
| public int? Status { get; } = status ?? StatusCodes.Status500InternalServerError; | |
| public static Error Combine(params Error[] errors) | |
| { | |
| var combinedMessage = string.Join("; ", errors.Select(e => e.Message)); | |
| return new Error("Multiple.Errors", combinedMessage, StatusCodes.Status207MultiStatus); | |
| } | |
| public static implicit operator string(Error error) => error.Code; | |
| public static bool operator ==(Error? a, Error? b) => a?.Equals(b) ?? b is null; | |
| public static bool operator !=(Error? a, Error? b) => !Equals(a, b); | |
| public virtual bool Equals(Error? other) => | |
| other is not null && Code == other.Code && Message == other.Message; | |
| public override bool Equals(object? obj) => obj is Error error && Equals(error); | |
| public override int GetHashCode() => HashCode.Combine(Code, Message); | |
| public override string ToString() => Code; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment