Echo Writes Code

posix_errors.cpp

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "crucible/core/errors.hpp"

#include <cerrno>
#include <cstring>

namespace crucible
{
  posix_error::posix_error(int const code) noexcept :
    my_code { code }
  {}

  posix_error::posix_error() noexcept :
    my_code { errno }
  {}

  auto posix_error::code() const noexcept -> int
  {
    return my_code;
  }

  auto posix_error::message() const -> string
  {
    // If `errno` happens to be 0, we override the default message because it sucks (usually
    // something like `Unrecognized code (0)`)
    if (my_code == 0)
    {
      return "no further information";
    }

    char const *description_data { std::strerror(my_code) };
    std::size_t const description_size { std::strlen(description_data) };
    return string { description_data, description_size };
  }

  // Errors arising from calling a POSIX API function

  auto fstat_failed::describe() const -> string
  {
    return "::fstat() failed: " + posix_error::message();
  }

  auto mkdir_failed::describe() const -> string
  {
    return "::mkdir() failed: " + posix_error::message();
  }

  auto open_failed::describe() const -> string
  {
    return "::open() failed: " + posix_error::message();
  }

  auto opendir_failed::describe() const -> string
  {
    return "::opendir() failed: " + posix_error::message();
  }

  auto read_failed::describe() const -> string
  {
    return "::read() failed: " + posix_error::message();
  }

  auto readdir_failed::describe() const -> string
  {
    return "::readdir() failed: " + posix_error::message();
  }

  auto rmdir_failed::describe() const -> string
  {
    return "::rmdir() failed: " + posix_error::message();
  }

  auto stat_failed::describe() const -> string
  {
    return "::stat() failed: " + posix_error::message();
  }

  auto unlink_failed::describe() const -> string
  {
    return "::unlink() failed: " + posix_error::message();
  }

  auto write_failed::describe() const -> string
  {
    return "::write() failed: " + posix_error::message();
  }

  // Errors arising from a precondition or postcondition being violated

  auto incomplete_read::describe() const -> string
  {
    return string { "fewer bytes than intended were read" };
  }

  auto incomplete_write::describe() const -> string
  {
    return string { "fewer bytes than intended were written" };
  }

  auto negative_file_size::describe() const -> string
  {
    return string { "file size was reported as a negative value" };
  }
}