forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgreSQLError.swift
75 lines (61 loc) · 2.23 KB
/
PostgreSQLError.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import Debugging
/// Errors that can be thrown while working with PostgreSQL.
public struct PostgreSQLError: Debuggable {
/// See `Debuggable`.
public static let readableName = "PostgreSQL Error"
/// Error communicating with PostgreSQL wire-protocol
static func `protocol`(reason: String) -> PostgreSQLError {
return .init(identifier: "protocol", reason: reason)
}
/// Error communicating with PostgreSQL wire-protocol
static func decode<T>(_ type: T.Type, from data: PostgreSQLData) -> PostgreSQLError {
return .init(identifier: "decode", reason: "Could not decode \(T.self): \(data).")
}
/// See `Debuggable`.
public let identifier: String
/// See `Debuggable`.
public var reason: String
/// See `Debuggable`.
public var sourceLocation: SourceLocation
/// See `Debuggable`.
public var stackTrace: [String]
/// See `Debuggable`.
public var possibleCauses: [String]
/// See `Debuggable`.
public var suggestedFixes: [String]
/// Create a new `PostgreSQLError`.
public init(
identifier: String,
reason: String,
possibleCauses: [String] = [],
suggestedFixes: [String] = [],
file: String = #file,
function: String = #function,
line: UInt = #line,
column: UInt = #column
) {
self.identifier = identifier
self.reason = reason
self.sourceLocation = SourceLocation(file: file, function: function, line: line, column: column, range: nil)
self.stackTrace = PostgreSQLError.makeStackTrace()
self.possibleCauses = possibleCauses
self.suggestedFixes = suggestedFixes
}
}
/// Only includes the supplied closure in non-release builds.
internal func debugOnly(_ body: () -> Void) {
assert({ body(); return true }())
}
/// Logs a runtime warning.
internal func WARNING(_ string: @autoclosure () -> String) {
print("[WARNING] [PostgreSQL] \(string())")
}
/// Logs a runtime error.
internal func ERROR(_ string: @autoclosure () -> String) {
print("[Error] [PostgreSQL] \(string())")
}
func VERBOSE(_ string: @autoclosure () -> (String)) {
#if VERBOSE
print("[VERBOSE] [PostgreSQL] \(string())")
#endif
}