Skip to content

Commit

Permalink
💥 update datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
ngocpd-1250 committed Aug 15, 2024
1 parent 34c9f4b commit 2cef774
Show file tree
Hide file tree
Showing 108 changed files with 1,287 additions and 1,653 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"prefs",
"riverpod",
"screenutil",
"unfocus",
"usecase",
"usecases"
]
}
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ analyzer:
- "**/*.freezed.dart"
errors:
invalid_annotation_target: ignore
must_be_immutable: ignore
6 changes: 6 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
PODS:
- Flutter (1.0.0)
- flutter_secure_storage (3.3.1):
- Flutter
- shared_preferences_foundation (0.0.1):
- Flutter
- FlutterMacOS

DEPENDENCIES:
- Flutter (from `Flutter`)
- flutter_secure_storage (from `.symlinks/plugins/flutter_secure_storage/ios`)
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)

EXTERNAL SOURCES:
Flutter:
:path: Flutter
flutter_secure_storage:
:path: ".symlinks/plugins/flutter_secure_storage/ios"
shared_preferences_foundation:
:path: ".symlinks/plugins/shared_preferences_foundation/darwin"

SPEC CHECKSUMS:
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
flutter_secure_storage: 7953c38a04c3fdbb00571bcd87d8e3b5ceb9daec
shared_preferences_foundation: fcdcbc04712aee1108ac7fda236f363274528f78

PODFILE CHECKSUM: 6a14efad752d34a4bf2f66b717e84e2f72f17aca
Expand Down
121 changes: 121 additions & 0 deletions lib/data/app_error.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import 'dart:io';

import 'package:flutter/foundation.dart';

import 'package:dio/dio.dart';

import 'package:base_flutter/data/remote/response/error_response.dart';

enum AppErrorType {
network,
badRequest,
unauthorized,
notFound,
cancel,
timeout,
server,
unknown,
}

class AppError {
final String? message;
final AppErrorType type;

final int? errorCode;
final List<ErrorResponse>? errors;

AppError(this.type, this.message, {this.errorCode, this.errors});

factory AppError.from(Object? exception) {
var type = AppErrorType.unknown;
String? message = '';
int? code;
List<ErrorResponse>? errs;
if (exception is DioException) {
message = exception.error.toString();
code = exception.response?.statusCode ?? -1;

switch (exception.type) {
case DioExceptionType.connectionTimeout:
case DioExceptionType.receiveTimeout:
type = AppErrorType.timeout;
break;
case DioExceptionType.sendTimeout:
case DioExceptionType.connectionError:
type = AppErrorType.network;
break;
case DioExceptionType.badResponse:
switch (exception.response?.statusCode) {
case HttpStatus.unauthorized:
type = AppErrorType.unauthorized;
break;
case HttpStatus.badRequest:
case HttpStatus.notFound:
case HttpStatus.methodNotAllowed:
case HttpStatus.unprocessableEntity:
case HttpStatus.internalServerError:
case HttpStatus.badGateway:
case HttpStatus.serviceUnavailable:
case HttpStatus.gatewayTimeout:
type = AppErrorType.server;
try {
final serverErr =
ErrorResponse.fromJson(exception.response?.data);
errs = [serverErr];
} catch (ex) {
errs = [ErrorResponse(statusCode: -1, message: ex.toString())];
}
break;
default:
type = AppErrorType.unknown;
break;
}
break;
case DioExceptionType.cancel:
type = AppErrorType.cancel;
break;

case DioExceptionType.unknown:
default:
if (exception.error is SocketException) {
type = AppErrorType.network;
} else {
type = AppErrorType.unknown;
}
break;
}
} else if (exception is AuthenticationException) {
type = AppErrorType.unauthorized;
code = HttpStatus.unauthorized;
message = exception.message;
} else {
code = -1;
type = AppErrorType.unknown;
message = 'AppError: $exception';
}

return AppError(type, message, errorCode: code, errors: errs);
}

@override
bool operator ==(covariant AppError other) {
if (identical(this, other)) return true;

return other.message == message &&
other.type == type &&
other.errorCode == errorCode &&
listEquals(other.errors, errors);
}

@override
String toString() {
return 'AppError(message: $message, type: $type, errorCode: $errorCode, errors: $errors)';
}
}

class AuthenticationException extends Object {
AuthenticationException({
required this.message,
});
final String message;
}
11 changes: 11 additions & 0 deletions lib/data/base/data_source.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:base_flutter/data/app_error.dart';

mixin DataSource {
Future<T> excuse<T>(Future<T> Function() block) async {
try {
return await block();
} catch (e) {
throw AppError.from(e);
}
}
}
19 changes: 0 additions & 19 deletions lib/data/data_sources/remote/api/auth_api_client.dart

This file was deleted.

1 change: 0 additions & 1 deletion lib/data/data_sources/remote/api/client/export.dart

This file was deleted.

142 changes: 0 additions & 142 deletions lib/data/data_sources/remote/api/client/res_api_client.dart

This file was deleted.

13 changes: 0 additions & 13 deletions lib/data/data_sources/remote/api/exception/base/app_exception.dart

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 2cef774

Please sign in to comment.