Throw an exception when response is not 200 OK
Not sure this is the best way to do this, but at least I'm not just ignoring errors now.
This commit is contained in:
parent
c98083837d
commit
9dffbf811c
5 changed files with 91 additions and 0 deletions
|
@ -122,6 +122,7 @@ executable(meson.project_name(),
|
||||||
'timer_oro_api.cpp',
|
'timer_oro_api.cpp',
|
||||||
'oro/originsdb.cpp',
|
'oro/originsdb.cpp',
|
||||||
'oro/api.cpp',
|
'oro/api.cpp',
|
||||||
|
'oro/api_nap_exception.cpp',
|
||||||
oro_rest_sources,
|
oro_rest_sources,
|
||||||
project_config_file,
|
project_config_file,
|
||||||
install: true,
|
install: true,
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
#include "private/v1_endpoints.hpp"
|
#include "private/v1_endpoints.hpp"
|
||||||
#include "private/dateconv.hpp"
|
#include "private/dateconv.hpp"
|
||||||
#include "duckhandy/int_conv.hpp"
|
#include "duckhandy/int_conv.hpp"
|
||||||
|
#include "api_nap_exception.hpp"
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
namespace sjd = simdjson::dom;
|
namespace sjd = simdjson::dom;
|
||||||
|
@ -223,6 +224,9 @@ template <typename T, typename F>
|
||||||
std::pair<Header, T> ApiNap::fetch_and_parse (const char* endpoint, F&& data_fill) {
|
std::pair<Header, T> ApiNap::fetch_and_parse (const char* endpoint, F&& data_fill) {
|
||||||
auto resp = m_qrest.fetch(m_prefix + endpoint);
|
auto resp = m_qrest.fetch(m_prefix + endpoint);
|
||||||
|
|
||||||
|
if (200 != resp.code)
|
||||||
|
throw ServerError(resp);
|
||||||
|
|
||||||
T dataret;
|
T dataret;
|
||||||
{
|
{
|
||||||
std::unique_lock lock(m_json_mutex);
|
std::unique_lock lock(m_json_mutex);
|
||||||
|
|
38
src/oro/api_nap_exception.cpp
Normal file
38
src/oro/api_nap_exception.cpp
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
/* Copyright 2020, Michele Santullo
|
||||||
|
* This file is part of orotool.
|
||||||
|
*
|
||||||
|
* Orotool is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Orotool is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Orotool. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "api_nap_exception.hpp"
|
||||||
|
#include "nap/http_response.hpp"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace oro {
|
||||||
|
|
||||||
|
ServerError::ServerError (const nap::HttpResponse& resp) :
|
||||||
|
std::runtime_error(
|
||||||
|
std::string("Server replied: ") + std::to_string(resp.code) + " (" +
|
||||||
|
std::string(resp.http_ver) + ", \"" + std::string(resp.code_desc) +
|
||||||
|
"\")"
|
||||||
|
),
|
||||||
|
m_err_code(static_cast<int>(resp.code))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
int ServerError::error_code() const noexcept {
|
||||||
|
return m_err_code;
|
||||||
|
}
|
||||||
|
|
||||||
|
} //namespace oro
|
37
src/oro/api_nap_exception.hpp
Normal file
37
src/oro/api_nap_exception.hpp
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
/* Copyright 2020, Michele Santullo
|
||||||
|
* This file is part of orotool.
|
||||||
|
*
|
||||||
|
* Orotool is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Orotool is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Orotool. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
namespace nap {
|
||||||
|
struct HttpResponse;
|
||||||
|
} //namespace nap
|
||||||
|
|
||||||
|
namespace oro {
|
||||||
|
|
||||||
|
class ServerError : public std::runtime_error {
|
||||||
|
public:
|
||||||
|
explicit ServerError (const nap::HttpResponse& resp);
|
||||||
|
int error_code() const noexcept;
|
||||||
|
|
||||||
|
private:
|
||||||
|
int m_err_code;
|
||||||
|
};
|
||||||
|
|
||||||
|
} //namespace oro
|
|
@ -20,6 +20,8 @@
|
||||||
#include "oro/api.hpp"
|
#include "oro/api.hpp"
|
||||||
#if defined(OROTOOL_WITH_RESTCCPP)
|
#if defined(OROTOOL_WITH_RESTCCPP)
|
||||||
# include <restc-cpp/error.h>
|
# include <restc-cpp/error.h>
|
||||||
|
#elif defined(OROTOOL_WITH_NAP)
|
||||||
|
# include "oro/api_nap_exception.hpp"
|
||||||
#endif
|
#endif
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
|
||||||
|
@ -79,6 +81,15 @@ inline void TimerOroApi<Op>::fetch_data() {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#elif defined(OROTOOL_WITH_NAP)
|
||||||
|
catch (const oro::ServerError& err) {
|
||||||
|
status_code = err.error_code();
|
||||||
|
if (429 == status_code) {
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
catch (...) {
|
catch (...) {
|
||||||
this->set_exception(std::current_exception());
|
this->set_exception(std::current_exception());
|
||||||
|
|
Loading…
Reference in a new issue