errors.odin 3.0 KB

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