Skip to content

Commit

Permalink
Create freezed error model instead of a interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dhzdhd committed May 11, 2024
1 parent 0674386 commit beb8ffa
Show file tree
Hide file tree
Showing 13 changed files with 540 additions and 122 deletions.
57 changes: 23 additions & 34 deletions lib/errors.dart
Original file line number Diff line number Diff line change
@@ -1,52 +1,41 @@
import 'package:dio/dio.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'errors.freezed.dart';

extension ErrorSegregation on Object {
AppError segregateError() {
if (this is AppError) {
return this as AppError;
} else if (this is DioException) {
final error = this as DioException;
return HttpError(
message: error.response.toString(),
stackTrace: error.stackTrace.toString(),
);
return HttpError(error.response.toString());
} else if (this is Exception) {
return GeneralError(toString());
} else {
return GeneralError(
message: toString(),
stackTrace: (this as Error).stackTrace.toString(),
throw NoSuchMethodError.withInvocation(
this,
Invocation.method(
const Symbol('segregateError'),
null,
),
);
}
}

String toErrorString() {
if (this is AppError) {
return (this as AppError).message;
}
return '';
}
}

sealed class AppError implements Exception {
String get message;
String get stackTrace;
@freezed
sealed class AppError with _$AppError {
const factory AppError.http(String message) = HttpError;
const factory AppError.general(String message) = GeneralError;
}

class HttpError implements AppError {
@override
final String message;
// sealed class AppError implements Exception {
// final String message;
// final String stackTrace;

@override
final String stackTrace;
// AppError({required this.message, required this.stackTrace});

HttpError({required this.message, required this.stackTrace});
}

class GeneralError implements AppError {
@override
final String message;

@override
final String stackTrace;

GeneralError({required this.message, required this.stackTrace});
}
// String get message => message;
// String get stackTrace;
// }
Loading

0 comments on commit beb8ffa

Please sign in to comment.