| | | 1 | | // SPDX-FileCopyrightText: 2026 Alper Çelik <[email protected]> |
| | | 2 | | // |
| | | 3 | | // SPDX-License-Identifier: AGPL-3.0-or-later |
| | | 4 | | |
| | | 5 | | namespace Api.Utils; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// An expected failure carrying the error code reported to the client. Throw it wherever the |
| | | 9 | | /// failure is detected — input rules, business conflicts, rejected uploads — and |
| | | 10 | | /// HotChocolate's mutation error middleware (see <c>[Error]</c>) reports it in the payload's |
| | | 11 | | /// <c>errors</c> list. Unexpected failures stay exceptions and remain GraphQL errors. |
| | | 12 | | /// </summary> |
| | 1 | 13 | | public sealed class DomainException(string code, string message) : Exception(message) |
| | | 14 | | { |
| | 1 | 15 | | public string Code { get; } = code; |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// The error shape reported in mutation payloads, paired with <see cref="DomainException"/> |
| | | 20 | | /// through its <c>CreateErrorFrom</c> factory. Named <c>DomainError</c> because <c>Error</c> |
| | | 21 | | /// is the interface HotChocolate adds to every error type. |
| | | 22 | | /// </summary> |
| | | 23 | | public sealed class DomainError |
| | | 24 | | { |
| | | 25 | | private DomainError(string message, string code) => (Message, Code) = (message, code); |
| | | 26 | | |
| | | 27 | | public string Message { get; } |
| | | 28 | | |
| | | 29 | | public string Code { get; } |
| | | 30 | | |
| | | 31 | | public static DomainError CreateErrorFrom(DomainException ex) => |
| | | 32 | | new(ex.Message, ex.Code); |
| | | 33 | | } |