Ubuntu Download Manager  0.6.0
A session-wide downloading service
 All Classes Namespaces Files Functions Typedefs Enumerations Enumerator
error.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2013-2014 Canonical Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of version 3 of the GNU Lesser General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the
15  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  * Boston, MA 02110-1301, USA.
17  */
18 
19 #include <QDBusError>
20 #include <ubuntu/download_manager/metatypes.h>
21 #include "error.h"
22 
23 namespace {
24  const QString DBUS_ERROR_STRING = "DBusError: %1 - %2";
25  const QString AUTH_ERROR_STRING = "AuthError: %1 - %2";
26  const QString HTTP_ERROR_STRING = "HttpError: %1 - %2";
27  const QString NETWORK_ERROR_STRING = "NetworkError: %1 - %2";
28  const QString PROCESS_ERROR_STRING = "ProcessError: %1 - %2\nExit code: %3\nStdout: %4\nStderr:%5";
29 }
30 
31 namespace Ubuntu {
32 
33 namespace DownloadManager {
34 
35 /*
36  * PRIMATE IMPLEMENTATIONS
37  */
38 
39 class ErrorPrivate {
40  Q_DECLARE_PUBLIC(Error)
41 
42  public:
43  ErrorPrivate(Error::Type type, Error* parent)
44  : _type(type),
45  q_ptr(parent) {
46  }
47 
48  Error::Type type() {
49  return _type;
50  }
51 
52  QString errorString() {
53  switch(_type) {
54  case Error::DBus:
55  return "DBusError";
56  case Error::Http:
57  return "HttpError";
58  case Error::Network:
59  return "NetworkError";
60  case Error::Process:
61  return "ProcessError";
62  default:
63  return "";
64  }
65  }
66 
67  private:
68  Error::Type _type;
69  Error* q_ptr;
70 };
71 
72 class DBusErrorPrivate {
73  Q_DECLARE_PUBLIC(DBusError)
74 
75  public:
76  DBusErrorPrivate(QDBusError err, DBusError* parent)
77  : _err(err),
78  q_ptr(parent) {
79  }
80 
81  inline QString message() {
82  return _err.message();
83  }
84 
85  inline QString name() {
86  return _err.name();
87  }
88 
89  inline QString errorString() {
90  return DBUS_ERROR_STRING.arg(_err.name(), _err.message());
91  }
92 
93  private:
94  QDBusError _err;
95  DBusError* q_ptr;
96 };
97 
98 class AuthErrorPrivate {
99  Q_DECLARE_PUBLIC(AuthError)
100 
101  public:
102  AuthErrorPrivate(Transfers::Errors::AuthErrorStruct err, AuthError* parent)
103  : _err(err),
104  q_ptr(parent) {
105  }
106 
107  inline AuthError::Type type() {
108  switch(_err.getType()) {
109  case Transfers::Errors::AuthErrorStruct::Proxy:
110  return AuthError::Proxy;
111  default:
112  return AuthError::Server;
113  }
114  }
115 
116  inline QString getTypeString() {
117  switch(_err.getType()) {
118  case Transfers::Errors::AuthErrorStruct::Proxy:
119  return "Proxy";
120  default:
121  return "Server";
122  }
123  }
124 
125  inline QString phrase() {
126  return _err.getPhrase();
127  }
128 
129  inline QString errorString() {
130  return AUTH_ERROR_STRING.arg(getTypeString(), _err.getPhrase());
131  }
132 
133  private:
134  Transfers::Errors::AuthErrorStruct _err;
135  AuthError* q_ptr;
136 };
137 
138 class HttpErrorPrivate {
139  Q_DECLARE_PUBLIC(HttpError)
140 
141  public:
142  HttpErrorPrivate(Transfers::Errors::HttpErrorStruct err, HttpError* parent)
143  : _err(err),
144  q_ptr(parent) {
145  }
146 
147  inline int code() {
148  return _err.getCode();
149  }
150 
151  inline QString phrase() {
152  return _err.getPhrase();
153  }
154 
155  inline QString errorString() {
156  return HTTP_ERROR_STRING.arg(QString::number(_err.getCode()),
157  _err.getPhrase());
158  }
159 
160  private:
161  Transfers::Errors::HttpErrorStruct _err;
162  HttpError* q_ptr;
163 };
164 
165 class NetworkErrorPrivate {
166  Q_DECLARE_PUBLIC(NetworkError)
167 
168  public:
169  NetworkErrorPrivate(Transfers::Errors::NetworkErrorStruct err,
170  NetworkError* parent)
171  : _err(err),
172  q_ptr(parent) {
173  }
174 
175  inline NetworkError::ErrorCode code() {
176  auto intCode = static_cast<NetworkError::ErrorCode>(_err.getCode());
177  return intCode;
178  }
179 
180  inline QString phrase() {
181  return _err.getPhrase();
182  }
183 
184  inline QString errorString() {
185  return NETWORK_ERROR_STRING.arg(QString::number(_err.getCode()),
186  _err.getPhrase());
187  }
188 
189  private:
190  Transfers::Errors::NetworkErrorStruct _err;
191  NetworkError* q_ptr;
192 };
193 
194 class ProcessErrorPrivate {
195  Q_DECLARE_PUBLIC(ProcessError)
196 
197  public:
198  ProcessErrorPrivate(Transfers::Errors::ProcessErrorStruct err,
199  ProcessError* parent)
200  : _err(err),
201  q_ptr(parent) {
202  }
203 
204  QProcess::ProcessError code() {
205  auto code = static_cast<QProcess::ProcessError>(_err.getCode());
206  return code;
207  }
208 
209  QString phrase() {
210  return _err.getPhrase();
211  }
212 
213  inline int exitCode() {
214  return _err.getExitCode();
215  }
216 
217  inline QString standardOut() {
218  return _err.getStandardOutput();
219  }
220 
221  inline QString standardError() {
222  return _err.getStandardError();
223  }
224 
225  inline QString errorString() {
226  return PROCESS_ERROR_STRING.arg(QString::number(_err.getCode()),
227  _err.getPhrase(), QString::number(_err.getExitCode()),
228  _err.getStandardOutput(), _err.getStandardError());
229  }
230 
231  private:
232  Transfers::Errors::ProcessErrorStruct _err;
233  ProcessError* q_ptr;
234 };
235 
236 /*
237  * PUBLIC IMPLEMENTATIONS
238  */
239 
241  : QObject(parent),
242  d_ptr(new ErrorPrivate(type, this)) {
243 }
244 
246  delete d_ptr;
247 }
248 
251  Q_D(Error);
252  return d->type();
253 }
254 
255 QString
257  Q_D(Error);
258  return d->errorString();
259 }
260 
261 DBusError::DBusError(QDBusError err, QObject* parent)
262  : Error(Error::DBus, parent),
263  d_ptr(new DBusErrorPrivate(err, this)) {
264 }
265 
266 DBusError::~DBusError() {
267  delete d_ptr;
268 }
269 
270 QString
271 DBusError::message() {
272  Q_D(DBusError);
273  return d->message();
274 }
275 
276 QString
277 DBusError::name() {
278  Q_D(DBusError);
279  return d->name();
280 }
281 
282 QString
283 DBusError::errorString() {
284  Q_D(DBusError);
285  return d->errorString();
286 }
287 
288 AuthError::AuthError(Transfers::Errors::AuthErrorStruct err, QObject* parent)
289  : Error(Error::Auth, parent),
290  d_ptr(new AuthErrorPrivate(err, this)) {
291 }
292 
294  delete d_ptr;
295 }
296 
299  Q_D(AuthError);
300  return d->type();
301 }
302 
303 QString
305  Q_D(AuthError);
306  return d->phrase();
307 }
308 
309 QString
311  Q_D(AuthError);
312  return d->errorString();
313 }
314 
315 HttpError::HttpError(Transfers::Errors::HttpErrorStruct err, QObject* parent)
316  : Error(Error::Http, parent),
317  d_ptr(new HttpErrorPrivate(err, this)) {
318 }
319 
321  delete d_ptr;
322 }
323 
324 int
326  Q_D(HttpError);
327  return d->code();
328 }
329 
330 QString
332  Q_D(HttpError);
333  return d->phrase();
334 }
335 
336 QString
338  Q_D(HttpError);
339  return d->errorString();
340 }
341 
342 NetworkError::NetworkError(Transfers::Errors::NetworkErrorStruct err,
343  QObject* parent)
344  : Error(Error::Network, parent),
345  d_ptr(new NetworkErrorPrivate(err, this)) {
346 }
347 
349  delete d_ptr;
350 }
351 
354  Q_D(NetworkError);
355  return d->code();
356 }
357 
358 QString
360  Q_D(NetworkError);
361  return d->phrase();
362 }
363 
364 QString
366  Q_D(NetworkError);
367  return d->errorString();
368 }
369 
370 ProcessError::ProcessError(Transfers::Errors::ProcessErrorStruct err,
371  QObject* parent)
372  : Error(Error::Process, parent),
373  d_ptr(new ProcessErrorPrivate(err, this)) {
374 }
375 
377  delete d_ptr;
378 }
379 
380 QString
382  Q_D(ProcessError);
383  return d->phrase();
384 }
385 
386 QProcess::ProcessError
388  Q_D(ProcessError);
389  return d->code();
390 }
391 
392 int
394  Q_D(ProcessError);
395  return d->exitCode();
396 }
397 
398 QString
400  Q_D(ProcessError);
401  return d->standardOut();
402 }
403 
404 QString
406  Q_D(ProcessError);
407  return d->standardError();
408 }
409 
410 QString
412  Q_D(ProcessError);
413  return d->errorString();
414 }
415 
416 } // DownloadManager
417 
418 } // Ubuntu
The Error class is the base class that represents an error in the download manager API...
Definition: error.h:55
QString errorString() override
Definition: error.cpp:411
QString errorString() override
Definition: error.cpp:365
QString errorString() override
Definition: error.cpp:310
QProcess::ProcessError code()
Definition: error.cpp:387
ProcessError(Transfers::Errors::ProcessErrorStruct err, QObject *parent)
Definition: error.cpp:370
NetworkError(Transfers::Errors::NetworkErrorStruct err, QObject *parent)
Definition: error.cpp:342
DBusError(QDBusError err, QObject *parent=0)
Definition: error.cpp:261
HttpError(Transfers::Errors::HttpErrorStruct err, QObject *parent)
Definition: error.cpp:315
AuthError(Transfers::Errors::AuthErrorStruct err, QObject *parent)
Definition: error.cpp:288
Error(Type type, QObject *parent=0)
Definition: error.cpp:240
The NetworkError represents an error that occurred during the download request.
Definition: error.h:310
virtual QString errorString()
Definition: error.cpp:256
The HttpError represents an error that occurred during the download request.
Definition: error.h:253
QString errorString() override
Definition: error.cpp:337
The ProcessError represents an error that occurred during the post processing of a downloaded file...
Definition: error.h:400
The AuthError represents an authentication error that occurred during the request of the download...
Definition: error.h:184