LiteCore
Couchbase Lite cross-platform core implementation
Loading...
Searching...
No Matches
c4Error.h
Go to the documentation of this file.
1//
2// c4Error.h
3//
4// Copyright 2021-Present Couchbase, Inc.
5//
6// Use of this software is governed by the Business Source License included
7// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
8// in that file, in accordance with the Business Source License, use of this
9// software will be governed by the Apache License, Version 2.0, included in
10// the file licenses/APL2.txt.
11//
12
13#pragma once
14#include "c4Compat.h"
15#include "fleece/FLSlice.h"
16
17#ifdef __cplusplus
18# include "fleece/slice.hh"
19# include <exception>
20# include <string>
21# include <cstdarg>
22# include <cstdint>
23#else
24# include <stdarg.h>
25# include <stdint.h>
26#endif
27
30
31
36// (These are identical to the internal C++ error::Domain enum values.)
37typedef C4_ENUM(uint8_t, C4ErrorDomain){
38 LiteCoreDomain = 1, // code is a Couchbase Lite Core error code (see below)
39 POSIXDomain, // code is an errno
40 SQLiteDomain, // code is a SQLite error; see "sqlite3.h"
41 FleeceDomain, // code is a Fleece error; see "FleeceException.h"
42 NetworkDomain, // code is a network error; see enum C4NetworkErrorCode, below
43 WebSocketDomain, // code is a WebSocket close code (1000...1015) or HTTP error (300..599)
44 MbedTLSDomain, // code is an mbedTLS error; see "mbedtls/error.h"
45
47
48
49// LiteCoreDomain error codes:
50// (These are identical to the internal C++ error::LiteCoreError enum values.)
51// clang-format off
52typedef C4_ENUM(int32_t, C4ErrorCode){
53 kC4ErrorAssertionFailed = 1, // Internal assertion failure
54 kC4ErrorUnimplemented, // Oops, an unimplemented API call
55 kC4ErrorUnsupportedEncryption, // Unsupported encryption algorithm
56 kC4ErrorBadRevisionID, // Invalid revision ID syntax
57 kC4ErrorCorruptRevisionData, // Revision contains corrupted/unreadable data
58 kC4ErrorNotOpen, // Database/KeyStore/index is not open
59 kC4ErrorNotFound, // Document not found
60 kC4ErrorConflict, // Document update conflict
61 kC4ErrorInvalidParameter, // Invalid function parameter or struct value
63 /*10*/ // Internal unexpected C++ exception
64 kC4ErrorCantOpenFile, // Database file can't be opened; may not exist
65 kC4ErrorIOError, // File I/O error
66 kC4ErrorMemoryError, // Memory allocation failed (out of memory?)
67 kC4ErrorNotWriteable, // File is not writeable
68 kC4ErrorCorruptData, // Data is corrupted
69 kC4ErrorBusy, // Database is busy/locked
70 kC4ErrorNotInTransaction, // Function must be called while in a transaction
71 kC4ErrorTransactionNotClosed, // Database can't be closed while a transaction is open
72 kC4ErrorUnsupported, // Operation not supported in this database
74 /*20*/ // File is not a database, or encryption key is wrong
75 kC4ErrorWrongFormat, // Database exists but not in the format/storage requested
76 kC4ErrorCrypto, // Encryption/decryption error
77 kC4ErrorInvalidQuery, // Invalid query
78 kC4ErrorMissingIndex, // No such index, or query requires a nonexistent index
79 kC4ErrorInvalidQueryParam, // Unknown query param name, or param number out of range
80 kC4ErrorRemoteError, // Unknown error from remote server
81 kC4ErrorDatabaseTooOld, // Database file format is older than what I can open
82 kC4ErrorDatabaseTooNew, // Database file format is newer than what I can open
83 kC4ErrorBadDocID, // Invalid document ID
85 /*30*/ // DB can't be upgraded (might be unsupported dev version)
86 kC4ErrorDeltaBaseUnknown, // Replicator can't apply delta: base revision body is missing
87 kC4ErrorCorruptDelta, // Replicator can't apply delta: delta data invalid
89// clang-format on
90
96// (These are identical to the internal C++ NetworkError enum values in WebSocketInterface.hh.)
97typedef C4_ENUM(int32_t, C4NetworkErrorCode){
98 kC4NetErrDNSFailure = 1, // DNS lookup failed [retryable]
99 kC4NetErrUnknownHost, // DNS server doesn't know the hostname [retryable]
100 kC4NetErrTimeout, // Connection timeout [ETIMEDOUT, retryable]
101 kC4NetErrInvalidURL, // Invalid URL
102 kC4NetErrTooManyRedirects, // HTTP redirect loop
103 kC4NetErrTLSHandshakeFailed, // TLS handshake failed, for reasons other than below
104 kC4NetErrTLSCertExpired, // Peer's cert has expired
105 kC4NetErrTLSCertUntrusted, // Peer's cert isn't trusted for other reason
106 kC4NetErrTLSCertRequiredByPeer, // Peer (server) requires me to provide a (client) cert
107 kC4NetErrTLSCertRejectedByPeer, // Peer says my cert is invalid or unauthorized
108 kC4NetErrTLSCertUnknownRoot, // Self-signed cert, or unknown anchor cert
109 kC4NetErrInvalidRedirect, // Attempted redirect to invalid replication endpoint
110 kC4NetErrUnknown, // Unknown error
111 kC4NetErrTLSCertRevoked, // Peer's cert has been revoked
112 kC4NetErrTLSCertNameMismatch, // Peer's cert's Common Name doesn't match hostname
113 kC4NetErrNetworkReset, // The network subsystem was reset [ENETRESET, retryable]
114 kC4NetErrConnectionAborted, // The connection was aborted by the OS [ECONNABORTED, retryable]
115 kC4NetErrConnectionReset, // The connection was reset by the other side [ECONNRESET, retryable]
116 kC4NetErrConnectionRefused, // The other side refused the connection [ECONNREFUSED, retryable]
117 kC4NetErrNetworkDown, // The network subsystem is not functioning [ENETDOWN, retryable]
118 kC4NetErrNetworkUnreachable, // There is no usable network at the moment [ENETUNREACH, retryable]
119 kC4NetErrNotConnected, // The socket in question is no longer connected [ENOTCONN, retryable]
120 kC4NetErrHostDown, // The other side reports it is down [EHOSTDOWN, retryable]
121 kC4NetErrHostUnreachable, // There is no network path to the host [EHOSTUNREACH, retryable]
122 kC4NetErrAddressNotAvailable, // The address in question is already being used [EADDRNOTAVAIL, retryable]
123 kC4NetErrBrokenPipe, // Broken pipe [EPIPE, retryable]
124 kC4NetErrUnknownInterface, // The specified network interface is not valid or unknown.
126
133typedef struct C4Error {
134 C4ErrorDomain domain; // Domain of error (LiteCore, POSIX, SQLite, ...)
135 int code; // Error code. Domain-specific, except 0 is ALWAYS "none".
136 unsigned internal_info; // No user-serviceable parts inside. Do not touch.
137
138#ifdef __cplusplus
139 // C4Error C++ API:
140 NODISCARD static C4Error make(C4ErrorDomain, int code, fleece::slice message = {});
141 NODISCARD static C4Error printf(C4ErrorDomain, int code, const char* format, ...) __printflike(3, 4);
142 NODISCARD static C4Error vprintf(C4ErrorDomain, int code, const char* format, va_list args) __printflike(3, 0);
143 static void set(C4Error* C4NULLABLE, C4ErrorDomain, int code, const char* format = nullptr, ...) __printflike(4, 5);
144
145 static void set(C4ErrorDomain domain, int code, fleece::slice message, C4Error* C4NULLABLE outError) {
146 if ( outError ) *outError = make(domain, code, message);
147 }
148
149 NODISCARD static C4Error fromException(const std::exception& e) noexcept;
150 NODISCARD static C4Error fromCurrentException() noexcept;
151
152 static void fromException(const std::exception& e, C4Error* C4NULLABLE outError) noexcept {
153 if ( outError ) *outError = fromException(e);
154 }
155
156 static void fromCurrentException(C4Error* C4NULLABLE outError) noexcept {
157 if ( outError ) *outError = fromCurrentException();
158 }
159
160 static void warnCurrentException(const char* inFunction) noexcept;
161
162 [[noreturn]] static void raise(C4ErrorDomain, int code, const char* C4NULLABLE format = nullptr, ...)
163 __printflike(3, 4);
164
165 [[noreturn]] static void raise(C4Error e) { e.raise(); }
166
167 static void setCaptureBacktraces(bool) noexcept;
168 static bool getCaptureBacktraces() noexcept;
169
170 [[noreturn]] void raise() const;
171
172 bool operator==(const C4Error& b) const { return code == b.code && (code == 0 || domain == b.domain); }
173
174 bool operator!=(const C4Error& b) const { return !(*this == b); }
175
176 explicit operator bool() const { return code != 0; }
177
178 bool operator!() const { return code == 0; }
179
180 [[nodiscard]] std::string message() const;
181 [[nodiscard]] std::string description() const;
182 [[nodiscard]] std::string backtrace() const;
183
184 [[nodiscard]] bool mayBeTransient() const noexcept;
185 [[nodiscard]] bool mayBeNetworkDependent() const noexcept;
186#endif
187} C4Error;
188
189#ifdef __cplusplus
190static constexpr C4Error kC4NoError = {};
191#else
192# define kC4NoError ((C4Error){})
193#endif
194
195// C4Error C API:
196
197
201
205
213CBL_CORE_API char* c4error_getDescriptionC(C4Error error, char* outBuffer, size_t bufferSize) C4API;
214
219
221
225
226
229
232NODISCARD CBL_CORE_API C4Error c4error_printf(C4ErrorDomain domain, int code, const char* format, ...) C4API
233 __printflike(3, 4);
234
236NODISCARD CBL_CORE_API C4Error c4error_vprintf(C4ErrorDomain domain, int code, const char* format, va_list args) C4API
237 __printflike(3, 0);
238
241CBL_CORE_API void c4error_return(C4ErrorDomain domain, int code, FLString message, C4Error* C4NULLABLE outError) C4API;
242
243
247
251
252
#define NODISCARD
Definition CompilerSupport.h:59
#define CBL_CORE_API
Definition c4Compat.h:134
#define C4NULLABLE
Definition c4Compat.h:38
#define C4_ENUM(_type, _name)
Definition c4Compat.h:62
#define C4API
Definition c4Compat.h:114
#define C4_ASSUME_NONNULL_BEGIN
Definition c4Compat.h:36
#define C4API_END_DECLS
Definition c4Compat.h:116
#define __printflike(fmtarg, firstvararg)
Definition c4Compat.h:143
#define C4API_BEGIN_DECLS
Definition c4Compat.h:115
#define C4_ASSUME_NONNULL_END
Definition c4Compat.h:37
CBL_CORE_API FLStringResult c4error_getBacktrace(C4Error error)
Returns the stack backtrace, if any, associated with a C4Error.
CBL_CORE_API FLSliceResult c4error_getDescription(C4Error error)
Returns a description of an error, including the domain and code as well as the message.
CBL_CORE_API void c4error_setCaptureBacktraces(bool)
If set to true, then when a C4Error is created the current thread's stack backtrace will be captured ...
#define kC4NoError
Definition c4Error.h:192
CBL_CORE_API char * c4error_getDescriptionC(C4Error error, char *outBuffer, size_t bufferSize)
Returns a description of an error, including the domain and code as well as the message.
C4NetworkErrorCode
Definition c4Error.h:97
NODISCARD CBL_CORE_API C4Error c4error_make(C4ErrorDomain domain, int code, FLString message)
Creates a C4Error struct with the given domain and code, and associates the message with it.
CBL_CORE_API bool c4error_mayBeNetworkDependent(C4Error err)
Returns true if this error might go away when the network environment changes, i.e.
CBL_CORE_API bool c4error_getCaptureBacktraces(void)
C4ErrorDomain
Definition c4Error.h:37
C4ErrorCode
Definition c4Error.h:52
NODISCARD CBL_CORE_API C4Error NODISCARD CBL_CORE_API C4Error CBL_CORE_API void c4error_return(C4ErrorDomain domain, int code, FLString message, C4Error *outError)
Creates and stores a C4Error in *outError, if not NULL.
NODISCARD CBL_CORE_API C4Error NODISCARD CBL_CORE_API C4Error c4error_vprintf(C4ErrorDomain domain, int code, const char *format, va_list args) __printflike(3
Same as c4error_printf, but with a premade va_list.
CBL_CORE_API FLStringResult c4error_getMessage(C4Error error)
Returns an error message describing a C4Error.
NODISCARD CBL_CORE_API C4Error c4error_printf(C4ErrorDomain domain, int code, const char *format,...) __printflike(3
Creates a C4Error struct with the given domain and code, formats the message as with printf,...
CBL_CORE_API bool c4error_mayBeTransient(C4Error err)
Returns true if this is a network error that may be transient, i.e.
@ kC4NetErrTLSCertExpired
Definition c4Error.h:104
@ kC4NetErrTLSCertUnknownRoot
Definition c4Error.h:108
@ kC4NetErrAddressNotAvailable
Definition c4Error.h:122
@ kC4NetErrUnknown
Definition c4Error.h:110
@ kC4NetErrConnectionAborted
Definition c4Error.h:114
@ kC4NetErrNetworkUnreachable
Definition c4Error.h:118
@ kC4NetErrHostDown
Definition c4Error.h:120
@ kC4NetErrTooManyRedirects
Definition c4Error.h:102
@ kC4NetErrConnectionRefused
Definition c4Error.h:116
@ kC4NetErrTimeout
Definition c4Error.h:100
@ kC4NetErrDNSFailure
Definition c4Error.h:98
@ kC4NetErrNotConnected
Definition c4Error.h:119
@ kC4NetErrTLSCertRejectedByPeer
Definition c4Error.h:107
@ kC4NetErrTLSCertNameMismatch
Definition c4Error.h:112
@ kC4NetErrNetworkReset
Definition c4Error.h:113
@ kC4NetErrInvalidRedirect
Definition c4Error.h:109
@ kC4NetErrHostUnreachable
Definition c4Error.h:121
@ kC4NetErrConnectionReset
Definition c4Error.h:115
@ kC4NetErrTLSCertUntrusted
Definition c4Error.h:105
@ kC4NetErrBrokenPipe
Definition c4Error.h:123
@ kC4NetErrNetworkDown
Definition c4Error.h:117
@ kC4NetErrTLSCertRequiredByPeer
Definition c4Error.h:106
@ kC4NetErrUnknownHost
Definition c4Error.h:99
@ kC4NumNetErrorCodesPlus1
Definition c4Error.h:125
@ kC4NetErrTLSCertRevoked
Definition c4Error.h:111
@ kC4NetErrTLSHandshakeFailed
Definition c4Error.h:103
@ kC4NetErrUnknownInterface
Definition c4Error.h:124
@ kC4NetErrInvalidURL
Definition c4Error.h:101
@ WebSocketDomain
Definition c4Error.h:43
@ POSIXDomain
Definition c4Error.h:39
@ MbedTLSDomain
Definition c4Error.h:44
@ NetworkDomain
Definition c4Error.h:42
@ kC4MaxErrorDomainPlus1
Definition c4Error.h:46
@ FleeceDomain
Definition c4Error.h:41
@ LiteCoreDomain
Definition c4Error.h:38
@ SQLiteDomain
Definition c4Error.h:40
@ kC4ErrorCrypto
Definition c4Error.h:76
@ kC4ErrorInvalidQuery
Definition c4Error.h:77
@ kC4ErrorNotADatabaseFile
Definition c4Error.h:73
@ kC4ErrorBadRevisionID
Definition c4Error.h:56
@ kC4ErrorIOError
Definition c4Error.h:65
@ kC4ErrorUnsupported
Definition c4Error.h:72
@ kC4ErrorNotOpen
Definition c4Error.h:58
@ kC4ErrorUnimplemented
Definition c4Error.h:54
@ kC4ErrorBadDocID
Definition c4Error.h:83
@ kC4ErrorNotFound
Definition c4Error.h:59
@ kC4ErrorInvalidParameter
Definition c4Error.h:61
@ kC4ErrorMissingIndex
Definition c4Error.h:78
@ kC4NumErrorCodesPlus1
Definition c4Error.h:88
@ kC4ErrorBusy
Definition c4Error.h:69
@ kC4ErrorUnsupportedEncryption
Definition c4Error.h:55
@ kC4ErrorNotWriteable
Definition c4Error.h:67
@ kC4ErrorMemoryError
Definition c4Error.h:66
@ kC4ErrorWrongFormat
Definition c4Error.h:75
@ kC4ErrorNotInTransaction
Definition c4Error.h:70
@ kC4ErrorUnexpectedError
Definition c4Error.h:62
@ kC4ErrorRemoteError
Definition c4Error.h:80
@ kC4ErrorDatabaseTooNew
Definition c4Error.h:82
@ kC4ErrorDeltaBaseUnknown
Definition c4Error.h:86
@ kC4ErrorTransactionNotClosed
Definition c4Error.h:71
@ kC4ErrorCantOpenFile
Definition c4Error.h:64
@ kC4ErrorInvalidQueryParam
Definition c4Error.h:79
@ kC4ErrorConflict
Definition c4Error.h:60
@ kC4ErrorDatabaseTooOld
Definition c4Error.h:81
@ kC4ErrorCorruptDelta
Definition c4Error.h:87
@ kC4ErrorCorruptData
Definition c4Error.h:68
@ kC4ErrorCorruptRevisionData
Definition c4Error.h:57
@ kC4ErrorCantUpgradeDatabase
Definition c4Error.h:84
@ kC4ErrorAssertionFailed
Definition c4Error.h:53
An error value.
Definition c4Error.h:133
C4ErrorDomain domain
Definition c4Error.h:134
unsigned internal_info
Definition c4Error.h:136
int code
Definition c4Error.h:135
A simple reference to a block of memory.
Definition FLSlice.h:45
A heap-allocated block of memory returned from an API call.
Definition FLSlice.h:63