Music Hub  ..
A session-wide music playback service
error.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2021-2022 UBports Foundation.
3  *
4  * Contact: Alberto Mardegan <mardy@users.sourceforge.net>
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License version 3,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "error_p.h"
20 
21 #include "dbus_constants.h"
22 
23 #include <QDBusMessage>
24 #include <QDebug>
25 
26 namespace lomiri {
27 namespace MediaHub {
28 
29 Error errorFromDBus(const QDBusMessage &msg)
30 {
31  using E = Error::Code;
32 
33  Error::Code code = E::NoError;
34 
35  const QString dbusCode = msg.errorName();
36  if (dbusCode.startsWith(QStringLiteral(MPRIS_ERROR_PREFIX))) {
37  const size_t prefixLength = sizeof(MPRIS_ERROR_PREFIX) - 1;
38  const QStringRef name = dbusCode.midRef(prefixLength);
39  if (name == MPRIS_ERROR_CREATING_SESSION ||
41  /* The client perfectly knows what it was trying to do, so there's
42  * no point in sending that information back.
43  */
44  code = E::InternalError;
46  code = E::AccessDeniedError;
47  } else if (name == MPRIS_ERROR_OOP_STREAMING_NOT_SUPPORTED) {
48  code = E::OutOfProcessBufferStreamingNotSupported;
49  } else if (name == MPRIS_ERROR_URI_NOT_FOUND) {
50  code = E::ResourceError;
51  }
52  } else if (dbusCode.startsWith(QStringLiteral(FDO_ERROR_PREFIX))) {
53  const size_t prefixLength = sizeof(FDO_ERROR_PREFIX) - 1;
54  const QStringRef name = dbusCode.midRef(prefixLength);
55  if (name == FDO_ERROR_ACCESS_DENIED) {
56  code = E::AccessDeniedError;
57  } else if (name == FDO_ERROR_DISCONNECTED ||
59  name == FDO_ERROR_SERVICE_UNKNOWN) {
61  }
62  }
63 
64  if (code == E::NoError) {
65  qWarning() << "Unexpected error code" << dbusCode;
66  code = E::InternalError;
67  }
68 
69  return Error(code, msg.errorMessage());
70 }
71 
72 Error errorFromApiCode(quint16 apiCode)
73 {
74  Error::Code code;
75  QString text;
76  switch (apiCode) {
78  code = Error::NoError;
79  break;
81  code = Error::ResourceError;
82  text = "A media resource couldn't be resolved.";
83  break;
85  code = Error::FormatError;
86  text = "The media format type is not playable "
87  "due to a missing codec.";
88  break;
90  code = Error::NetworkError;
91  text = "A network error occurred.";
92  break;
95  text = "Insufficient privileges to play that media.";
96  break;
99  text = "A valid playback service was not found, "
100  "playback cannot proceed.";
101  break;
102  default:
103  qWarning() << "Unknown error code" << apiCode;
104  code = Error::InternalError;
105  text = "The backend emitted an unrecognized error code";
106  }
107 
108  return Error(code, text);
109 }
110 
111 } // namespace MediaHub
112 } // namespace lomiri
MPRIS_ERROR_URI_NOT_FOUND
#define MPRIS_ERROR_URI_NOT_FOUND
Definition: dbus_constants.h:39
DBusConstants::NetworkError
@ NetworkError
Definition: dbus_constants.h:64
FDO_ERROR_PREFIX
#define FDO_ERROR_PREFIX
Definition: dbus_constants.h:43
lomiri::MediaHub::Error::Code
Code
Definition: error.h:32
lomiri::MediaHub::Error::InternalError
@ InternalError
Definition: error.h:39
MPRIS_ERROR_DESTROYING_SESSION
#define MPRIS_ERROR_DESTROYING_SESSION
Definition: dbus_constants.h:34
MPRIS_ERROR_PREFIX
#define MPRIS_ERROR_PREFIX
Definition: dbus_constants.h:32
lomiri::MediaHub::Error::ServiceMissingError
@ ServiceMissingError
Definition: error.h:38
lomiri::MediaHub::errorFromDBus
Error errorFromDBus(const QDBusMessage &msg)
Definition: error.cpp:29
lomiri::MediaHub::Error::ResourceError
@ ResourceError
Definition: error.h:34
DBusConstants::FormatError
@ FormatError
Definition: dbus_constants.h:63
dbus_constants.h
FDO_ERROR_DISCONNECTED
#define FDO_ERROR_DISCONNECTED
Definition: dbus_constants.h:45
DBusConstants::AccessDeniedError
@ AccessDeniedError
Definition: dbus_constants.h:65
FDO_ERROR_ACCESS_DENIED
#define FDO_ERROR_ACCESS_DENIED
Definition: dbus_constants.h:44
lomiri::MediaHub::Error::NoError
@ NoError
Definition: error.h:33
FDO_ERROR_SERVICE_UNKNOWN
#define FDO_ERROR_SERVICE_UNKNOWN
Definition: dbus_constants.h:47
MPRIS_ERROR_INSUFFICIENT_APPARMOR_PERMISSIONS
#define MPRIS_ERROR_INSUFFICIENT_APPARMOR_PERMISSIONS
Definition: dbus_constants.h:35
lomiri::MediaHub::errorFromApiCode
Error errorFromApiCode(quint16 apiCode)
Definition: error.cpp:72
MPRIS_ERROR_CREATING_SESSION
#define MPRIS_ERROR_CREATING_SESSION
Definition: dbus_constants.h:33
MPRIS_ERROR_OOP_STREAMING_NOT_SUPPORTED
#define MPRIS_ERROR_OOP_STREAMING_NOT_SUPPORTED
Definition: dbus_constants.h:37
error_p.h
lomiri::MediaHub::Error::NetworkError
@ NetworkError
Definition: error.h:36
lomiri::MediaHub::Error
Definition: error.h:29
FDO_ERROR_NAME_HAS_NO_OWNER
#define FDO_ERROR_NAME_HAS_NO_OWNER
Definition: dbus_constants.h:46
lomiri::MediaHub::Error::AccessDeniedError
@ AccessDeniedError
Definition: error.h:37
lomiri
Definition: dbus_utils.h:24
DBusConstants::Error
Error
Definition: dbus_constants.h:59
DBusConstants::ServiceMissingError
@ ServiceMissingError
Definition: dbus_constants.h:66
DBusConstants::NoError
@ NoError
Definition: dbus_constants.h:61
lomiri::MediaHub::Error::FormatError
@ FormatError
Definition: error.h:35
DBusConstants::ResourceError
@ ResourceError
Definition: dbus_constants.h:62