]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/http/exception.hh
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / seastar / include / seastar / http / 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 #include <seastar/util/log.hh>
24 #include <seastar/http/reply.hh>
25 #include <seastar/json/json_elements.hh>
26
27 namespace seastar {
28
29 namespace httpd {
30
31 /**
32 * The base_exception is a base for all http exception.
33 * It contains a message that will be return as the message content
34 * and a status that will be return as a status code.
35 */
36 class base_exception : public std::exception {
37 public:
38 base_exception(const std::string& msg, http::reply::status_type status)
39 : _msg(msg), _status(status) {
40 }
41
42 virtual const char* what() const throw () {
43 return _msg.c_str();
44 }
45
46 http::reply::status_type status() const {
47 return _status;
48 }
49
50 virtual const std::string& str() const {
51 return _msg;
52 }
53 private:
54 std::string _msg;
55 http::reply::status_type _status;
56
57 };
58
59 /**
60 * Throwing this exception will result in a redirect to the given url
61 */
62 class redirect_exception : public base_exception {
63 public:
64 redirect_exception(const std::string& url)
65 : base_exception("", http::reply::status_type::moved_permanently), url(
66 url) {
67 }
68 std::string url;
69 };
70
71 /**
72 * Throwing this exception will result in a 404 not found result
73 */
74 class not_found_exception : public base_exception {
75 public:
76 not_found_exception(const std::string& msg = "Not found")
77 : base_exception(msg, http::reply::status_type::not_found) {
78 }
79 };
80
81 /**
82 * Throwing this exception will result in a 400 bad request result
83 */
84
85 class bad_request_exception : public base_exception {
86 public:
87 bad_request_exception(const std::string& msg)
88 : base_exception(msg, http::reply::status_type::bad_request) {
89 }
90 };
91
92 class bad_param_exception : public bad_request_exception {
93 public:
94 bad_param_exception(const std::string& msg)
95 : bad_request_exception(msg) {
96 }
97 };
98
99 class missing_param_exception : public bad_request_exception {
100 public:
101 missing_param_exception(const std::string& param)
102 : bad_request_exception(
103 std::string("Missing mandatory parameter '") + param + "'") {
104 }
105 };
106
107 class bad_chunk_exception : public bad_request_exception {
108 public:
109 bad_chunk_exception(const std::string& msg)
110 : bad_request_exception(
111 std::string("Can't read body chunk in a 'chunked' request '") + msg + "'") {
112 }
113 };
114
115 class server_error_exception : public base_exception {
116 public:
117 server_error_exception(const std::string& msg)
118 : base_exception(msg, http::reply::status_type::internal_server_error) {
119 }
120 };
121
122 class json_exception : public json::json_base {
123 public:
124 json::json_element<std::string> _msg;
125 json::json_element<int> _code;
126 void register_params() {
127 add(&_msg, "message");
128 add(&_code, "code");
129 }
130
131 json_exception(const base_exception & e) {
132 set(e.str(), e.status());
133 }
134
135 json_exception(std::exception_ptr e) {
136 std::ostringstream exception_description;
137 exception_description << e;
138 set(exception_description.str(), http::reply::status_type::internal_server_error);
139 }
140 private:
141 void set(const std::string& msg, http::reply::status_type code) {
142 register_params();
143 _msg = msg;
144 _code = (int) code;
145 }
146 };
147
148 }
149
150 }