-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't pop an error when there is no logs file
- Loading branch information
Showing
3 changed files
with
103 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:flutter/widgets.dart'; | ||
import 'package:mobile_nebula/services/result.dart'; | ||
|
||
class LogsNotFoundException implements Exception { | ||
String error() => 'No logs file found'; | ||
} | ||
|
||
class LogsNotifier extends ChangeNotifier { | ||
Result<String>? logsResult; | ||
|
||
LogsNotifier(); | ||
|
||
loadLogs({required String logFile}) async { | ||
final file = File(logFile); | ||
try { | ||
logsResult = Result.ok(await file.readAsString()); | ||
notifyListeners(); | ||
} on FileSystemException { | ||
logsResult = Result.error(LogsNotFoundException()); | ||
notifyListeners(); | ||
} on Exception catch (err) { | ||
logsResult = Result.error(err); | ||
notifyListeners(); | ||
} catch (err) { | ||
logsResult = Result.error(Exception(err)); | ||
notifyListeners(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/// Utility class that simplifies handling errors. | ||
/// | ||
/// Return a [Result] from a function to indicate success or failure. | ||
/// | ||
/// A [Result] is either an [Ok] with a value of type [T] | ||
/// or an [Error] with an [Exception]. | ||
/// | ||
/// Use [Result.ok] to create a successful result with a value of type [T]. | ||
/// Use [Result.error] to create an error result with an [Exception]. | ||
/// | ||
/// Evaluate the result using a switch statement: | ||
/// ```dart | ||
/// switch (result) { | ||
/// case Ok(): { | ||
/// print(result.value); | ||
/// } | ||
/// case Error(): { | ||
/// print(result.error); | ||
/// } | ||
/// } | ||
/// ``` | ||
sealed class Result<T> { | ||
const Result(); | ||
|
||
/// Creates a successful [Result], completed with the specified [value]. | ||
const factory Result.ok(T value) = Ok._; | ||
|
||
/// Creates an error [Result], completed with the specified [error]. | ||
const factory Result.error(Exception error) = Error._; | ||
} | ||
|
||
/// A successful [Result] with a returned [value]. | ||
final class Ok<T> extends Result<T> { | ||
const Ok._(this.value); | ||
|
||
/// The returned value of this result. | ||
final T value; | ||
|
||
@override | ||
String toString() => 'Result<$T>.ok($value)'; | ||
} | ||
|
||
/// An error [Result] with a resulting [error]. | ||
final class Error<T> extends Result<T> { | ||
const Error._(this.error); | ||
|
||
/// The resulting error of this result. | ||
final Exception error; | ||
|
||
@override | ||
String toString() => 'Result<$T>.error($error)'; | ||
} |