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.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * Copyright 2015 Cloudius Systems
23 #include <seastar/util/log.hh>
24 #include <seastar/http/reply.hh>
25 #include <seastar/json/json_elements.hh>
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.
36 class base_exception : public std::exception {
38 base_exception(const std::string& msg, http::reply::status_type status)
39 : _msg(msg), _status(status) {
42 virtual const char* what() const throw () {
46 http::reply::status_type status() const {
50 virtual const std::string& str() const {
55 http::reply::status_type _status;
60 * Throwing this exception will result in a redirect to the given url
62 class redirect_exception : public base_exception {
64 redirect_exception(const std::string& url)
65 : base_exception("", http::reply::status_type::moved_permanently), url(
72 * Throwing this exception will result in a 404 not found result
74 class not_found_exception : public base_exception {
76 not_found_exception(const std::string& msg = "Not found")
77 : base_exception(msg, http::reply::status_type::not_found) {
82 * Throwing this exception will result in a 400 bad request result
85 class bad_request_exception : public base_exception {
87 bad_request_exception(const std::string& msg)
88 : base_exception(msg, http::reply::status_type::bad_request) {
92 class bad_param_exception : public bad_request_exception {
94 bad_param_exception(const std::string& msg)
95 : bad_request_exception(msg) {
99 class missing_param_exception : public bad_request_exception {
101 missing_param_exception(const std::string& param)
102 : bad_request_exception(
103 std::string("Missing mandatory parameter '") + param + "'") {
107 class bad_chunk_exception : public bad_request_exception {
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 + "'") {
115 class server_error_exception : public base_exception {
117 server_error_exception(const std::string& msg)
118 : base_exception(msg, http::reply::status_type::internal_server_error) {
122 class json_exception : public json::json_base {
124 json::json_element<std::string> _msg;
125 json::json_element<int> _code;
126 void register_params() {
127 add(&_msg, "message");
131 json_exception(const base_exception & e) {
132 set(e.str(), e.status());
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);
141 void set(const std::string& msg, http::reply::status_type code) {