Skip to content

Commit b137dc2

Browse files
committed
feat: add getOrThrow method
1 parent 76cba75 commit b137dc2

File tree

7 files changed

+105
-24
lines changed

7 files changed

+105
-24
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
## 5.0.0 05/10/2022
1+
## 5.1.0 07/17/2023
2+
3+
* Adds [getOrThrow] method
4+
5+
## 5.0.0 05/10/2023
26

37
* Supports new dart 3 features
48

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,20 @@ void main() {
124124
});
125125
```
126126

127+
#### Handling the Result with `getOrThrow`
128+
129+
You may use the `getOrThrow` to get the value when you're sure that the result was a `Success`.
130+
Be aware that accessing this method when the result is actually an `Error` will throw a `SuccessResultNotFoundException`.
131+
132+
```dart
133+
final result = getSomethingPretty();
134+
135+
if (result.isSuccess()) {
136+
final mySuccessResult = result.getOrThrow();
137+
}
138+
```
139+
140+
127141
#### Handling the Result with `tryGetSuccess`
128142

129143
```dart

example/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,5 @@ ResultOf<String, Exception> _getName({bool forceError = false}) {
5656
if (forceError) {
5757
return Result.error(Exception("Error forced"));
5858
}
59-
return Result.success("Higor");
59+
return const Result.success("Higor");
6060
}

