Output login errors to user

This commit is contained in:
2025-09-13 20:58:15 +02:00
parent dc0da85688
commit 4aa43d8f20
3 changed files with 84 additions and 8 deletions

70
lib/utility.dart Normal file
View File

@@ -0,0 +1,70 @@
/// 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 [Err] 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) = Err._;
bool isOk() {
return this is Ok;
}
T value() {
Ok<T> ok = this as Ok<T>;
return ok._value;
}
bool isErr() {
return this is Err;
}
Exception error() {
Err<T> err = this as Err<T>;
return err._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 Err<T> extends Result<T> {
const Err._(this._error);
/// The resulting error of this result.
final Exception _error;
@override
String toString() => 'Result<$T>.error($_error)';
}