LeechCraft 0.6.70-19214-g121e5318a7
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
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/logging.h>
12#include <util/sll/visitor.h>
13
14namespace LC::Util
15{
16 QDebug operator<< (QDebug dbg, const NetworkReplyError& error)
17 {
18 QDebugStateSaver saver { dbg };
19
20 dbg.nospace () << "{ url: " << error.Url_
21 << "; error: " << error.Error_
22 << "; text: " << error.ErrorText_
23 << " }";
24
25 return dbg;
26 }
27
29 : std::runtime_error { "network reply returned an error: " + error.ErrorText_.toStdString () }
30 , Error_ { std::move (error) }
31 {
32 }
33
35 {
36 return Error_;
37 }
38
39 QDebug operator<< (QDebug dbg, const NetworkReplyErrorException& exception)
40 {
41 return dbg << exception.GetError ();
42 }
43
44 std::optional<NetworkReplyError> NetworkResult::IsError () const
45 {
46 if (const auto errPtr = std::get_if<NetworkReplyError> (this))
47 return *errPtr;
48 return {};
49 }
50
51 QByteArray NetworkResult::GetReplyData () const
52 {
53 return Visit (*this,
54 [] (const NetworkReplySuccess& success) { return success.Data_; },
55 [] (const NetworkReplyError& error) -> QByteArray { throw NetworkReplyErrorException { error }; });
56 }
57
58 Either<QString, QByteArray> NetworkResult::ToEither (const std::source_location& loc) const
59 {
60 using Result_t = Either<QString, QByteArray>;
61 return Visit (*this,
62 [] (const NetworkReplySuccess& success) { return Result_t { success.Data_ }; },
63 [&loc] (const NetworkReplyError& error)
64 {
65 LogAt (loc).warning () << error;
66 return Result_t { error.ErrorText_ };
67 });
68 }
69
70 Either<QString, QByteArray> NetworkResult::ToEither (const QString& errorContext) const
71 {
72 using Result_t = Either<QString, QByteArray>;
73 return Visit (*this,
74 [] (const NetworkReplySuccess& success) { return Result_t { success.Data_ }; },
75 [&errorContext] (const NetworkReplyError& error)
76 {
77 qWarning () << errorContext << error;
78 return Result_t { error.ErrorText_ };
79 });
80 }
81
82 QDebug operator<< (QDebug dbg, const NetworkResult& result)
83 {
84 QDebugStateSaver saver { dbg };
85
86 dbg.noquote ();
87 Visit (result,
88 [&dbg] (const NetworkReplySuccess& success) { dbg << "success:" << success.Data_; },
89 [&dbg] (const NetworkReplyError& error) { dbg << "error:" << error; });
90
91 return dbg;
92 }
93}
const NetworkReplyError & GetError() const
NetworkReplyErrorException(NetworkReplyError error)
std::optional< NetworkReplyError > IsError() const
Either< QString, QByteArray > ToEither(const std::source_location &=std::source_location::current()) const
QByteArray GetReplyData() const
QMessageLogger LogAt(std::source_location loc)
Definition logging.cpp:14
auto Visit(const Either< Left, Right > &either, Args &&... args)
Definition either.h:189
QDebug operator<<(QDebug dbg, const CtString< N, Char > &str)
QNetworkReply::NetworkError Error_