lib/src/result.dart

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,21 @@ sealed class Result<S, E> {
1515
const Result();
1616

1717
/// Build a [Result] that returns a [Success].
18-
factory Result.success(S s) => Success(s);
18+
const factory Result.success(S s) = Success;
1919

2020
/// Build a [Result] that returns a [Error].
21-
factory Result.error(E e) => Error(e);
21+
const factory Result.error(E e) = Error;
22+
23+
/// Get the [S] value IF it is a [Success].
24+
///
25+
/// It may throw [SuccessResultNotFoundException] if this result is actually
26+
/// an [Error] of [E].
27+
///
28+
/// Make sure to use [isSuccess] or `if (result case Success())` before
29+
/// accessing this method.
30+
///
31+
/// You can also use [tryGetSuccess] if you're unsure of the possible result.
32+
S getOrThrow();
2233

2334
/// Returns the value of [S] if any.
2435
S? tryGetSuccess();
@@ -97,6 +108,9 @@ final class Success<S, E> extends Result<S, E> {
97108
/// Success value
98109
S get success => _success;
99110

111+
@override
112+
S getOrThrow() => _success;
113+
100114
@override
101115
E? tryGetError() => null;
102116

@@ -153,6 +167,9 @@ final class Error<S, E> extends Result<S, E> {
153167
) =>
154168
whenError(_error);
155169

170+
@override
171+
S getOrThrow() => throw SuccessResultNotFoundException();
172+
156173
@override
157174
E tryGetError() => _error;
158175

@@ -165,3 +182,18 @@ final class Error<S, E> extends Result<S, E> {
165182
@override
166183
R? whenSuccess<R>(R Function(S success) whenSuccess) => null;
167184
}
185+
186+
/// Thrown when getting the [S] type when none is available.
187+
final class SuccessResultNotFoundException<S, E> implements Exception {
188+
const SuccessResultNotFoundException();
189+
190+
@override
191+
String toString() {
192+
return '''
193+
Tried to get the success value of [$S], but none was found.
194+
Make sure you're checking for `isSuccess` before trying to get it through
195+
`getOrThrow`. You can also use `tryGetSuccess` if you're unsure or
196+
`if (result case Success())`
197+
''';
198+
}
199+
}

pubspec.lock

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ packages:
55
dependency: transitive
66
description:
77
name: _fe_analyzer_shared
8-
sha256: "3444216bfd127af50bbe4862d8843ed44db946dd933554f0d7285e89f10e28ac"
8+
sha256: "0816708f5fbcacca324d811297153fe3c8e047beb5c6752e12292d2974c17045"
99
url: "https://pub.dev"
1010
source: hosted
11-
version: "50.0.0"
11+
version: "62.0.0"
1212
analyzer:
1313
dependency: transitive
1414
description:
1515
name: analyzer
16-
sha256: "68796c31f510c8455a06fed75fc97d8e5ad04d324a830322ab3efc9feb6201c1"
16+
sha256: "21862995c9932cd082f89d72ae5f5e2c110d1a0204ad06e4ebaee8307b76b834"
1717
url: "https://pub.dev"
1818
source: hosted
19-
version: "5.2.0"
19+
version: "6.0.0"
2020
args:
2121
dependency: transitive
2222
description:
@@ -53,10 +53,10 @@ packages:
5353
dependency: transitive
5454
description:
5555
name: collection
56-
sha256: "6d4193120997ecfd09acf0e313f13dc122b119e5eca87ef57a7d065ec9183762"
56+
sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687
5757
url: "https://pub.dev"
5858
source: hosted
59-
version: "1.15.0"
59+
version: "1.17.2"
6060
convert:
6161
dependency: transitive
6262
description:
@@ -81,6 +81,14 @@ packages:
8181
url: "https://pub.dev"
8282
source: hosted
8383
version: "3.0.0"
84+
dart_internal:
85+
dependency: transitive
86+
description:
87+
name: dart_internal
88+
sha256: dae3976f383beddcfcd07ad5291a422df2c8c0a8a03c52cda63ac7b4f26e0f4e
89+
url: "https://pub.dev"
90+
source: hosted
91+
version: "0.2.8"
8492
file:
8593
dependency: transitive
8694
description:
@@ -141,10 +149,10 @@ packages:
141149
dependency: "direct dev"
142150
description:
143151
name: lints
144-
sha256: "6b0206b0bf4f04961fc5438198ccb3a885685cd67d4d4a32cc20ad7f8adbe015"
152+
sha256: "0a217c6c989d21039f1498c3ed9f3ed71b354e69873f13a8dfc3c9fe76f1b452"
145153
url: "https://pub.dev"
146154
source: hosted
147-
version: "2.1.0"
155+
version: "2.1.1"
148156
logging:
149157
dependency: transitive
150158
description:
@@ -157,10 +165,10 @@ packages:
157165
dependency: transitive
158166
description:
159167
name: matcher
160-
sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb"
168+
sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e"
161169
url: "https://pub.dev"
162170
source: hosted
163-
version: "0.12.15"
171+
version: "0.12.16"
164172
meta:
165173
dependency: transitive
166174
description:
@@ -317,26 +325,26 @@ packages:
317325
dependency: "direct dev"
318326
description:
319327
name: test
320-
sha256: "4f92f103ef63b1bbac6f4bd1930624fca81b2574464482512c4f0896319be575"
328+
sha256: "67ec5684c7a19b2aba91d2831f3d305a6fd8e1504629c5818f8d64478abf4f38"
321329
url: "https://pub.dev"
322330
source: hosted
323-
version: "1.24.2"
331+
version: "1.24.4"
324332
test_api:
325333
dependency: transitive
326334
description:
327335
name: test_api
328-
sha256: daadc9baabec998b062c9091525aa95786508b1c48e9c30f1f891b8bf6ff2e64
336+
sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b"
329337
url: "https://pub.dev"
330338
source: hosted
331-
version: "0.5.2"
339+
version: "0.6.1"
332340
test_core:
333341
dependency: transitive
334342
description:
335343
name: test_core
336-
sha256: "3642b184882f79e76ca57a9230fb971e494c3c1fd09c21ae3083ce891bcc0aa1"
344+
sha256: "6b753899253c38ca0523bb0eccff3934ec83d011705dae717c61ecf209e333c9"
337345
url: "https://pub.dev"
338346
source: hosted
339-
version: "0.5.2"
347+
version: "0.5.4"
340348
typed_data:
341349
dependency: transitive
342350
description:
@@ -386,4 +394,4 @@ packages:
386394
source: hosted
387395
version: "3.1.0"
388396
sdks:
389-
dart: ">=3.0.0 <4.0.0"
397+
dart: ">=3.0.0 <3.2.0"

pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
name: multiple_result
22
description: Multiple results for dart. Inspired by dartz's Either and Kotlin's sealed classes
3-
version: 5.0.0
3+
version: 5.1.0
44
repository: https://github.com/higorlapa/result
55

66
environment:
77
sdk: ">=3.0.0 <4.0.0"
88

99
dev_dependencies:
10-
test: ^1.24.2
11-
lints: ^2.0.1
10+
test: 1.24.4
11+
lints: 2.1.1

test/src/result_test.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,29 @@ void main() {
182182
expect(Error(1) == Error(1), isTrue);
183183
expect(Error(1).hashCode == Error(1).hashCode, isTrue);
184184
});
185+
186+
group("getOrThrow", () {
187+
test(
188+
"When accessing `getOrThrow` with a Success result, should return the Success value",
189+
() {
190+
final result = useCase.call();
191+
192+
expect(result.getOrThrow(), isA<MyResult>());
193+
});
194+
195+
test(
196+
"When accessing `getOrThrow` with a Error result, should throw SuccessResultNotFoundException",
197+
() {
198+
final result = useCase.call(returnError: true);
199+
200+
expect(
201+
() => result.getOrThrow(),
202+
throwsA(
203+
isA<SuccessResultNotFoundException>(),
204+
),
205+
);
206+
});
207+
});
185208
}
186209

187210
class MyUseCase {

0 commit comments

Comments
 (0)