Files
fsh-oop/include/util/error.hpp
2025-08-29 00:02:45 +02:00

76 lines
1.9 KiB
C++

#pragma once
#include <stdexcept>
#include <optional>
namespace fsh::util {
// Generates msg only once what has been called
// Usefull for errors meant to be silently discarded frequently
class LazyError : public std::exception {
public:
LazyError() {}
LazyError(const std::string& msg_) : msg_(msg_) {}
LazyError(const char* msg_) : msg_(msg_) {}
const char* what() const noexcept override;
protected:
mutable std::optional<std::string> msg_;
virtual std::string build_msg() const noexcept;
};
class CmdNotFoundError : public LazyError {
public:
CmdNotFoundError(std::string cmd_) : cmd_(cmd_) {}
protected:
std::string build_msg() const noexcept override;
private:
std::string cmd_;
};
class CmdError : public LazyError {
public:
CmdError(const std::string& msg_) : LazyError(msg_) {}
CmdError(const char* msg_) : LazyError(msg_) {}
};
class RuntimeError : public std::exception {
public:
const char* msg_;
RuntimeError() = delete;
RuntimeError(const char* msg_) : msg_(msg_) {}
const char* what() const noexcept override;
};
class FileDeleteError : public RuntimeError {
public:
FileDeleteError() : RuntimeError("Error deleting file") {}
};
class FileExistsError : public RuntimeError {
public:
FileExistsError() : RuntimeError("File already exists") {}
};
class FileOpenError : public RuntimeError {
public:
FileOpenError() : RuntimeError("Failed to open file") {}
};
class DoubleInputError : public RuntimeError {
public:
DoubleInputError() : RuntimeError("Tried to set input twice") {}
};
class LexerError : public LazyError {
public:
LexerError(const std::string& msg_) : LazyError(msg_) {}
LexerError(const char* msg_) : LazyError(msg_) {}
};
}