-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create freezed error model instead of a interface
- Loading branch information
Showing
13 changed files
with
540 additions
and
122 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
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; | ||
// } |
Oops, something went wrong.