Debian Codegraph @

lmdb/error.go

raw symbols SCIP
1 package lmdb
2
3 /*
4 #include "lmdb.h"
5 */
6 import "C"
7
8 import (
9 "os"
10 "syscall"
11 )
12
13 // OpError is an error returned by the C API. Not all errors returned by
14 // lmdb-go have type OpError but typically they do. The Errno field will
15 // either have type Errno or syscall.Errno.
16 type OpError struct {
17 Op string
18 Errno error
19 }
20
21 // Error implements the error interface.
22 func (err *OpError) Error() string {
23 return err.Op + ": " + err.Errno.Error()
24 }
25
26 // The most common error codes do not need to be handled explicity. Errors can
27 // be checked through helper functions IsNotFound, IsMapFull, etc, Otherwise
28 // they should be checked using the IsErrno function instead of direct
29 // comparison because they will typically be wrapped with an OpError.
30 const (
31 // Error codes defined by LMDB. See the list of LMDB return codes for more
32 // information about each
33 //
34 // http://symas.com/mdb/doc/group__errors.html
35
36 KeyExist Errno = C.MDB_KEYEXIST
37 NotFound Errno = C.MDB_NOTFOUND
38 PageNotFound Errno = C.MDB_PAGE_NOTFOUND
39 Corrupted Errno = C.MDB_CORRUPTED
40 Panic Errno = C.MDB_PANIC
41 VersionMismatch Errno = C.MDB_VERSION_MISMATCH
42 Invalid Errno = C.MDB_INVALID
43 MapFull Errno = C.MDB_MAP_FULL
44 DBsFull Errno = C.MDB_DBS_FULL
45 ReadersFull Errno = C.MDB_READERS_FULL
46 TLSFull Errno = C.MDB_TLS_FULL
47 TxnFull Errno = C.MDB_TXN_FULL
48 CursorFull Errno = C.MDB_CURSOR_FULL
49 PageFull Errno = C.MDB_PAGE_FULL
50 MapResized Errno = C.MDB_MAP_RESIZED
51 Incompatible Errno = C.MDB_INCOMPATIBLE
52 BadRSlot Errno = C.MDB_BAD_RSLOT
53 BadTxn Errno = C.MDB_BAD_TXN
54 BadValSize Errno = C.MDB_BAD_VALSIZE
55 BadDBI Errno = C.MDB_BAD_DBI
56 )
57
58 // Errno is an error type that represents the (unique) errno values defined by
59 // LMDB. Other errno values (such as EINVAL) are represented with type
60 // syscall.Errno. On Windows, LMDB return codes are translated into portable
61 // syscall.Errno constants (e.g. syscall.EINVAL, syscall.EACCES, etc.).
62 //
63 // Most often helper functions such as IsNotFound may be used instead of
64 // dealing with Errno values directly.
65 //
66 // lmdb.IsNotFound(err)
67 // lmdb.IsErrno(err, lmdb.TxnFull)
68 // lmdb.IsErrnoSys(err, syscall.EINVAL)
69 // lmdb.IsErrnoFn(err, os.IsPermission)
70 type Errno C.int
71
72 // minimum and maximum values produced for the Errno type. syscall.Errnos of
73 // other values may still be produced.
74 const minErrno, maxErrno C.int = C.MDB_KEYEXIST, C.MDB_LAST_ERRCODE
75
76 func (e Errno) Error() string {
77 return C.GoString(C.mdb_strerror(C.int(e)))
78 }
79
80 // _operrno is for use by tests that can't import C
81 func _operrno(op string, ret int) error {
82 return operrno(op, C.int(ret))
83 }
84
85 // IsNotFound returns true if the key requested in Txn.Get or Cursor.Get does
86 // not exist or if the Cursor reached the end of the database without locating
87 // a value (EOF).
88 func IsNotFound(err error) bool {
89 return IsErrno(err, NotFound)
90 }
91
92 // IsNotExist returns true the path passed to the Env.Open method does not
93 // exist.
94 func IsNotExist(err error) bool {
95 return IsErrnoFn(err, os.IsNotExist)
96 }
97
98 // IsMapFull returns true if the environment map size has been reached.
99 func IsMapFull(err error) bool {
100 return IsErrno(err, MapFull)
101 }
102
103 // IsMapResized returns true if the environment has grown too large for the
104 // current map after being resized by another process.
105 func IsMapResized(err error) bool {
106 return IsErrno(err, MapResized)
107 }
108
109 // IsErrno returns true if err's errno is the given errno.
110 func IsErrno(err error, errno Errno) bool {
111 return IsErrnoFn(err, func(err error) bool { return err == errno })
112 }
113
114 // IsErrnoSys returns true if err's errno is the given errno.
115 func IsErrnoSys(err error, errno syscall.Errno) bool {
116 return IsErrnoFn(err, func(err error) bool { return err == errno })
117 }
118
119 // IsErrnoFn calls fn on the error underlying err and returns the result. If
120 // err is an *OpError then err.Errno is passed to fn. Otherwise err is passed
121 // directly to fn.
122 func IsErrnoFn(err error, fn func(error) bool) bool {
123 if err == nil {
124 return false
125 }
126 if err, ok := err.(*OpError); ok {
127 return fn(err.Errno)
128 }
129 return fn(err)
130 }