aboutsummaryrefslogtreecommitdiff
path: root/src/errors/io.hpp
blob: 1d0aec391eb03c3337e382e9ad74fd7b053c82d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#pragma once

#include "util/fs.hpp"

#include <fmt/core.h>

#include <filesystem>
#include <stdexcept>
#include <string>

class IOError : public std::runtime_error
{
public:
	IOError() noexcept : std::runtime_error("Unknown IO error") {}

	explicit IOError(const std::string &error_message) noexcept
		: std::runtime_error(error_message)
	{
	}
};

class NoSuchFileOrDirectoryError : public IOError
{
public:
	explicit NoSuchFileOrDirectoryError(const std::filesystem::path &path) noexcept
		: IOError(
			  fmt::format("No file or directory exists with path '{}'", path.string()))
	{
	}
};

class WrongFileTypeError : public IOError
{
public:
	explicit WrongFileTypeError(
		const std::filesystem::path &path,
		std::filesystem::file_type expected_file_type,
		std::filesystem::file_type actual_file_type) noexcept
		: IOError(fmt::format(
			  "Wrong file type of file with path '{}'. Expected {}, is {}",
			  path.string(),
			  file_type_names.at(expected_file_type),
			  file_type_names.at(actual_file_type)))
	{
	}
};

class FileNotOpenError : public IOError
{
public:
	explicit FileNotOpenError(const std::filesystem::path &path) noexcept
		: IOError(fmt::format("Failed to open file with path '{}'", path.string()))
	{
	}
};