aboutsummaryrefslogtreecommitdiffstats
path: root/examples/ColorPalette/AbstractResource.cs
blob: 5b690741cda2a8c7357c3321a09274e1a4bbfb2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/***************************************************************************************************
 Copyright (C) 2025 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/

using System.Runtime.CompilerServices;
using System.Diagnostics;
using Refit;

namespace ColorPalette
{
    public abstract class AbstractResource
    {
        internal RestService Service { get; set; }

        internal abstract void ResetConnection();

        protected void ErrorMsg(string message, [CallerFilePath] string source = "")
        {
            Debug.WriteLine($"[{Path.GetFileNameWithoutExtension(source)}] {message}");
        }

        protected void HandleRequestException(
            Exception e, Action<ApiException> fnApiEx, Action<Exception> fnEx = null,
            [CallerFilePath] string source = "")
        {
            var q = new Queue<Exception>();
            q.Enqueue(e);
            while (q.TryDequeue(out var ex)) {
                switch (ex) {
                    case ApiException apiEx:
                        fnApiEx?.Invoke(apiEx);
                        break;
                    case AggregateException aggEx:
                        foreach (var inEx in aggEx.InnerExceptions)
                            q.Enqueue(inEx);
                        break;
                    default:
                        if (fnEx != null)
                            fnEx.Invoke(ex);
                        else
                            ErrorMsg($"Request error: {ex.Message}", source);
                        break;
                }
            }
        }
    }
}