Libmagicxx v9.0.2
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 {
12
18class magic_exception : public std::runtime_error {
19public:
20 using std::runtime_error::runtime_error;
21
30 const std::string& function,
31 const std::string& error_message
32 )
33 : std::runtime_error{
34 error_message.empty()
35 ? function + " failed."
36 : function + " failed with " + error_message + "."
37 }
38 { }
39};
40
46class null_tracker final : public magic_exception {
47public:
52 : magic_exception{"shared progress_tracker is null."}
53 { }
54};
55
61class empty_path final : public magic_exception {
62public:
67 : magic_exception{"path is empty."}
68 { }
69};
70
78public:
85 explicit path_is_not_regular_file(const std::string& path)
86 : magic_exception{std::format("'{}' is not a regular file.", path)}
87 { }
88};
89
97public:
104 explicit path_is_not_directory(const std::string& path)
105 : magic_exception{std::format("'{}' is not a directory.", path)}
106 { }
107};
108
115public:
121 explicit path_does_not_exist(const std::string& path)
122 : magic_exception{std::format("'{}' does not exist.", path)}
123 { }
124};
125
131class filesystem_error final : public magic_exception {
132public:
139 filesystem_error(const std::string& path, const std::string& error_message)
140 : magic_exception{std::format("'{}': {}.", path, error_message)}
141 { }
142};
143
149class magic_is_closed final : public magic_exception {
150public:
155 : magic_exception{"magic is closed."}
156 { }
157};
158
164class magic_open_error final : public magic_exception {
165public:
171 explicit magic_open_error(const std::string& error_message)
172 : magic_exception{"magic_open", error_message}
173 { }
174};
175
182public:
191 const std::string& error_message,
192 const std::string& database_file_path
193 )
195 std::format("magic_load_database_file({})", database_file_path),
196 error_message
197 }
198 { }
199};
200
207public:
212 : magic_exception{"magic database is not loaded."}
213 { }
214};
215
222public:
231 const std::string& error_message,
232 const std::string& file_path
233 )
235 std::format("magic_identify_file({})", file_path),
236 error_message
237 }
238 { }
239};
240
247public:
256 const std::string& error_message,
257 const std::string& flag_names
258 )
260 std::format("magic_set_flags({})", flag_names),
261 error_message
262 }
263 { }
264};
265
272public:
282 const std::string& error_message,
283 const std::string& parameter_name,
284 std::size_t value
285 )
287 std::format("magic_set_parameter({}, {})", parameter_name, value),
288 error_message
289 }
290 { }
291};
292
293} /* namespace recognition */
294
295#endif /* MAGIC_EXCEPTION_HPP */
Exception thrown when a path is empty.
Definition magic_exception.hpp:61
empty_path()
Construct empty_path.
Definition magic_exception.hpp:66
Exception thrown when the underlying std::filesystem OS API fails.
Definition magic_exception.hpp:131
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:139
Exception thrown when magic database is not loaded.
Definition magic_exception.hpp:206
magic_database_not_loaded()
Construct magic_database_not_loaded.
Definition magic_exception.hpp:211
The base class for all exceptions thrown by the magic class.
Definition magic_exception.hpp:18
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:29
Exception thrown when magic::identify_file(s) fails.
Definition magic_exception.hpp:221
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:230
Exception thrown when magic is closed.
Definition magic_exception.hpp:149
magic_is_closed()
Construct magic_is_closed.
Definition magic_exception.hpp:154
Exception thrown when magic::load_database_file fails.
Definition magic_exception.hpp:181
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:190
Exception thrown when magic::open fails.
Definition magic_exception.hpp:164
magic_open_error(const std::string &error_message)
Construct magic_open_error with an error message.
Definition magic_exception.hpp:171
Exception thrown when magic::set_flags fails.
Definition magic_exception.hpp:246
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:255
Exception thrown when magic::set_parameter(s) fails.
Definition magic_exception.hpp:271
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:281
Exception thrown when a shared progress_tracker is null.
Definition magic_exception.hpp:46
null_tracker()
Construct null_tracker.
Definition magic_exception.hpp:51
Exception thrown when a path does not exist.
Definition magic_exception.hpp:114
path_does_not_exist(const std::string &path)
Construct path_does_not_exist with the path that does not exist.
Definition magic_exception.hpp:121
Exception thrown from magic::identify_files(directory) when the path is not a directory.
Definition magic_exception.hpp:96
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:104
Exception thrown from magic::load_database_file when the database file path is not a regular file.
Definition magic_exception.hpp:77
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:85
Definition magic.hpp:19