summaryrefslogtreecommitdiffstats
path: root/src/common-lib/exception.cpp
blob: 2421a18c4239586a0ae241a523582ccf82c63690 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (C) 2021 The Qt Company Ltd.
// Copyright (C) 2019 Luxoft Sweden AB
// Copyright (C) 2018 Pelagicore AG
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QFile>
#include <errno.h>

#include "exception.h"

QT_BEGIN_NAMESPACE_AM

Exception::Exception(const char *errorString) noexcept
    : m_errorCode(Error::System)
    , m_errorString(errorString ? QString::fromLatin1(errorString) : QString())
{ }

Exception::Exception(const QString &errorString) noexcept
    : m_errorCode(Error::System)
    , m_errorString(errorString)
{ }

Exception::Exception(Error errorCode, const char *errorString) noexcept
    : m_errorCode(errorCode)
    , m_errorString(errorString ? QString::fromLatin1(errorString) : QString())
{ }

Exception::Exception(Error errorCode, const QString &errorString) noexcept
    : m_errorCode(errorCode)
    , m_errorString(errorString)
{ }

Exception::Exception(int _errno, const char *errorString) noexcept
    : m_errorCode(_errno == EACCES ? Error::Permissions : Error::IO)
    , m_errorString(QString::fromLatin1(errorString) + u": " + QString::fromLocal8Bit(strerror(_errno)))
{ }

Exception::Exception(const QFileDevice &file, const char *errorString) noexcept
    : m_errorCode(file.error() == QFileDevice::PermissionsError ? Error::Permissions : Error::IO)
    , m_errorString(QString::fromLatin1(errorString) + u" (" + file.fileName() + u"): " + file.errorString())
{ }

Exception::Exception(const Exception &copy) noexcept
    : m_errorCode(copy.m_errorCode)
    , m_errorString(copy.m_errorString)
{ }

Exception::Exception(Exception &&move) noexcept
    : m_errorCode(move.m_errorCode)
    , m_errorString(move.m_errorString)
{
    std::swap(m_whatBuffer, move.m_whatBuffer);
}

Exception::~Exception() noexcept
{
    delete m_whatBuffer;
}

Error Exception::errorCode() const noexcept
{
    return m_errorCode;
}

QString Exception::errorString() const noexcept
{
    return m_errorString;
}

void Exception::raise() const
{
    throw *this;
}

Exception *Exception::clone() const noexcept
{
    return new Exception(*this);
}

const char *Exception::what() const noexcept
{
    if (!m_whatBuffer)
        m_whatBuffer = new QByteArray;
    *m_whatBuffer = m_errorString.toLocal8Bit();
    return m_whatBuffer->constData();
}

QT_END_NAMESPACE_AM