LeechCraft 0.6.70-17335-ge406ffdcaf
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/visitor.h>
12
13namespace 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 std::source_location& loc) const
58 {
59 using Result_t = Either<QString, QByteArray>;
60 return Visit (*this,
61 [] (const NetworkReplySuccess& success) { return Result_t { success.Data_ }; },
62 [&loc] (const NetworkReplyError& error)
63 {
64 QMessageLogger { loc.file_name (), static_cast<int> (loc.line ()), loc.function_name () }.warning () << error;
65 return Result_t { error.ErrorText_ };
66 });
67 }
68
69 Either<QString, QByteArray> NetworkResult::ToEither (const QString& errorContext) const
70 {
71 using Result_t = Either<QString, QByteArray>;
72 return Visit (*this,
73 [] (const NetworkReplySuccess& success) { return Result_t { success.Data_ }; },
74 [&errorContext] (const NetworkReplyError& error)
75 {
76 qWarning () << errorContext << error;
77 return Result_t { error.ErrorText_ };
78 });
79 }
80
81 QDebug operator<< (QDebug dbg, const NetworkResult& result)
82 {
83 QDebugStateSaver saver { dbg };
84
85 dbg.noquote ();
86 Visit (result,
87 [&dbg] (const NetworkReplySuccess& success) { dbg << "success:" << success.Data_; },
88 [&dbg] (const NetworkReplyError& error) { dbg << "error:" << error; });
89
90 return dbg;
91 }
92}
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
auto Visit(const Either< Left, Right > &either, Args &&... args)
Definition either.h:204
QDebug operator<<(QDebug dbg, const CtString< N, Char > &str)
QNetworkReply::NetworkError Error_