Libmagicxx v9.1.1
A C++ wrapper library over the Magic Number Recognition Library.
Loading...
Searching...
No Matches
magic_exception.hpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: Copyright (c) 2022-2025 Oğuz Toraman <oguz.toraman@tutanota.com> */
2/* SPDX-License-Identifier: LGPL-3.0-only */
3
4#ifndef MAGIC_EXCEPTION_HPP
5#define MAGIC_EXCEPTION_HPP
6
7#include <format>
8#include <stdexcept>
9#include <string>
10
11namespace recognition {
17class magic_exception : public std::runtime_error {
18public:
19 using std::runtime_error::runtime_error;
20
29 const std::string& function,
30 const std::string& error_message
31 )
32 : std::runtime_error{
33 error_message.empty()
34 ? function + " failed."
35 : function + " failed with " + error_message + "."
36 }
37 { }
38};
39
45class null_tracker final : public magic_exception {
46public:
51 : magic_exception{"shared progress_tracker is null."}
52 { }
53};
54
60class empty_path final : public magic_exception {
61public:
66 : magic_exception{"path is empty."}
67 { }
68};
69
77public:
84 explicit path_is_not_regular_file(const std::string& path)
85 : magic_exception{std::format("'{}' is not a regular file.", path)}
86 { }
87};
88
96public:
103 explicit path_is_not_directory(const std::string& path)
104 : magic_exception{std::format("'{}' is not a directory.", path)}
105 { }
106};
107
114public:
120 explicit path_does_not_exist(const std::string& path)
121 : magic_exception{std::format("'{}' does not exist.", path)}
122 { }
123};
124
130class filesystem_error final : public magic_exception {
131public:
138 filesystem_error(const std::string& path, const std::string& error_message)
139 : magic_exception{std::format("'{}': {}.", path, error_message)}
140 { }
141};
142
148class magic_is_closed final : public magic_exception {
149public:
154 : magic_exception{"magic is closed."}
155 { }
156};
157
163class magic_open_error final : public magic_exception {
164public:
170 explicit magic_open_error(const std::string& error_message)
171 : magic_exception{"magic_open", error_message}
172 { }
173};
174
181public:
190 const std::string& error_message,
191 const std::string& database_file_path
192 )
194 std::format("magic_load_database_file({})", database_file_path),
195 error_message
196 }
197 { }
198};
199
206public:
211 : magic_exception{"magic database is not loaded."}
212 { }
213};
214
221public:
230 const std::string& error_message,
231 const std::string& file_path
232 )
234 std::format("magic_identify_file({})", file_path),
235 error_message
236 }
237 { }
238};
239
246public:
255 const std::string& error_message,
256 const std::string& flag_names
257 )
259 std::format("magic_set_flags({})", flag_names),
260 error_message
261 }
262 { }
263};
264
271public:
281 const std::string& error_message,
282 const std::string& parameter_name,
283 std::size_t value
284 )
286 std::format("magic_set_parameter({}, {})", parameter_name, value),
287 error_message
288 }
289 { }
290};
291} /* namespace recognition */
292
293#endif /* MAGIC_EXCEPTION_HPP */
empty_path()
Construct empty_path.
Definition magic_exception.hpp:65
filesystem_error(const std::string &path, const std::string &error_message)
Construct filesystem_error with a path and an error message.
Definition magic_exception.hpp:138
magic_database_not_loaded()
Construct magic_database_not_loaded.
Definition magic_exception.hpp:210
magic_exception(const std::string &function, const std::string &error_message)
Construct magic_exception with an error message and the name of the funtion where the error occurred.
Definition magic_exception.hpp:28
magic_identify_file_error(const std::string &error_message, const std::string &file_path)
Construct magic_identify_file_error with an error message, and the path of the file.
Definition magic_exception.hpp:229
magic_is_closed()
Construct magic_is_closed.
Definition magic_exception.hpp:153
magic_load_database_file_error(const std::string &error_message, const std::string &database_file_path)
Construct magic_load_database_file_error with an error message and the path of the database file.
Definition magic_exception.hpp:189
magic_open_error(const std::string &error_message)
Construct magic_open_error with an error message.
Definition magic_exception.hpp:170
magic_set_flags_error(const std::string &error_message, const std::string &flag_names)
Construct magic_set_flags_error with an error message and the names of the flags.
Definition magic_exception.hpp:254
magic_set_parameter_error(const std::string &error_message, const std::string &parameter_name, std::size_t value)
Construct magic_set_parameter_error with an error message, the name of the parameter and its value.
Definition magic_exception.hpp:280
null_tracker()
Construct null_tracker.
Definition magic_exception.hpp:50
path_does_not_exist(const std::string &path)
Construct path_does_not_exist with the path that does not exist.
Definition magic_exception.hpp:120
path_is_not_directory(const std::string &path)
Construct path_is_not_directory with the path that is not a directory.
Definition magic_exception.hpp:103
path_is_not_regular_file(const std::string &path)
Construct path_is_not_regular_file with the database file path that is not a regular file.
Definition magic_exception.hpp:84
Definition magic.hpp:19