errors.odin 2.3 KB

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