-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: more helpful error messages for streaming connection failures
- Loading branch information
1 parent
f719d78
commit 2b379f3
Showing
6 changed files
with
110 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 39 additions & 6 deletions
45
libs/server-sent-events/include/launchdarkly/sse/error.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,44 @@ | ||
#pragma once | ||
#include <chrono> | ||
#include <optional> | ||
#include <ostream> | ||
#include <variant> | ||
|
||
#include <boost/beast/http/status.hpp> | ||
|
||
namespace launchdarkly::sse { | ||
namespace errors { | ||
|
||
struct NoContent {}; | ||
std::ostream& operator<<(std::ostream& out, NoContent const&); | ||
|
||
struct InvalidRedirectLocation { | ||
std::string location; | ||
}; | ||
std::ostream& operator<<(std::ostream& out, InvalidRedirectLocation const&); | ||
|
||
struct NotRedirectable {}; | ||
std::ostream& operator<<(std::ostream& out, NotRedirectable const&); | ||
|
||
enum class Error { | ||
NoContent = 1, | ||
InvalidRedirectLocation = 2, | ||
UnrecoverableClientError = 3, | ||
ReadTimeout = 4, | ||
struct ReadTimeout { | ||
std::optional<std::chrono::milliseconds> timeout; | ||
}; | ||
} | ||
std::ostream& operator<<(std::ostream& out, ReadTimeout const&); | ||
|
||
struct UnrecoverableClientError { | ||
boost::beast::http::status status; | ||
}; | ||
std::ostream& operator<<(std::ostream& out, UnrecoverableClientError const&); | ||
|
||
} // namespace errors | ||
|
||
using Error = std::variant<errors::NoContent, | ||
errors::InvalidRedirectLocation, | ||
errors::NotRedirectable, | ||
errors::ReadTimeout, | ||
errors::UnrecoverableClientError>; | ||
|
||
std::ostream& operator<<(std::ostream& out, Error const& error); | ||
|
||
std::string ErrorToString(Error const& error); | ||
} // namespace launchdarkly::sse |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include <launchdarkly/sse/error.hpp> | ||
|
||
#include <sstream> | ||
|
||
namespace launchdarkly::sse { | ||
namespace errors { | ||
|
||
std::ostream& operator<<(std::ostream& out, NoContent const&) { | ||
out << "no content, will not attempt to reconnect (HTTP 204)"; | ||
return out; | ||
} | ||
|
||
std::ostream& operator<<(std::ostream& out, | ||
InvalidRedirectLocation const& invalid) { | ||
out << "server responded with an invalid redirect (" << invalid.location | ||
<< ")"; | ||
return out; | ||
} | ||
|
||
std::ostream& operator<<(std::ostream& out, NotRedirectable const&) { | ||
out << "cannot follow server redirect"; | ||
return out; | ||
} | ||
|
||
std::ostream& operator<<(std::ostream& out, ReadTimeout const& err) { | ||
out << "timed out reading response body (exceeded " << err.timeout->count() | ||
<< "ms)"; | ||
return out; | ||
} | ||
|
||
std::ostream& operator<<(std::ostream& out, | ||
UnrecoverableClientError const& err) { | ||
if (err.status == boost::beast::http::status::unauthorized || | ||
err.status == boost::beast::http::status::forbidden) { | ||
out << "invalid auth key (HTTP " << static_cast<int>(err.status) << ")"; | ||
|
||
} else { | ||
out << "unrecoverable client-side error (HTTP " | ||
<< static_cast<int>(err.status) << ")"; | ||
} | ||
return out; | ||
} | ||
} // namespace errors | ||
|
||
std::ostream& operator<<(std::ostream& out, Error const& error) { | ||
std::visit( | ||
[&](auto&& arg) { | ||
using T = std::decay_t<decltype(arg)>; | ||
out << arg; | ||
}, | ||
error); | ||
return out; | ||
} | ||
|
||
std::string ErrorToString(Error const& error) { | ||
std::stringstream ss; | ||
ss << error; | ||
return ss.str(); | ||
} | ||
|
||
} // namespace launchdarkly::sse |