/
Abort.swift
106 lines (93 loc) 路 3.48 KB
/
Abort.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/// Default implementation of `AbortError`. You can use this as a convenient method for throwing
/// `AbortError`s without having to conform your own error-type to `AbortError`.
///
/// throw Abort(.badRequest, reason: "Something's not quite right...")
///
public struct Abort: AbortError {
/// Creates a redirecting `Abort` error.
///
/// throw Abort.redirect(to: "https://vapor.codes")"
///
/// Set type to '.permanently' to allow caching to automatically redirect from browsers.
/// Defaulting to non-permanent to prevent unexpected caching.
public static func redirect(to location: String, type: RedirectType = .normal) -> Abort {
var headers: HTTPHeaders = [:]
headers.replaceOrAdd(name: .location, value: location)
return .init(type.status, headers: headers)
}
/// See `Debuggable`
public var identifier: String
/// See `AbortError`
public var status: HTTPResponseStatus
/// See `AbortError`.
public var headers: HTTPHeaders
/// See `AbortError`
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]
/// See `Debuggable`
public var documentationLinks: [String]
/// See `Debuggable`
public var stackOverflowQuestions: [String]
/// See `Debuggable`
public var gitHubIssues: [String]
/// Create a new `Abort`, capturing current source location info.
public init(
_ status: HTTPResponseStatus,
headers: HTTPHeaders = [:],
reason: String? = nil,
identifier: String? = nil,
possibleCauses: [String] = [],
suggestedFixes: [String] = [],
documentationLinks: [String] = [],
stackOverflowQuestions: [String] = [],
gitHubIssues: [String] = [],
file: String = #file,
function: String = #function,
line: UInt = #line,
column: UInt = #column
) {
self.identifier = identifier ?? status.code.description
self.headers = headers
self.status = status
self.reason = reason ?? status.reasonPhrase
self.possibleCauses = possibleCauses
self.suggestedFixes = suggestedFixes
self.documentationLinks = documentationLinks
self.stackOverflowQuestions = stackOverflowQuestions
self.gitHubIssues = gitHubIssues
self.sourceLocation = SourceLocation(file: file, function: function, line: line, column: column, range: nil)
self.stackTrace = Abort.makeStackTrace()
}
/// Create a new `Abort` from an error conforming to `AbortError`,
/// capturing current source location info.
public init(
_ error: AbortError,
file: String = #file,
function: String = #function,
line: UInt = #line,
column: UInt = #column
) {
self.init(
error.status,
headers: error.headers,
reason: error.reason,
identifier: error.identifier,
possibleCauses: error.possibleCauses,
suggestedFixes: error.suggestedFixes,
documentationLinks: error.documentationLinks,
stackOverflowQuestions: error.stackOverflowQuestions,
gitHubIssues: error.gitHubIssues,
file: file,
function: function,
line: line,
column: column
)
}
}