error.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
  2. //
  3. // Use of this source code is governed by an MIT-style
  4. // license that can be found in the LICENSE file.
  5. package sqlite3
  6. /*
  7. #ifndef USE_LIBSQLITE3
  8. #include "sqlite3-binding.h"
  9. #else
  10. #include <sqlite3.h>
  11. #endif
  12. */
  13. import "C"
  14. import "syscall"
  15. // ErrNo inherit errno.
  16. type ErrNo int
  17. // ErrNoMask is mask code.
  18. const ErrNoMask C.int = 0xff
  19. // ErrNoExtended is extended errno.
  20. type ErrNoExtended int
  21. // Error implement sqlite error code.
  22. type Error struct {
  23. Code ErrNo /* The error code returned by SQLite */
  24. ExtendedCode ErrNoExtended /* The extended error code returned by SQLite */
  25. SystemErrno syscall.Errno /* The system errno returned by the OS through SQLite, if applicable */
  26. err string /* The error string returned by sqlite3_errmsg(),
  27. this usually contains more specific details. */
  28. }
  29. // result codes from http://www.sqlite.org/c3ref/c_abort.html
  30. var (
  31. ErrError = ErrNo(1) /* SQL error or missing database */
  32. ErrInternal = ErrNo(2) /* Internal logic error in SQLite */
  33. ErrPerm = ErrNo(3) /* Access permission denied */
  34. ErrAbort = ErrNo(4) /* Callback routine requested an abort */
  35. ErrBusy = ErrNo(5) /* The database file is locked */
  36. ErrLocked = ErrNo(6) /* A table in the database is locked */
  37. ErrNomem = ErrNo(7) /* A malloc() failed */
  38. ErrReadonly = ErrNo(8) /* Attempt to write a readonly database */
  39. ErrInterrupt = ErrNo(9) /* Operation terminated by sqlite3_interrupt() */
  40. ErrIoErr = ErrNo(10) /* Some kind of disk I/O error occurred */
  41. ErrCorrupt = ErrNo(11) /* The database disk image is malformed */
  42. ErrNotFound = ErrNo(12) /* Unknown opcode in sqlite3_file_control() */
  43. ErrFull = ErrNo(13) /* Insertion failed because database is full */
  44. ErrCantOpen = ErrNo(14) /* Unable to open the database file */
  45. ErrProtocol = ErrNo(15) /* Database lock protocol error */
  46. ErrEmpty = ErrNo(16) /* Database is empty */
  47. ErrSchema = ErrNo(17) /* The database schema changed */
  48. ErrTooBig = ErrNo(18) /* String or BLOB exceeds size limit */
  49. ErrConstraint = ErrNo(19) /* Abort due to constraint violation */
  50. ErrMismatch = ErrNo(20) /* Data type mismatch */
  51. ErrMisuse = ErrNo(21) /* Library used incorrectly */
  52. ErrNoLFS = ErrNo(22) /* Uses OS features not supported on host */
  53. ErrAuth = ErrNo(23) /* Authorization denied */
  54. ErrFormat = ErrNo(24) /* Auxiliary database format error */
  55. ErrRange = ErrNo(25) /* 2nd parameter to sqlite3_bind out of range */
  56. ErrNotADB = ErrNo(26) /* File opened that is not a database file */
  57. ErrNotice = ErrNo(27) /* Notifications from sqlite3_log() */
  58. ErrWarning = ErrNo(28) /* Warnings from sqlite3_log() */
  59. )
  60. // Error return error message from errno.
  61. func (err ErrNo) Error() string {
  62. return Error{Code: err}.Error()
  63. }
  64. // Extend return extended errno.
  65. func (err ErrNo) Extend(by int) ErrNoExtended {
  66. return ErrNoExtended(int(err) | (by << 8))
  67. }
  68. // Error return error message that is extended code.
  69. func (err ErrNoExtended) Error() string {
  70. return Error{Code: ErrNo(C.int(err) & ErrNoMask), ExtendedCode: err}.Error()
  71. }
  72. func (err Error) Error() string {
  73. var str string
  74. if err.err != "" {
  75. str = err.err
  76. } else {
  77. str = C.GoString(C.sqlite3_errstr(C.int(err.Code)))
  78. }
  79. if err.SystemErrno != 0 {
  80. str += ": " + err.SystemErrno.Error()
  81. }
  82. return str
  83. }
  84. // result codes from http://www.sqlite.org/c3ref/c_abort_rollback.html
  85. var (
  86. ErrIoErrRead = ErrIoErr.Extend(1)
  87. ErrIoErrShortRead = ErrIoErr.Extend(2)
  88. ErrIoErrWrite = ErrIoErr.Extend(3)
  89. ErrIoErrFsync = ErrIoErr.Extend(4)
  90. ErrIoErrDirFsync = ErrIoErr.Extend(5)
  91. ErrIoErrTruncate = ErrIoErr.Extend(6)
  92. ErrIoErrFstat = ErrIoErr.Extend(7)
  93. ErrIoErrUnlock = ErrIoErr.Extend(8)
  94. ErrIoErrRDlock = ErrIoErr.Extend(9)
  95. ErrIoErrDelete = ErrIoErr.Extend(10)
  96. ErrIoErrBlocked = ErrIoErr.Extend(11)
  97. ErrIoErrNoMem = ErrIoErr.Extend(12)
  98. ErrIoErrAccess = ErrIoErr.Extend(13)
  99. ErrIoErrCheckReservedLock = ErrIoErr.Extend(14)
  100. ErrIoErrLock = ErrIoErr.Extend(15)
  101. ErrIoErrClose = ErrIoErr.Extend(16)
  102. ErrIoErrDirClose = ErrIoErr.Extend(17)
  103. ErrIoErrSHMOpen = ErrIoErr.Extend(18)
  104. ErrIoErrSHMSize = ErrIoErr.Extend(19)
  105. ErrIoErrSHMLock = ErrIoErr.Extend(20)
  106. ErrIoErrSHMMap = ErrIoErr.Extend(21)
  107. ErrIoErrSeek = ErrIoErr.Extend(22)
  108. ErrIoErrDeleteNoent = ErrIoErr.Extend(23)
  109. ErrIoErrMMap = ErrIoErr.Extend(24)
  110. ErrIoErrGetTempPath = ErrIoErr.Extend(25)
  111. ErrIoErrConvPath = ErrIoErr.Extend(26)
  112. ErrLockedSharedCache = ErrLocked.Extend(1)
  113. ErrBusyRecovery = ErrBusy.Extend(1)
  114. ErrBusySnapshot = ErrBusy.Extend(2)
  115. ErrCantOpenNoTempDir = ErrCantOpen.Extend(1)
  116. ErrCantOpenIsDir = ErrCantOpen.Extend(2)
  117. ErrCantOpenFullPath = ErrCantOpen.Extend(3)
  118. ErrCantOpenConvPath = ErrCantOpen.Extend(4)
  119. ErrCorruptVTab = ErrCorrupt.Extend(1)
  120. ErrReadonlyRecovery = ErrReadonly.Extend(1)
  121. ErrReadonlyCantLock = ErrReadonly.Extend(2)
  122. ErrReadonlyRollback = ErrReadonly.Extend(3)
  123. ErrReadonlyDbMoved = ErrReadonly.Extend(4)
  124. ErrAbortRollback = ErrAbort.Extend(2)
  125. ErrConstraintCheck = ErrConstraint.Extend(1)
  126. ErrConstraintCommitHook = ErrConstraint.Extend(2)
  127. ErrConstraintForeignKey = ErrConstraint.Extend(3)
  128. ErrConstraintFunction = ErrConstraint.Extend(4)
  129. ErrConstraintNotNull = ErrConstraint.Extend(5)
  130. ErrConstraintPrimaryKey = ErrConstraint.Extend(6)
  131. ErrConstraintTrigger = ErrConstraint.Extend(7)
  132. ErrConstraintUnique = ErrConstraint.Extend(8)
  133. ErrConstraintVTab = ErrConstraint.Extend(9)
  134. ErrConstraintRowID = ErrConstraint.Extend(10)
  135. ErrNoticeRecoverWAL = ErrNotice.Extend(1)
  136. ErrNoticeRecoverRollback = ErrNotice.Extend(2)
  137. ErrWarningAutoIndex = ErrWarning.Extend(1)
  138. )