Skip to content

Commit 72b4743

Browse files
committed
update docs
1 parent ea8a0aa commit 72b4743

File tree

4 files changed

+147
-59
lines changed

4 files changed

+147
-59
lines changed

README.md

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -64,34 +64,11 @@ if let error {
6464
}
6565
```
6666

67-
### Basic Synchronous Usage
68-
69-
```swift
70-
// Returns (error, result, success)
71-
let (error, result, success) = mightFail {
72-
return "Success"
73-
}
74-
75-
// Check success
76-
if success {
77-
print(result) // "Success"
78-
}
79-
```
80-
81-
### Simplified Return Type
82-
83-
```swift
84-
// Returns just (error, result)
85-
let (error, result) = mightFail {
86-
return 42
87-
}
88-
89-
print(result) // 42
90-
print(error) // nil
91-
```
9267

9368
### Handling Errors
9469

70+
Like I said, always guard. So the success case is at the bottom of the function.
71+
9572
#### Traditional Error Handling:
9673

9774
```swift
@@ -135,6 +112,40 @@ guard success else {
135112
print("Success! Yum.")
136113
```
137114

115+
### Basic Synchronous Usage
116+
117+
```swift
118+
// Returns (error, result, success)
119+
let (error, result, success) = mightFail {
120+
return "Success"
121+
}
122+
123+
// Check success
124+
guard success else {
125+
print(error)
126+
return
127+
}
128+
129+
print(result) // "Success"
130+
```
131+
132+
### Simplified Return Type
133+
134+
```swift
135+
// Returns just (error, result)
136+
let (error, result) = mightFail {
137+
return 42
138+
}
139+
140+
guard let result else {
141+
print(error)
142+
return
143+
}
144+
145+
print(result) // 42
146+
```
147+
148+
138149
### Async Support
139150

140151
```swift
@@ -195,22 +206,35 @@ for deleteResult in deleteResults.filter({ $0.success == true }) {
195206

196207
### Optional Values
197208

198-
MightFail handles optional values gracefully:
209+
If you pass an optional value to mightFail, the first guard is for the error check, you'll need a second guard to see if you have a value.
199210

200211
```swift
201212
func returnOptional() throws -> String? {
202213
return nil
203214
}
204215

205-
let (error, result, success) = mightFail {
216+
let (error, result) = mightFail {
206217
try returnOptional()
207218
}
208219

209-
// success will be true
210-
// result will be nil
211-
// error will be nil
220+
// result is String??
221+
guard let result else {
222+
// there was an error, handle it
223+
print(error)
224+
return
225+
}
226+
227+
guard let result else {
228+
// no error, but result is nil, handle it
229+
print("No result")
230+
return
231+
}
232+
233+
print(result) // "Success"
212234
```
213235

236+
---
237+
214238
# do, try, catch is bad
215239

216240
I think throwing exceptions is nice, I like that an exception breaks control flow and I like exception propogation. The only thing I don't like catching exceptions.

Sources/MightFail/MightFail.docc/GettingStarted.md

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Add MightFail as a dependency to your `Package.swift`:
1010

1111
```swift
1212
dependencies: [
13-
.package(url: "https://github.com/might-fail/swift.git", from: "0.1.0")
13+
.package(url: "https://github.com/might-fail/swift.git", from: "0.2.1")
1414
]
1515
```
1616

@@ -65,9 +65,12 @@ let (error, result, success) = mightFail {
6565
}
6666

6767
// Check success
68-
if success {
69-
print(result) // "Success"
68+
guard success else {
69+
print(error)
70+
return
7071
}
72+
73+
print(result) // "Success"
7174
```
7275

7376
### Simplified Return Type
@@ -78,8 +81,12 @@ let (error, result) = mightFail {
7881
return 42
7982
}
8083

84+
guard let result else {
85+
print(error)
86+
return
87+
}
88+
8189
print(result) // 42
82-
print(error) // nil
8390
```
8491

8592
### Handling Errors
@@ -153,12 +160,13 @@ let results = mightFail([
153160
])
154161

155162
// Check results
156-
results.forEach { (error, result) in
157-
guard let result else {
158-
print("Error: \(error)")
159-
return
163+
for (index, (error, result, success)) in results.enumerated() {
164+
guard success else {
165+
print("Operation \(index + 1) failed with error: \(error)")
166+
continue
160167
}
161-
print("Result: \(result)")
168+
169+
print("Operation \(index + 1) succeeded with result: \(result!)")
162170
}
163171
```
164172

@@ -185,18 +193,29 @@ for deleteResult in deleteResults.filter({ $0.success == true }) {
185193

186194
### Optional Values
187195

188-
MightFail handles optional values gracefully:
196+
If you pass an optional value to mightFail, the first guard is for the error check, you'll need a second guard to see if you have a value.
189197

190198
```swift
191199
func returnOptional() throws -> String? {
192200
return nil
193201
}
194202

195-
let (error, result, success) = mightFail {
203+
let (error, result) = mightFail {
196204
try returnOptional()
197205
}
198206

199-
// success will be true
200-
// result will be nil
201-
// error will be nil
207+
// result is String??
208+
guard let result else {
209+
// there was an error, handle it
210+
print(error)
211+
return
212+
}
213+
214+
guard let result else {
215+
// no error, but result is nil, handle it
216+
print("No result")
217+
return
218+
}
219+
220+
print(result) // "Success"
202221
```

Sources/MightFail/MightFail.docc/MightFail.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@ A Swift package for handling potentially failing operations with ease.
44

55
## Overview
66

7-
MightFail provides a set of functions to handle operations that might throw errors, allowing you to handle success and failure cases without using do-catch blocks.
7+
MightFail provides a set of functions to handle operations that might throw errors, allowing you to handle success and failure cases without using do-catch blocks. It works with both synchronous and asynchronous code.
8+
9+
Visit the [GitHub repository](https://github.com/might-fail/swift.git) for more information, issues, and contributions.
810

911
## Topics
1012

1113
### Essentials
1214

1315
- <doc:GettingStarted>
16+
- <doc:DoTryCatchIsBad>
17+
- <doc:OtherLanguages>
1418

1519
### Functions
1620

17-
<!-- - `mightFail(_:)-5owtv`
18-
- `mightFail(_:)-1gr5o`
19-
- `mightFail(_:)-7bt4a`
20-
- `mightFail(_:)-1m6lj`
21-
- `mightFail(_:)-2s1e4`
22-
- `mightFail(_:)-8avrp` -->
21+
- ``mightFail(_:)-90d29`` - Sync with success
22+
- ``mightFail(_:)-cq0w`` - Async with success
23+
- ``mightFail(_:)-51klq`` - Sync without success
24+
- ``mightFail(_:)-2mkjp`` - Async without success
25+
- ``mightFail(_:)-1rji`` - Multiple sync operations
26+
- ``mightFail(_:)-4qoxi`` - Multiple async operations

Tests/MightFailTests/MightFailTests.swift

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,21 @@ enum TestError: Error, Equatable {
219219
try returnStringOptional()
220220
}
221221

222-
#expect(success)
223-
#expect(result == nil)
224-
#expect(error == nil)
222+
guard let result else {
223+
print("failed and there is an error")
224+
throw TestError.withMessage("Failed operation")
225+
}
226+
227+
guard let result else {
228+
// this is just checking if there's a value
229+
// should have no value in this case
230+
#expect(success)
231+
#expect(result == nil)
232+
#expect(error != nil)
233+
return
234+
}
235+
236+
throw TestError.withMessage("Failed operation \(result)")
225237
}
226238

227239
@Test("Handling optional values")
@@ -233,9 +245,18 @@ enum TestError: Error, Equatable {
233245
try returnStringOptional()
234246
}
235247

248+
guard let result else {
249+
print("failed and there is an error \(error)")
250+
throw TestError.withMessage("Failed operation")
251+
}
252+
guard let result else {
253+
// just checking if there's a value
254+
throw TestError.withMessage("Failed operation")
255+
}
256+
236257
#expect(success)
237258
#expect(result == "Hello")
238-
#expect(error == nil)
259+
#expect(error != nil)
239260
}
240261

241262
// Add these tests in the "Asynchronous Tests" section
@@ -247,10 +268,21 @@ enum TestError: Error, Equatable {
247268
let (error, result, success) = await mightFail {
248269
try await returnStringOptional()
249270
}
271+
guard let result else {
272+
print("failed and there is an error")
273+
throw TestError.withMessage("Failed operation")
274+
}
275+
guard let result else {
276+
// this is just checking if there's a value
277+
// should have no value in this case
278+
#expect(success)
279+
#expect(result == nil)
280+
#expect(error != nil)
281+
return
282+
}
283+
284+
throw TestError.withMessage("Failed operation \(result)")
250285

251-
#expect(success)
252-
#expect(result == nil)
253-
#expect(error == nil)
254286
}
255287

256288
@Test("Handling async optional values")
@@ -262,9 +294,18 @@ enum TestError: Error, Equatable {
262294
try await returnStringOptional()
263295
}
264296

297+
guard let result else {
298+
print("failed and there is an error \(error)")
299+
throw TestError.withMessage("Failed operation")
300+
}
301+
guard let result else {
302+
// just checking if there's a value
303+
throw TestError.withMessage("Failed operation")
304+
}
305+
265306
#expect(success)
266307
#expect(result == "Hello")
267-
#expect(error == nil)
308+
#expect(error != nil)
268309
}
269310

270311
}

0 commit comments

Comments
 (0)