LeechCraft  0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
networkresult.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #include "networkresult.h"
10 #include <util/sll/either.h>
11 #include <util/sll/visitor.h>
12 
13 namespace LC::Util
14 {
15  QDebug operator<< (QDebug dbg, const NetworkReplyError& error)
16  {
17  QDebugStateSaver saver { dbg };
18 
19  dbg.nospace () << "{ url: " << error.Url_
20  << "; error: " << error.Error_
21  << "; text: " << error.ErrorText_
22  << " }";
23 
24  return dbg;
25  }
26 
28  : std::runtime_error { "network reply returned an error: " + error.ErrorText_.toStdString () }
29  , Error_ { std::move (error) }
30  {
31  }
32 
34  {
35  return Error_;
36  }
37 
38  QDebug operator<< (QDebug dbg, const NetworkReplyErrorException& exception)
39  {
40  return dbg << exception.GetError ();
41  }
42 
43  std::optional<NetworkReplyError> NetworkResult::IsError () const
44  {
45  if (const auto errPtr = std::get_if<NetworkReplyError> (this))
46  return *errPtr;
47  return {};
48  }
49 
50  QByteArray NetworkResult::GetReplyData () const
51  {
52  return Visit (*this,
53  [] (const NetworkReplySuccess& success) { return success.Data_; },
54  [] (const NetworkReplyError& error) -> QByteArray { throw NetworkReplyErrorException { error }; });
55  }
56 
57  Either<QString, QByteArray> NetworkResult::ToEither (const QString& errorContext) const
58  {
59  using Result_t = Either<QString, QByteArray>;
60  return Visit (*this,
61  [] (const NetworkReplySuccess& success) { return Result_t { success.Data_ }; },
62  [&errorContext] (const NetworkReplyError& error)
63  {
64  qWarning () << errorContext << error;
65  return Result_t { errorContext + ' ' + error.ErrorText_ };
66  });
67  }
68 
69  QDebug operator<< (QDebug dbg, const NetworkResult& result)
70  {
71  QDebugStateSaver saver { dbg };
72 
73  dbg.noquote ();
74  Visit (result,
75  [&dbg] (const NetworkReplySuccess& success) { dbg << "success:" << success.Data_; },
76  [&dbg] (const NetworkReplyError& error) { dbg << "error:" << error; });
77 
78  return dbg;
79  }
80 }
NetworkReplyErrorException(NetworkReplyError error)
STL namespace.
auto Visit(const Either< Left, Right > &either, Args &&... args)
Definition: either.h:215
QNetworkReply::NetworkError Error_
Definition: networkresult.h:19
Either< QString, QByteArray > ToEither(const QString &errorContext) const
const NetworkReplyError & GetError() const
std::optional< NetworkReplyError > IsError() const
QByteArray GetReplyData() const
QDebug operator<<(QDebug dbg, const CtString< N, Char > &str)
Definition: ctstringutils.h:86