errors.odin 2.5 KB

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