diff --git a/src/meson.build b/src/meson.build index 36e4c5f..4aa844a 100644 --- a/src/meson.build +++ b/src/meson.build @@ -122,6 +122,7 @@ executable(meson.project_name(), 'timer_oro_api.cpp', 'oro/originsdb.cpp', 'oro/api.cpp', + 'oro/api_nap_exception.cpp', oro_rest_sources, project_config_file, install: true, diff --git a/src/oro/api_nap.cpp b/src/oro/api_nap.cpp index 6a740c1..62d5564 100644 --- a/src/oro/api_nap.cpp +++ b/src/oro/api_nap.cpp @@ -19,6 +19,7 @@ #include "private/v1_endpoints.hpp" #include "private/dateconv.hpp" #include "duckhandy/int_conv.hpp" +#include "api_nap_exception.hpp" #include namespace sjd = simdjson::dom; @@ -223,6 +224,9 @@ template std::pair ApiNap::fetch_and_parse (const char* endpoint, F&& data_fill) { auto resp = m_qrest.fetch(m_prefix + endpoint); + if (200 != resp.code) + throw ServerError(resp); + T dataret; { std::unique_lock lock(m_json_mutex); diff --git a/src/oro/api_nap_exception.cpp b/src/oro/api_nap_exception.cpp new file mode 100644 index 0000000..39968c7 --- /dev/null +++ b/src/oro/api_nap_exception.cpp @@ -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 . + */ + +#include "api_nap_exception.hpp" +#include "nap/http_response.hpp" +#include + +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(resp.code)) +{ +} + +int ServerError::error_code() const noexcept { + return m_err_code; +} + +} //namespace oro diff --git a/src/oro/api_nap_exception.hpp b/src/oro/api_nap_exception.hpp new file mode 100644 index 0000000..230bd99 --- /dev/null +++ b/src/oro/api_nap_exception.hpp @@ -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 . + */ + +#pragma once + +#include + +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 diff --git a/src/timer_oro_api.cpp b/src/timer_oro_api.cpp index 22a47f2..8e8c6ec 100644 --- a/src/timer_oro_api.cpp +++ b/src/timer_oro_api.cpp @@ -20,6 +20,8 @@ #include "oro/api.hpp" #if defined(OROTOOL_WITH_RESTCCPP) # include +#elif defined(OROTOOL_WITH_NAP) +# include "oro/api_nap_exception.hpp" #endif #include @@ -79,6 +81,15 @@ inline void TimerOroApi::fetch_data() { 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 catch (...) { this->set_exception(std::current_exception());