Skip to content

Commit

Permalink
Convert error segregation to pattern match
Browse files Browse the repository at this point in the history
  • Loading branch information
dhzdhd committed Jun 17, 2024
1 parent 9212d49 commit 6973c90
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions lib/errors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,35 @@ 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(error.response.toString());
} else if (this is Exception) {
return GeneralError(toString());
} else {
throw NoSuchMethodError.withInvocation(
this,
Invocation.method(
const Symbol('segregateError'),
null,
),
);
}
return switch (this) {
AppError err => err,
DioException err => HttpError(err.response.toString()),
Exception err => GeneralError(err.toString()),
Object err => throw NoSuchMethodError.withInvocation(
err,
Invocation.method(
const Symbol('segregateError'),
null,
),
)
};

// if (this is AppError) {
// return this as AppError;
// } else if (this is DioException) {
// final error = this as DioException;
// return HttpError(error.response.toString());
// } else if (this is Exception) {
// return GeneralError(toString());
// } else {
// throw NoSuchMethodError.withInvocation(
// this,
// Invocation.method(
// const Symbol('segregateError'),
// null,
// ),
// );
// }
}
}

Expand Down

0 comments on commit 6973c90

Please sign in to comment.