Приложение на флаторе, настройки
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
|
||||
part 'errors.freezed.dart';
|
||||
|
||||
@freezed
|
||||
class AppError with _$AppError {
|
||||
const factory AppError.unknown({String? message}) = UnknownError;
|
||||
const factory AppError.network({String? message}) = NetworkError;
|
||||
const factory AppError.unauthorized({String? message}) = UnauthorizedError;
|
||||
const factory AppError.forbidden({String? message}) = ForbiddenError;
|
||||
const factory AppError.notFound({String? message}) = NotFoundError;
|
||||
const factory AppError.server({int? statusCode, String? message}) = ServerError;
|
||||
const factory AppError.database({String? message}) = DatabaseError;
|
||||
const factory AppError.validation({Map<String, String>? fieldErrors}) = ValidationError;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,30 @@
|
||||
import 'errors.dart';
|
||||
|
||||
/// A generic class for handling success/failure results
|
||||
class Result<T> {
|
||||
final T? data;
|
||||
final AppError? error;
|
||||
|
||||
const Result.success(this.data) : error = null;
|
||||
const Result.failure(this.error) : data = null;
|
||||
|
||||
bool get isSuccess => error == null;
|
||||
bool get isFailure => error != null;
|
||||
|
||||
R when<R>({
|
||||
required R Function(T data) onSuccess,
|
||||
required R Function(AppError error) onFailure,
|
||||
}) {
|
||||
if (isSuccess) {
|
||||
return onSuccess(data as T);
|
||||
}
|
||||
return onFailure(error!);
|
||||
}
|
||||
|
||||
T getOrThrow() {
|
||||
if (isFailure) {
|
||||
throw error!;
|
||||
}
|
||||
return data as T;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user