errors.odin 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package os2
  2. import "core:io"
  3. import "base:runtime"
  4. General_Error :: enum u32 {
  5. None,
  6. Permission_Denied,
  7. Exist,
  8. Not_Exist,
  9. Closed,
  10. Timeout,
  11. Broken_Pipe,
  12. // Indicates that an attempt to retrieve a file's size was made, but the
  13. // file doesn't have a size.
  14. No_Size,
  15. Invalid_File,
  16. Invalid_Dir,
  17. Invalid_Path,
  18. Invalid_Callback,
  19. Pattern_Has_Separator,
  20. Unsupported,
  21. }
  22. Platform_Error :: enum i32 {None=0}
  23. Error :: union #shared_nil {
  24. General_Error,
  25. io.Error,
  26. runtime.Allocator_Error,
  27. Platform_Error,
  28. }
  29. #assert(size_of(Error) == size_of(u64))
  30. ERROR_NONE :: Error{}
  31. is_platform_error :: proc(ferr: Error) -> (err: i32, ok: bool) {
  32. v := ferr.(Platform_Error) or_else {}
  33. return i32(v), i32(v) != 0
  34. }
  35. error_string :: proc(ferr: Error) -> string {
  36. if ferr == nil {
  37. return ""
  38. }
  39. switch e in ferr {
  40. case General_Error:
  41. switch e {
  42. case .None: return ""
  43. case .Permission_Denied: return "permission denied"
  44. case .Exist: return "file already exists"
  45. case .Not_Exist: return "file does not exist"
  46. case .Closed: return "file already closed"
  47. case .Timeout: return "i/o timeout"
  48. case .Broken_Pipe: return "Broken pipe"
  49. case .No_Size: return "file has no definite size"
  50. case .Invalid_File: return "invalid file"
  51. case .Invalid_Dir: return "invalid directory"
  52. case .Invalid_Path: return "invalid path"
  53. case .Invalid_Callback: return "invalid callback"
  54. case .Unsupported: return "unsupported"
  55. case .Pattern_Has_Separator: return "pattern has separator"
  56. }
  57. case io.Error:
  58. switch e {
  59. case .None: return ""
  60. case .EOF: return "eof"
  61. case .Unexpected_EOF: return "unexpected eof"
  62. case .Short_Write: return "short write"
  63. case .Invalid_Write: return "invalid write result"
  64. case .Short_Buffer: return "short buffer"
  65. case .No_Progress: return "multiple read calls return no data or error"
  66. case .Invalid_Whence: return "invalid whence"
  67. case .Invalid_Offset: return "invalid offset"
  68. case .Invalid_Unread: return "invalid unread"
  69. case .Negative_Read: return "negative read"
  70. case .Negative_Write: return "negative write"
  71. case .Negative_Count: return "negative count"
  72. case .Buffer_Full: return "buffer full"
  73. case .Unknown, .Empty: //
  74. }
  75. case runtime.Allocator_Error:
  76. switch e {
  77. case .None: return ""
  78. case .Out_Of_Memory: return "out of memory"
  79. case .Invalid_Pointer: return "invalid allocator pointer"
  80. case .Invalid_Argument: return "invalid allocator argument"
  81. case .Mode_Not_Implemented: return "allocator mode not implemented"
  82. }
  83. case Platform_Error:
  84. return _error_string(i32(e))
  85. }
  86. return "unknown error"
  87. }
  88. print_error :: proc(f: ^File, ferr: Error, msg: string) {
  89. TEMP_ALLOCATOR_GUARD()
  90. err_str := error_string(ferr)
  91. // msg + ": " + err_str + '\n'
  92. length := len(msg) + 2 + len(err_str) + 1
  93. buf := make([]u8, length, temp_allocator())
  94. copy(buf, msg)
  95. buf[len(msg)] = ':'
  96. buf[len(msg) + 1] = ' '
  97. copy(buf[len(msg) + 2:], err_str)
  98. buf[length - 1] = '\n'
  99. write(f, buf)
  100. }