libkeepass 0.2.0
KeePass key database importer/exporter library
Loading...
Searching...
No Matches
exception.hh
Go to the documentation of this file.
1/*
2 * libkeepass - KeePass key database importer/exporter
3 * Copyright (C) 2014 Christian Kindahl
4 * Copyright (C) 2024 Dominik Krümpelmann
5 *
6 * This program 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 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
22#pragma once
23
24#include <cstdint>
25#include <optional>
26#include <string>
27#include <utility>
28
29#include "libkeepass/export.hh"
30
31namespace keepass {
32
48
50LIBKEEPASS_API const char* ErrorCategoryName(ErrorCategory category) noexcept;
51
55class LIBKEEPASS_API Error : public std::exception {
56public:
58 virtual ErrorCategory category() const noexcept = 0;
59};
60
64class LIBKEEPASS_API PasswordError final : public Error {
65public:
66 explicit PasswordError() = default;
67
68 const char* what() const noexcept override { return "Invalid password."; }
69
70 ErrorCategory category() const noexcept override { return ErrorCategory::kAuthentication; }
71};
72
80private:
81 const std::string msg_{};
82 std::optional<uint64_t> offset_{};
83
84public:
85 explicit FormatError(std::string msg) : msg_(std::move(msg)) {}
86
91 FormatError(std::string msg, uint64_t offset) : msg_(std::move(msg)), offset_(offset) {}
92
93 const char* what() const noexcept override { return msg_.c_str(); }
94
96 std::optional<uint64_t> offset() const noexcept { return offset_; }
97
98 ErrorCategory category() const noexcept override { return ErrorCategory::kFormat; }
99};
100
109public:
110 explicit UnsupportedFeatureError(std::string msg) : FormatError(std::move(msg)) {}
111
112 UnsupportedFeatureError(std::string msg, uint64_t offset) : FormatError(std::move(msg), offset) {}
113
114 ErrorCategory category() const noexcept override { return ErrorCategory::kUnsupported; }
115};
116
121public:
122 explicit ResourceLimitError(std::string msg) : FormatError(std::move(msg)) {}
123
124 ResourceLimitError(std::string msg, uint64_t offset) : FormatError(std::move(msg), offset) {}
125
126 ErrorCategory category() const noexcept override { return ErrorCategory::kResourceLimit; }
127};
128
133private:
134 const std::string msg_{};
135
136public:
137 explicit InternalError(std::string msg) : msg_(std::move(msg)) {}
138
139 const char* what() const noexcept override { return msg_.c_str(); }
140
141 ErrorCategory category() const noexcept override { return ErrorCategory::kInternal; }
142};
143
148private:
149 const std::string msg_{};
150
151public:
152 explicit IoError(std::string msg) : msg_(std::move(msg)) {}
153
154 const char* what() const noexcept override { return msg_.c_str(); }
155
156 ErrorCategory category() const noexcept override { return ErrorCategory::kIo; }
157};
158
163public:
164 FileNotFoundError() : IoError("File not found.") {}
165};
166
167} // namespace keepass
Common base class for all libkeepass exceptions.
Definition exception.hh:55
virtual ErrorCategory category() const noexcept=0
Returns the coarse category of this error.
Thrown when a requested file does not exist.
Definition exception.hh:162
Thrown when a database or stream has an invalid format.
Definition exception.hh:79
std::optional< uint64_t > offset() const noexcept
Returns the byte offset of the failure, if known.
Definition exception.hh:96
ErrorCategory category() const noexcept override
Returns the coarse category of this error.
Definition exception.hh:98
FormatError(std::string msg, uint64_t offset)
Definition exception.hh:91
Thrown for the same class of errors as asserts, but in release builds.
Definition exception.hh:132
ErrorCategory category() const noexcept override
Returns the coarse category of this error.
Definition exception.hh:141
Thrown for input/output errors while reading or writing databases.
Definition exception.hh:147
ErrorCategory category() const noexcept override
Returns the coarse category of this error.
Definition exception.hh:156
Thrown when an invalid password is supplied while opening a database.
Definition exception.hh:64
ErrorCategory category() const noexcept override
Returns the coarse category of this error.
Definition exception.hh:70
Thrown when a parser, decompressor or XML budget is exceeded.
Definition exception.hh:120
ErrorCategory category() const noexcept override
Returns the coarse category of this error.
Definition exception.hh:126
Thrown when a database uses a valid but unsupported format feature.
Definition exception.hh:108
ErrorCategory category() const noexcept override
Returns the coarse category of this error.
Definition exception.hh:114
LIBKEEPASS_API const char * ErrorCategoryName(ErrorCategory category) noexcept
Returns a stable, lowercase name for an error category.
ErrorCategory
Coarse error category shared by all library exceptions.
Definition exception.hh:40
@ kFormat
Corrupt, truncated or otherwise invalid database.
@ kResourceLimit
A parser/decompression resource budget was exceeded.
@ kUnsupported
A valid but unsupported format feature/version.
@ kInternal
An internal invariant or allocation failed.
@ kAuthentication
Wrong password, key file or key.
@ kIo
Reading or writing failed.
Visibility/export macro for the shared library.
#define LIBKEEPASS_API
Marks a class, function or variable as part of the public ABI.
Definition export.hh:43