Libmagicxx v10.0.3
A modern C++23 wrapper for libmagic — the library that powers the Unix file command.
Loading...
Searching...
No Matches
magic_exception.hpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: Copyright (c) 2022-2026 Oğuz Toraman <oguz.toraman@tutanota.com> */
2/* SPDX-License-Identifier: LGPL-3.0-only */
3
58
59#ifndef MAGIC_EXCEPTION_HPP
60#define MAGIC_EXCEPTION_HPP
61
62#include <format>
63#include <stdexcept>
64#include <string>
65
66namespace Recognition {
77
102class MagicException : public std::runtime_error {
103public:
105 using std::runtime_error::runtime_error;
106
120 const std::string& function,
121 const std::string& error_message
122 )
123 : std::runtime_error{
124 error_message.empty()
125 ? function + " failed."
126 : function + " failed with " + error_message + "."
127 }
128 { }
129};
130
145class NullTracker final : public MagicException {
146public:
153 : MagicException{"shared ProgressTracker is null."}
154 { }
155};
156
168class EmptyPath final : public MagicException {
169public:
176 : MagicException{"path is empty."}
177 { }
178};
179
194public:
202 explicit PathIsNotRegularFile(const std::string& path)
203 : MagicException{std::format("'{}' is not a regular file.", path)}
204 { }
205};
206
220class PathIsNotDirectory final : public MagicException {
221public:
229 explicit PathIsNotDirectory(const std::string& path)
230 : MagicException{std::format("'{}' is not a directory.", path)}
231 { }
232};
233
245class PathDoesNotExist final : public MagicException {
246public:
254 explicit PathDoesNotExist(const std::string& path)
255 : MagicException{std::format("'{}' does not exist.", path)}
256 { }
257};
258
270class FilesystemError final : public MagicException {
271public:
280 FilesystemError(const std::string& path, const std::string& error_message)
281 : MagicException{std::format("'{}': {}.", path, error_message)}
282 { }
283};
284
305class MagicIsClosed final : public MagicException {
306public:
313 : MagicException{"Magic is closed."}
314 { }
315};
316
330class MagicOpenError final : public MagicException {
331public:
339 explicit MagicOpenError(const std::string& error_message)
340 : MagicException{"magic_open", error_message}
341 { }
342};
343
358public:
368 const std::string& error_message,
369 const std::string& database_file_path
370 )
372 std::format("Magic::LoadDatabaseFile({})", database_file_path),
373 error_message
374 }
375 { }
376};
377
400public:
407 : MagicException{"magic database is not loaded."}
408 { }
409};
410
427public:
437 const std::string& error_message,
438 const std::string& file_path
439 )
441 std::format("Magic::IdentifyFile({})", file_path),
442 error_message
443 }
444 { }
445};
446
461class MagicSetFlagsError final : public MagicException {
462public:
472 const std::string& error_message,
473 const std::string& flag_names
474 )
476 std::format("Magic::SetFlags({})", flag_names),
477 error_message
478 }
479 { }
480};
481
497public:
508 const std::string& error_message,
509 const std::string& parameter_name,
510 std::size_t value
511 )
513 std::format("Magic::SetParameter({}, {})", parameter_name, value),
514 error_message
515 }
516 { }
517};
518
520} /* namespace Recognition */
521
522#endif /* MAGIC_EXCEPTION_HPP */
EmptyPath()
Construct EmptyPath exception.
Definition magic_exception.hpp:175
FilesystemError(const std::string &path, const std::string &error_message)
Construct FilesystemError with path and error description.
Definition magic_exception.hpp:280
MagicDatabaseNotLoaded()
Construct MagicDatabaseNotLoaded exception.
Definition magic_exception.hpp:406
MagicException(const std::string &function, const std::string &error_message)
Construct MagicException with function name and error message.
Definition magic_exception.hpp:119
MagicIdentifyFileError(const std::string &error_message, const std::string &file_path)
Construct MagicIdentifyFileError with details.
Definition magic_exception.hpp:436
MagicIsClosed()
Construct MagicIsClosed exception.
Definition magic_exception.hpp:312
MagicLoadDatabaseFileError(const std::string &error_message, const std::string &database_file_path)
Construct MagicLoadDatabaseFileError with details.
Definition magic_exception.hpp:367
MagicOpenError(const std::string &error_message)
Construct MagicOpenError with error description.
Definition magic_exception.hpp:339
MagicSetFlagsError(const std::string &error_message, const std::string &flag_names)
Construct MagicSetFlagsError with details.
Definition magic_exception.hpp:471
MagicSetParameterError(const std::string &error_message, const std::string &parameter_name, std::size_t value)
Construct MagicSetParameterError with details.
Definition magic_exception.hpp:507
NullTracker()
Construct NullTracker exception.
Definition magic_exception.hpp:152
PathDoesNotExist(const std::string &path)
Construct PathDoesNotExist with the missing path.
Definition magic_exception.hpp:254
PathIsNotDirectory(const std::string &path)
Construct PathIsNotDirectory with the invalid path.
Definition magic_exception.hpp:229
PathIsNotRegularFile(const std::string &path)
Construct PathIsNotRegularFile with the invalid path.
Definition magic_exception.hpp:202
Root namespace for the libmagicxx library.