]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/http/exception.hh
update sources to ceph Nautilus 14.2.1
[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
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, 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 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 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("", 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, 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, 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 server_error_exception : public base_exception {
108 public:
109 server_error_exception(const std::string& msg)
110 : base_exception(msg, reply::status_type::internal_server_error) {
111 }
112 };
113
114 class json_exception : public json::json_base {
115 public:
116 json::json_element<std::string> _msg;
117 json::json_element<int> _code;
118 void register_params() {
119 add(&_msg, "message");
120 add(&_code, "code");
121 }
122
123 json_exception(const base_exception & e) {
124 set(e.str(), e.status());
125 }
126
127 json_exception(const std::exception& e) {
128 set(e.what(), reply::status_type::internal_server_error);
129 }
130 private:
131 void set(const std::string& msg, reply::status_type code) {
132 register_params();
133 _msg = msg;
134 _code = (int) code;
135 }
136 };
137
138 }
139
140 }