errors.odin 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package os2
  2. Platform_Error_Min_Bits :: 32;
  3. Error :: enum u64 {
  4. None = 0,
  5. // General Errors
  6. Invalid_Argument,
  7. Permission_Denied,
  8. Exist,
  9. Not_Exist,
  10. Closed,
  11. // Timeout Errors
  12. Timeout,
  13. // I/O Errors
  14. // EOF is the error returned by `read` when no more input is available
  15. EOF,
  16. // Unexpected_EOF means that EOF was encountered in the middle of reading a fixed-sized block of data
  17. Unexpected_EOF,
  18. // Short_Write means that a write accepted fewer bytes than requested but failed to return an explicit error
  19. Short_Write,
  20. // Invalid_Write means that a write returned an impossible count
  21. Invalid_Write,
  22. // Short_Buffer means that a read required a longer buffer than was provided
  23. Short_Buffer,
  24. // No_Progress is returned by some implementations of `io.Reader` when many calls
  25. // to `read` have failed to return any data or error.
  26. // This is usually a signed of a broken `io.Reader` implementation
  27. No_Progress,
  28. Invalid_Whence,
  29. Invalid_Offset,
  30. Invalid_Unread,
  31. Negative_Read,
  32. Negative_Write,
  33. Negative_Count,
  34. Buffer_Full,
  35. // Platform Specific Errors
  36. Platform_Minimum = 1<<Platform_Error_Min_Bits,
  37. }
  38. Path_Error :: struct {
  39. op: string,
  40. path: string,
  41. err: Error,
  42. }
  43. Link_Error :: struct {
  44. op: string,
  45. old: string,
  46. new: string,
  47. err: Error,
  48. }
  49. path_error_delete :: proc(perr: Maybe(Path_Error)) {
  50. if err, ok := perr.?; ok {
  51. context.allocator = error_allocator();
  52. delete(err.op);
  53. delete(err.path);
  54. }
  55. }
  56. link_error_delete :: proc(lerr: Maybe(Link_Error)) {
  57. if err, ok := lerr.?; ok {
  58. context.allocator = error_allocator();
  59. delete(err.op);
  60. delete(err.old);
  61. delete(err.new);
  62. }
  63. }
  64. is_platform_error :: proc(ferr: Error) -> (err: i32, ok: bool) {
  65. if ferr >= .Platform_Minimum {
  66. err = i32(u64(ferr)>>Platform_Error_Min_Bits);
  67. ok = true;
  68. }
  69. return;
  70. }
  71. error_from_platform_error :: proc(errno: i32) -> Error {
  72. return Error(u64(errno) << Platform_Error_Min_Bits);
  73. }
  74. error_string :: proc(ferr: Error) -> string {
  75. #partial switch ferr {
  76. case .None: return "";
  77. case .Invalid_Argument: return "invalid argument";
  78. case .Permission_Denied: return "permission denied";
  79. case .Exist: return "file already exists";
  80. case .Not_Exist: return "file does not exist";
  81. case .Closed: return "file already closed";
  82. case .Timeout: return "i/o timeout";
  83. case .EOF: return "eof";
  84. case .Unexpected_EOF: return "unexpected eof";
  85. case .Short_Write: return "short write";
  86. case .Invalid_Write: return "invalid write result";
  87. case .Short_Buffer: return "short buffer";
  88. case .No_Progress: return "multiple read calls return no data or error";
  89. case .Invalid_Whence: return "invalid whence";
  90. case .Invalid_Offset: return "invalid offset";
  91. case .Invalid_Unread: return "invalid unread";
  92. case .Negative_Read: return "negative read";
  93. case .Negative_Write: return "negative write";
  94. case .Negative_Count: return "negative count";
  95. case .Buffer_Full: return "buffer full";
  96. }
  97. if errno, ok := is_platform_error(ferr); ok {
  98. return _error_string(errno);
  99. }
  100. return "unknown error";
  101. }