Ubuntu Download Manager  1.2.0
A session-wide downloading service
error.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2013-2015 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  const QString HASH_ERROR_STRING = "Hash validation error using %1: Expected result is %2 but result was %3.";
30 }
31 
32 namespace Ubuntu {
33 
34 namespace DownloadManager {
35 
36 /*
37  * PRIMATE IMPLEMENTATIONS
38  */
39 
40 class ErrorPrivate {
41  Q_DECLARE_PUBLIC(Error)
42 
43  public:
44  ErrorPrivate(Error::Type type, Error* parent)
45  : _type(type),
46  q_ptr(parent) {
47  }
48 
49  Error::Type type() const {
50  return _type;
51  }
52 
53  QString errorString() const {
54  switch(_type) {
55  case Error::DBus:
56  return "DBusError";
57  case Error::Http:
58  return "HttpError";
59  case Error::Network:
60  return "NetworkError";
61  case Error::Process:
62  return "ProcessError";
63  case Error::Hash:
64  return "HashError";
65  default:
66  return "";
67  }
68  }
69 
70  private:
71  Error::Type _type;
72  Error* q_ptr;
73 };
74 
75 class DBusErrorPrivate {
76  Q_DECLARE_PUBLIC(DBusError)
77 
78  public:
79  DBusErrorPrivate(QDBusError err, DBusError* parent)
80  : _err(err),
81  q_ptr(parent) {
82  }
83 
84  inline QString message() const {
85  return _err.message();
86  }
87 
88  inline QString name() const {
89  return _err.name();
90  }
91 
92  inline QString errorString() const {
93  return DBUS_ERROR_STRING.arg(_err.name(), _err.message());
94  }
95 
96  private:
97  QDBusError _err;
98  DBusError* q_ptr;
99 };
100 
101 class AuthErrorPrivate {
102  Q_DECLARE_PUBLIC(AuthError)
103 
104  public:
105  AuthErrorPrivate(Transfers::Errors::AuthErrorStruct err, AuthError* parent)
106  : _err(err),
107  q_ptr(parent) {
108  }
109 
110  inline AuthError::Type type() const {
111  switch(_err.getType()) {
112  case Transfers::Errors::AuthErrorStruct::Proxy:
113  return AuthError::Proxy;
114  default:
115  return AuthError::Server;
116  }
117  }
118 
119  inline QString getTypeString() const {
120  switch(_err.getType()) {
121  case Transfers::Errors::AuthErrorStruct::Proxy:
122  return "Proxy";
123  default:
124  return "Server";
125  }
126  }
127 
128  inline QString phrase() const {
129  return _err.getPhrase();
130  }
131 
132  inline QString errorString() const {
133  return AUTH_ERROR_STRING.arg(getTypeString(), _err.getPhrase());
134  }
135 
136  private:
137  Transfers::Errors::AuthErrorStruct _err;
138  AuthError* q_ptr;
139 };
140 
141 class HttpErrorPrivate {
142  Q_DECLARE_PUBLIC(HttpError)
143 
144  public:
145  HttpErrorPrivate(Transfers::Errors::HttpErrorStruct err, HttpError* parent)
146  : _err(err),
147  q_ptr(parent) {
148  }
149 
150  inline int code() const {
151  return _err.getCode();
152  }
153 
154  inline QString phrase() const {
155  return _err.getPhrase();
156  }
157 
158  inline QString errorString() const {
159  return HTTP_ERROR_STRING.arg(QString::number(_err.getCode()),
160  _err.getPhrase());
161  }
162 
163  private:
164  Transfers::Errors::HttpErrorStruct _err;
165  HttpError* q_ptr;
166 };
167 
168 class NetworkErrorPrivate {
169  Q_DECLARE_PUBLIC(NetworkError)
170 
171  public:
172  NetworkErrorPrivate(Transfers::Errors::NetworkErrorStruct err,
173  NetworkError* parent)
174  : _err(err),
175  q_ptr(parent) {
176  }
177 
178  inline NetworkError::ErrorCode code() const {
179  auto intCode = static_cast<NetworkError::ErrorCode>(_err.getCode());
180  return intCode;
181  }
182 
183  inline QString phrase() const {
184  return _err.getPhrase();
185  }
186 
187  inline QString errorString() const {
188  return NETWORK_ERROR_STRING.arg(QString::number(_err.getCode()),
189  _err.getPhrase());
190  }
191 
192  private:
193  Transfers::Errors::NetworkErrorStruct _err;
194  NetworkError* q_ptr;
195 };
196 
197 class ProcessErrorPrivate {
198  Q_DECLARE_PUBLIC(ProcessError)
199 
200  public:
201  ProcessErrorPrivate(Transfers::Errors::ProcessErrorStruct err,
202  ProcessError* parent)
203  : _err(err),
204  q_ptr(parent) {
205  }
206 
207  QProcess::ProcessError code() const {
208  auto code = static_cast<QProcess::ProcessError>(_err.getCode());
209  return code;
210  }
211 
212  QString phrase() const {
213  return _err.getPhrase();
214  }
215 
216  inline int exitCode() const {
217  return _err.getExitCode();
218  }
219 
220  inline QString standardOut() const {
221  return _err.getStandardOutput();
222  }
223 
224  inline QString standardError() const {
225  return _err.getStandardError();
226  }
227 
228  inline QString errorString() const {
229  return PROCESS_ERROR_STRING.arg(QString::number(_err.getCode()),
230  _err.getPhrase(), QString::number(_err.getExitCode()),
231  _err.getStandardOutput(), _err.getStandardError());
232  }
233 
234  private:
235  Transfers::Errors::ProcessErrorStruct _err;
236  ProcessError* q_ptr;
237 };
238 
239 class HashErrorPrivate {
240  Q_DECLARE_PUBLIC(HashError)
241 
242  public:
243  HashErrorPrivate (Transfers::Errors::HashErrorStruct err, HashError* parent)
244  : _err(err),
245  q_ptr(parent) {
246  }
247 
248  inline QString method() const {
249  return _err.getMethod();
250  }
251 
252  inline QString expected() const {
253  return _err.getExpected();
254  }
255 
256  inline QString checksum() const {
257  return _err.getChecksum();
258  }
259 
260  inline QString errorString() const {
261  return HASH_ERROR_STRING.arg(_err.getMethod()).arg(_err.getExpected()).arg(_err.getChecksum());
262  }
263 
264  private:
265  Transfers::Errors::HashErrorStruct _err;
266  HashError* q_ptr;
267 };
268 
269 /*
270  * PUBLIC IMPLEMENTATIONS
271  */
272 
274  : QObject(parent),
275  d_ptr(new ErrorPrivate(type, this)) {
276 }
277 
279  delete d_ptr;
280 }
281 
284  Q_D(Error);
285  return d->type();
286 }
287 
288 QString
290  Q_D(Error);
291  return d->errorString();
292 }
293 
294 DBusError::DBusError(QDBusError err, QObject* parent)
295  : Error(Error::DBus, parent),
296  d_ptr(new DBusErrorPrivate(err, this)) {
297 }
298 
299 DBusError::~DBusError() {
300  delete d_ptr;
301 }
302 
303 QString
304 DBusError::message() {
305  Q_D(DBusError);
306  return d->message();
307 }
308 
309 QString
310 DBusError::name() {
311  Q_D(DBusError);
312  return d->name();
313 }
314 
315 QString
316 DBusError::errorString() {
317  Q_D(DBusError);
318  return d->errorString();
319 }
320 
321 AuthError::AuthError(Transfers::Errors::AuthErrorStruct err, QObject* parent)
322  : Error(Error::Auth, parent),
323  d_ptr(new AuthErrorPrivate(err, this)) {
324 }
325 
327  delete d_ptr;
328 }
329 
332  Q_D(AuthError);
333  return d->type();
334 }
335 
336 QString
338  Q_D(AuthError);
339  return d->phrase();
340 }
341 
342 QString
344  Q_D(AuthError);
345  return d->errorString();
346 }
347 
348 HttpError::HttpError(Transfers::Errors::HttpErrorStruct err, QObject* parent)
349  : Error(Error::Http, parent),
350  d_ptr(new HttpErrorPrivate(err, this)) {
351 }
352 
354  delete d_ptr;
355 }
356 
357 int
359  Q_D(HttpError);
360  return d->code();
361 }
362 
363 QString
365  Q_D(HttpError);
366  return d->phrase();
367 }
368 
369 QString
371  Q_D(HttpError);
372  return d->errorString();
373 }
374 
375 NetworkError::NetworkError(Transfers::Errors::NetworkErrorStruct err,
376  QObject* parent)
377  : Error(Error::Network, parent),
378  d_ptr(new NetworkErrorPrivate(err, this)) {
379 }
380 
382  delete d_ptr;
383 }
384 
387  Q_D(NetworkError);
388  return d->code();
389 }
390 
391 QString
393  Q_D(NetworkError);
394  return d->phrase();
395 }
396 
397 QString
399  Q_D(NetworkError);
400  return d->errorString();
401 }
402 
403 ProcessError::ProcessError(Transfers::Errors::ProcessErrorStruct err,
404  QObject* parent)
405  : Error(Error::Process, parent),
406  d_ptr(new ProcessErrorPrivate(err, this)) {
407 }
408 
410  delete d_ptr;
411 }
412 
413 QString
415  Q_D(ProcessError);
416  return d->phrase();
417 }
418 
419 QProcess::ProcessError
421  Q_D(ProcessError);
422  return d->code();
423 }
424 
425 int
427  Q_D(ProcessError);
428  return d->exitCode();
429 }
430 
431 QString
433  Q_D(ProcessError);
434  return d->standardOut();
435 }
436 
437 QString
439  Q_D(ProcessError);
440  return d->standardError();
441 }
442 
443 QString
445  Q_D(ProcessError);
446  return d->errorString();
447 }
448 
449 HashError::HashError(Transfers::Errors::HashErrorStruct errStruct, QObject* parent)
450  : Error(Error::Hash, parent),
451  d_ptr(new HashErrorPrivate(errStruct, this)) {
452 }
453 
455  delete d_ptr;
456 }
457 
458 QString
460  Q_D(HashError);
461  return d->method();
462 }
463 
464 QString
466  Q_D(HashError);
467  return d->expected();
468 }
469 
470 QString
472  Q_D(HashError);
473  return d->checksum();
474 }
475 
476 QString
478  Q_D(HashError);
479  return d->errorString();
480 }
481 
482 } // DownloadManager
483 
484 } // 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:444
QString errorString() override
Definition: error.cpp:398
QString errorString() override
Definition: error.cpp:343
QProcess::ProcessError code()
Definition: error.cpp:420
ProcessError(Transfers::Errors::ProcessErrorStruct err, QObject *parent)
Definition: error.cpp:403
NetworkError(Transfers::Errors::NetworkErrorStruct err, QObject *parent)
Definition: error.cpp:375
The HashError represents an error that occurred during the hash validation after the download...
Definition: error.h:480
HashError(Transfers::Errors::HashErrorStruct err, QObject *parent)
Definition: error.cpp:449
DBusError(QDBusError err, QObject *parent=0)
Definition: error.cpp:294
HttpError(Transfers::Errors::HttpErrorStruct err, QObject *parent)
Definition: error.cpp:348
AuthError(Transfers::Errors::AuthErrorStruct err, QObject *parent)
Definition: error.cpp:321
Error(Type type, QObject *parent=0)
Definition: error.cpp:273
The NetworkError represents an error that occurred during the download request.
Definition: error.h:311
virtual QString errorString()
Definition: error.cpp:289
The HttpError represents an error that occurred during the download request.
Definition: error.h:254
QString errorString() override
Definition: error.cpp:370
The ProcessError represents an error that occurred during the post processing of a downloaded file...
Definition: error.h:401
QString errorString() override
Definition: error.cpp:477
The AuthError represents an authentication error that occurred during the request of the download...
Definition: error.h:185