Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
exception.hh
1/*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18/*
19 * Copyright 2015 Cloudius Systems
20 */
21
22#pragma once
23
24#include <seastar/util/log.hh>
25#include <seastar/util/modules.hh>
26#include <seastar/http/reply.hh>
27#include <seastar/json/json_elements.hh>
28
29namespace seastar {
30
31namespace httpd {
32
33SEASTAR_MODULE_EXPORT_BEGIN
34
40class base_exception : public std::exception {
41public:
42 base_exception(const std::string& msg, http::reply::status_type status)
43 : _msg(msg), _status(status) {
44 }
45
46 virtual const char* what() const noexcept {
47 return _msg.c_str();
48 }
49
50 http::reply::status_type status() const {
51 return _status;
52 }
53
54 virtual const std::string& str() const {
55 return _msg;
56 }
57private:
58 std::string _msg;
60
61};
62
67public:
69 : base_exception("", status), url(url) {
70 }
71 std::string url;
72};
73
78public:
79 not_found_exception(const std::string& msg = "Not found")
81 }
82};
83
89public:
90 bad_request_exception(const std::string& msg)
92 }
93};
94
96public:
97 bad_param_exception(const std::string& msg)
99 }
100};
101
103public:
104 missing_param_exception(const std::string& param)
106 std::string("Missing mandatory parameter '") + param + "'") {
107 }
108};
109
111public:
112 bad_chunk_exception(const std::string& msg)
114 std::string("Can't read body chunk in a 'chunked' request '") + msg + "'") {
115 }
116};
117
119public:
120 server_error_exception(const std::string& msg)
122 }
123};
124
126public:
129 void register_params() {
130 add(&_msg, "message");
131 add(&_code, "code");
132 }
133
134 json_exception(const base_exception & e) {
135 set(e.str(), e.status());
136 }
137
138 json_exception(std::exception_ptr e) {
139 std::ostringstream exception_description;
140 exception_description << e;
141 set(exception_description.str(), http::reply::status_type::internal_server_error);
142 }
143private:
144 void set(const std::string& msg, http::reply::status_type code) {
145 register_params();
146 _msg = msg;
147 _code = (int) code;
148 }
149};
150
155public:
157 : base_exception(fmt::to_string(st), st)
158 {}
159};
160
161SEASTAR_MODULE_EXPORT_END
162}
163
164}
165
166SEASTAR_MODULE_EXPORT
167template <>
168struct fmt::formatter<seastar::httpd::base_exception> {
169 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
170 auto format(const seastar::httpd::base_exception& e, fmt::format_context& ctx) const {
171 return fmt::format_to(ctx.out(), "{} ({})", e.what(), e.status());
172 }
173};
Definition: exception.hh:110
Definition: exception.hh:95
Definition: exception.hh:88
Definition: exception.hh:40
Definition: exception.hh:125
Definition: exception.hh:102
Definition: exception.hh:77
Definition: exception.hh:66
Definition: exception.hh:118
Definition: exception.hh:154
Definition: routes.hh:43
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
status_type
Definition: reply.hh:65
@ moved_permanently
moved_permanently
@ internal_server_error
internal_server_error
Definition: json_elements.hh:228
virtual void add(json_base_element *element, std::string name, bool mandatory=false)