1 module fdb.error;
2 
3 import
4     std.conv,
5     std.exception;
6 
7 import
8     fdb.fdb_c;
9 
10 // https://foundationdb.com/key-value-store/documentation/api-error-codes.html
11 enum FDBError : uint
12 {
13     SUCCESS                       =    0, // Success
14     OPERATION_FAILED              = 1000, // Operation failed
15     TIMED_OUT                     = 1004, // Operation timed out
16     PAST_VERSION                  = 1007, // Version no longer available
17     FUTURE_VERSION                = 1009, // Request for future version
18     NOT_COMMITTED                 = 1020, // Transaction not committed
19     COMMIT_UNKNOWN_RESULT         = 1021, // Transaction may or may not have
20                                           // committed
21     TRANSACTION_CANCELLED         = 1025, // Operation aborted because the
22                                           // transaction was cancelled
23     TRANSACTION_TIMED_OUT         = 1031, // Operation aborted because the
24                                           // transaction timed out
25     TOO_MANY_WATCHES              = 1032, // Too many watches are currently set
26     WATCHES_DISABLED              = 1034, // Disabling read your writes also
27                                           // disables watches
28     OPERATION_CANCELLED           = 1101, // Asynchronous operation cancelled
29     FUTURE_RELEASED               = 1102, // The future has been released
30     PLATFORM_ERROR                = 1500, // A platform error occurred
31     LARGE_ALLOC_FAILED            = 1501, // Large block allocation failed
32     PERFORMANCE_COUNTER_ERROR     = 1502, // QueryPerformanceCounter doesn’t
33                                           // work
34     IO_ERROR                      = 1510, // A disk i/o operation failed
35     FILE_NOT_FOUND                = 1511, // File not found
36     BIND_FAILED                   = 1512, // Unable to bind to network
37     FILE_NOT_READABLE             = 1513, // File could not be read from
38     FILE_NOT_WRITABLE             = 1514, // File could not be written to
39     NO_CLUSTER_FILE_FOUND         = 1515, // No cluster file found in current
40                                           // directory or default location
41     CLUSTER_FILE_TOO_LARGE        = 1516, // Cluster file too large to be read
42     CLIENT_INVALID_OPERATION      = 2000, // The client made an invalid API
43                                           // call
44     COMMIT_READ_INCOMPLETE        = 2002, // Commit with incomplete read
45     TEST_SPECIFICATION_INVALID    = 2003, // The test specification is invalid
46     KEY_OUTSIDE_LEGAL_RANGE       = 2004, // The specified key was outside the
47                                           // legal range
48     INVERTED_RANGE                = 2005, // The specified range has a begin
49                                           // key larger than the end key
50     INVALID_OPTION_VALUE          = 2006, // An invalid value was passed with
51                                           // the specified option
52     INVALID_OPTION                = 2007, // Option not valid in this context
53     NETWORK_NOT_SETUP             = 2008, // Action not possible before the
54                                           // network is configured
55     NETWORK_ALREADY_SETUP         = 2009, // Network can be configured only
56                                           // once
57     READ_VERSION_ALREADY_SET      = 2010, // Transaction already has a read
58                                           // version set
59     VERSION_INVALID               = 2011, // Version not valid
60     RANGE_LIMITS_INVALID          = 2012, // getRange limits not valid
61     INVALID_DATABASE_NAME         = 2013, // Database name not supported in
62                                           // this version
63     ATTRIBUTE_NOT_FOUND           = 2014, // Attribute not found in string
64     FUTURE_NOT_SET                = 2015, // The future has not been set
65     FUTURE_NOT_ERROR              = 2016, // The future is not an error
66     USED_DURING_COMMIT            = 2017, // An operation was issued while a
67                                           // commit was outstanding
68     INVALID_MUTATION_TYPE         = 2018, // An invalid atomic mutation type
69                                           // was issued
70     INCOMPATIBLE_PROTOCOL_VERSION = 2100, // Incompatible protocol version
71     TRANSACTION_TOO_LARGE         = 2101, // Transaction too large
72     KEY_TOO_LARGE                 = 2102, // Key too large
73     VALUE_TOO_LARGE               = 2103, // Value too large
74     CONNECTION_STRING_INVALID     = 2104, // Connection string invalid
75     ADDRESS_IN_USE                = 2105, // Local address in use
76     INVALID_LOCAL_ADDRESS         = 2106, // Invalid local address
77     TLS_ERROR                     = 2107, // TLS error
78     API_VERSION_UNSET             = 2200, // API version must be set
79     API_VERSION_ALREADY_SET       = 2201, // API version may be set only once
80     API_VERSION_INVALID           = 2202, // API version not valid
81     API_VERSION_NOT_SUPPORTED     = 2203, // API version not supported in this
82                                           // version or binding
83     EXACT_MODE_WITHOUT_LIMITS     = 2210, // EXACT streaming mode requires
84                                           // limits, but none were given
85     UNKNOWN_ERROR                 = 4000, // An unknown error occurred
86     INTERNAL_ERROR                = 4100  // An internal error occurred
87 }
88 
89 class FDBException : Exception
90 {
91     const fdb_error_t err;
92 
93     this(const fdb_error_t err)
94     {
95         auto msg = err.message;
96         super(msg, "", 0, null);
97         this.err = err;
98     }
99 }
100 
101 auto message(const fdb_error_t err)
102 {
103     return fdb_get_error(err).to!string;
104 }
105 
106 auto enforceError(const fdb_error_t err)
107 {
108     return enforce(err == FDBError.SUCCESS, err.toException);
109 }
110 
111 Exception toException(const fdb_error_t err)
112 {
113     return (err == FDBError.SUCCESS) ? null : new FDBException(err);
114 }