]> git.sesse.net Git - casparcg/blob - common/except.h
5a498acbeaa7cdc910c6839b2b4d9281ca6f7b30
[casparcg] / common / except.h
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #pragma once
23
24 #include "utf.h"
25
26 #include "os/stack_trace.h"
27
28 #include <exception>
29 #include <boost/exception/all.hpp>
30 #include <boost/exception/error_info.hpp>
31 #include <boost/throw_exception.hpp>
32
33 namespace caspar {
34
35 typedef boost::error_info<struct tag_arg_name_info,             std::string>    arg_name_info_t;
36 typedef boost::error_info<struct tag_arg_value_info,    std::string>    arg_value_info_t;
37 typedef boost::error_info<struct tag_msg_info,                  std::string>    msg_info_t;
38 typedef boost::error_info<struct tag_call_stack_info,   std::string>    call_stack_info_t;
39 typedef boost::error_info<struct tag_error_info,                std::string>    error_info_t;
40 typedef boost::error_info<struct tag_source_info,               std::string>    source_info_t;
41 typedef boost::error_info<struct tag_file_name_info,    std::string>    file_name_info_t;
42
43 template<typename T>
44 inline arg_name_info_t          arg_name_info(const T& str)             {return arg_name_info_t(u8(str));}
45 template<typename T>
46 inline arg_value_info_t         arg_value_info(const T& str)    {return arg_value_info_t(u8(str));}
47 template<typename T>
48 inline msg_info_t                       msg_info(const T& str)                  {return msg_info_t(u8(str));}
49 template<typename T>
50 inline call_stack_info_t        call_stack_info(const T& str)   {return call_stack_info_t(u8(str));}
51 template<typename T>
52 inline error_info_t                     error_info(const T& str)                {return error_info_t(u8(str));}
53 template<typename T>
54 inline source_info_t            source_info(const T& str)               {return source_info_t(u8(str));}
55 template<typename T>
56 inline file_name_info_t         file_name_info(const T& str)    {return file_name_info_t(u8(str));}
57
58 typedef boost::error_info<struct tag_line_info, size_t>                                         line_info;
59 typedef boost::error_info<struct tag_nested_exception_, std::exception_ptr> nested_exception;
60
61 struct caspar_exception                 : virtual boost::exception, virtual std::exception 
62 {
63         caspar_exception(){}
64         const char* what() const throw() override
65         {
66                 return boost::diagnostic_information_what(*this);
67         }
68 };
69
70 struct io_error                                 : virtual caspar_exception {};
71 struct directory_not_found              : virtual io_error {};
72 struct file_not_found                   : virtual io_error {};
73 struct file_read_error          : virtual io_error {};
74 struct file_write_error         : virtual io_error {};
75
76 struct invalid_argument                 : virtual caspar_exception {};
77 struct null_argument                    : virtual invalid_argument {};
78 struct out_of_range                             : virtual invalid_argument {};
79 struct programming_error                : virtual caspar_exception {};
80 struct bad_alloc                                : virtual caspar_exception {};
81
82 struct invalid_operation                : virtual caspar_exception {};
83 struct operation_failed                 : virtual caspar_exception {};
84 struct timed_out                                : virtual caspar_exception {};
85
86 struct not_implemented                  : virtual caspar_exception {};
87
88 struct user_error                               : virtual caspar_exception {};
89 struct not_supported                    : virtual user_error {};
90
91 #define CASPAR_THROW_EXCEPTION(e) BOOST_THROW_EXCEPTION(e << call_stack_info(caspar::get_call_stack()))
92
93 }
94