error.go raw

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