Skip to content

Commit 1ba4b6d

Browse files
vnapnicvnapnic
authored andcommitted
update:
defined data response defined api exception
1 parent e701c48 commit 1ba4b6d

File tree

19 files changed

+359
-90
lines changed

19 files changed

+359
-90
lines changed
544 Bytes
Loading
52.4 KB
Binary file not shown.

configuration/.ios/Flutter/Generated.xcconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This is a generated file; do not edit or check into version control.
2-
FLUTTER_ROOT=D:\Tools\flutter_windows_2.2.1-stable\flutter
3-
FLUTTER_APPLICATION_PATH=D:\project\MyProject\Flutter_Architecture\configuration
2+
FLUTTER_ROOT=D:\SDK\flutter
3+
FLUTTER_APPLICATION_PATH=D:\MyProject\Flutter_Clean_Architecture\configuration
44
COCOAPODS_PARALLEL_CODE_SIGN=true
55
FLUTTER_TARGET=lib\main.dart
66
FLUTTER_BUILD_DIR=build

configuration/.ios/Flutter/flutter_export_environment.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/sh
22
# This is a generated file; do not edit or check into version control.
3-
export "FLUTTER_ROOT=D:\Tools\flutter_windows_2.2.1-stable\flutter"
4-
export "FLUTTER_APPLICATION_PATH=D:\project\MyProject\Flutter_Architecture\configuration"
3+
export "FLUTTER_ROOT=D:\SDK\flutter"
4+
export "FLUTTER_APPLICATION_PATH=D:\MyProject\Flutter_Clean_Architecture\configuration"
55
export "COCOAPODS_PARALLEL_CODE_SIGN=true"
66
export "FLUTTER_TARGET=lib\main.dart"
77
export "FLUTTER_BUILD_DIR=build"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'package:configuration/data/common/response_code.dart';
2+
3+
class BaseResponse {
4+
String? timestamp;
5+
int? code;
6+
String? message;
7+
String? error;
8+
String? token;
9+
dynamic errorBody;
10+
11+
BaseResponse(
12+
{this.timestamp,
13+
this.code,
14+
this.message,
15+
this.error,
16+
this.token,
17+
this.errorBody});
18+
19+
BaseResponse.fromJson(Map<String, dynamic> json) {
20+
timestamp = json['timestamp'] ?? "";
21+
code = json['code'] ?? ResponseCode.SERVER_UNKNOWN_ERROR;
22+
message = json['message'] ?? ResponseCode.SERVER_UNKNOWN_ERROR.message;
23+
error = json['error'] ?? "";
24+
token = json['token'] ?? null;
25+
errorBody = json['errorBody'];
26+
}
27+
28+
Map<String, dynamic> toJson() => <String, dynamic>{
29+
'timestamp': this.timestamp,
30+
'code': this.code,
31+
'message': this.message,
32+
'error': this.error,
33+
'token': this.token,
34+
'errorBody': this.errorBody,
35+
};
36+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import 'package:configuration/generated/l10n.dart';
2+
3+
class ResponseCode {
4+
static const SUCCESS = 200;
5+
static const FAILED = 500;
6+
static const NOT_FOUND = 404;
7+
static const UNAUTHORIZED = 401;
8+
static const FORBIDDEN = 403;
9+
static const EXPECTATION_FAILED = 417;
10+
11+
// Error
12+
static const SERVER_UNKNOWN_ERROR = 1000;
13+
14+
static const EMAIL_PASSWORD_INCORRECT = 1001;
15+
static const SOCIAL_PASSWORD_INCORRECT = 1002;
16+
17+
static const PHONE_NUMBER_NOT_EXISTS = 1003;
18+
19+
static const PASSWORD_IS_NULL_BLANK = 1004;
20+
static const EMAIL_IS_NULL_BLANK = 1005;
21+
22+
static const EMAIL_IS_EXISTS = 1006;
23+
static const SOCIAL_IS_EXISTS = 1007;
24+
25+
static const PHONE_NUMBER_IS_NULL_BLANK = 1008;
26+
static const PHONE_NUMBER_IS_EXISTS = 1009;
27+
28+
static const STAFF_CODE_IS_NULL_BLANK = 1010;
29+
30+
static const FILE_TOO_LARGE = 1011;
31+
32+
static const FILE_UPLOAD_FAIL = 1012;
33+
34+
static const UNSUPPORTED_MEDIA_TYPE = 1013;
35+
36+
static const UNSUPPORTED_DEVICE = 1014;
37+
38+
static const PHONE_NUMBER_WRONG_FORMAT = 1015;
39+
40+
static const EMAIL_WRONG_FORMAT = 1016;
41+
42+
static const USER_NOT_FOUND = 1017;
43+
44+
static const WARNING_DATA_FORMAT = 1018;
45+
46+
static const VERIFY_CODE_EXPIRE = 1019;
47+
48+
static const VERIFY_CODE_INCORRECT = 1020;
49+
50+
static const WRONG_TOO_MANY_TIME = 1021;
51+
}
52+
53+
extension messageResponse on int {
54+
String? get message => messages[this];
55+
56+
static final messages = <int, String>{
57+
// ResponseCode.SUCCESS: S.current.code_200,
58+
// ResponseCode.FAILED: S.current.code_500,
59+
// ResponseCode.NOT_FOUND: S.current.code_404,
60+
// ResponseCode.UNAUTHORIZED: S.current.code_403,
61+
// ResponseCode.FORBIDDEN: S.current.code_401,
62+
// ResponseCode.EXPECTATION_FAILED: S.current.code_417,
63+
//
64+
// // Error
65+
// ResponseCode.SERVER_UNKNOWN_ERROR: S.current.code_1000,
66+
//
67+
// ResponseCode.EMAIL_PASSWORD_INCORRECT: S.current.code_1001,
68+
// ResponseCode.SOCIAL_PASSWORD_INCORRECT: S.current.code_1002,
69+
//
70+
// ResponseCode.PHONE_NUMBER_NOT_EXISTS: S.current.code_1003,
71+
//
72+
// ResponseCode.PASSWORD_IS_NULL_BLANK: S.current.code_1004,
73+
// ResponseCode.EMAIL_IS_NULL_BLANK: S.current.code_1005,
74+
//
75+
// ResponseCode.EMAIL_IS_EXISTS: S.current.code_1006,
76+
// ResponseCode.SOCIAL_IS_EXISTS: S.current.code_1007,
77+
//
78+
// ResponseCode.PHONE_NUMBER_IS_NULL_BLANK: S.current.code_1008,
79+
// ResponseCode.PHONE_NUMBER_IS_EXISTS: S.current.code_1009,
80+
//
81+
// ResponseCode.STAFF_CODE_IS_NULL_BLANK: S.current.code_1010,
82+
//
83+
// ResponseCode.FILE_TOO_LARGE: S.current.code_1011,
84+
//
85+
// ResponseCode.FILE_UPLOAD_FAIL: S.current.code_1012,
86+
//
87+
// ResponseCode.UNSUPPORTED_MEDIA_TYPE: S.current.code_1013,
88+
//
89+
// ResponseCode.UNSUPPORTED_DEVICE: S.current.code_1014,
90+
//
91+
// ResponseCode.PHONE_NUMBER_WRONG_FORMAT: S.current.code_1015,
92+
//
93+
// ResponseCode.EMAIL_WRONG_FORMAT: S.current.code_1016,
94+
//
95+
// ResponseCode.USER_NOT_FOUND: S.current.code_1017,
96+
//
97+
// ResponseCode.WARNING_DATA_FORMAT: S.current.code_1018,
98+
//
99+
// ResponseCode.VERIFY_CODE_EXPIRE: S.current.code_1019,
100+
//
101+
// ResponseCode.VERIFY_CODE_INCORRECT: S.current.code_1020,
102+
//
103+
// ResponseCode.WRONG_TOO_MANY_TIME: S.current.code_1021,
104+
};
105+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import 'dart:io';
2+
import 'package:configuration/data/common/base_response.dart';
3+
import 'package:configuration/data/common/response_code.dart';
4+
import 'package:configuration/generated/l10n.dart';
5+
import 'package:dio/dio.dart';
6+
import 'package:intl/intl.dart';
7+
8+
class ApiException {
9+
late int? errorCode;
10+
late String? errorMessage = "";
11+
late dynamic errorBody = "";
12+
late DioError exception;
13+
14+
String? get displayError {
15+
if (exception.type == DioErrorType.connectTimeout) {
16+
return S.current.connect_timeout;
17+
}
18+
19+
if (exception.type == DioErrorType.receiveTimeout) {
20+
return S.current.receive_timeout;
21+
}
22+
23+
if (exception.type == DioErrorType.sendTimeout) {
24+
return S.current.send_timeout;
25+
}
26+
27+
if (exception.type == DioErrorType.other) {
28+
if (exception.error is SocketException) {
29+
return S.current.please_check_internet_connection;
30+
}
31+
return exception.error.toString();
32+
}
33+
34+
// Prioritize error returned in response body
35+
final responseData = exception.response?.data;
36+
if (responseData is Map && responseData["message"] != null) {
37+
final message = responseData["message"];
38+
if (message is List) {
39+
return message
40+
.map((e) => toBeginningOfSentenceCase(e.toString()))
41+
.join("\n");
42+
}
43+
44+
return toBeginningOfSentenceCase(message.toString());
45+
}
46+
47+
if (responseData is Map && responseData["error"] != null) {
48+
return responseData["error"].toString();
49+
}
50+
51+
// Fallback to request error if no error returned in response body
52+
return exception.error?.toString() ?? exception.response?.statusMessage;
53+
}
54+
55+
ApiException({required DioError exception}) {
56+
this.exception = exception;
57+
switch (exception.type) {
58+
case DioErrorType.response:
59+
{
60+
dynamic data = exception.response?.data;
61+
try {
62+
if (data is BaseResponse) {
63+
errorMessage = data.code == ResponseCode.UNAUTHORIZED ||
64+
data.code == ResponseCode.FORBIDDEN
65+
? S.current.invalid_credentials
66+
: data.code?.message ?? data.error;
67+
68+
errorCode = data.code;
69+
errorBody = data.errorBody;
70+
}
71+
} catch (e) {
72+
errorMessage = e.toString();
73+
// Try to get Dio internal error which might due to service not available
74+
if (exception.error != null) {
75+
errorMessage = exception.error.toString();
76+
}
77+
if (exception.response?.statusMessage != null &&
78+
exception.response?.statusMessage?.isNotEmpty == true) {
79+
errorMessage = exception.response?.statusMessage;
80+
}
81+
}
82+
}
83+
break;
84+
default:
85+
{
86+
switch (exception.type) {
87+
case DioErrorType.cancel:
88+
{
89+
errorMessage = S.current.cancelled;
90+
break;
91+
}
92+
case DioErrorType.connectTimeout:
93+
case DioErrorType.receiveTimeout:
94+
case DioErrorType.sendTimeout:
95+
{
96+
errorMessage = S.current.connect_timeout;
97+
}
98+
break;
99+
default:
100+
{
101+
if (exception.response?.statusCode == HttpStatus.notFound) {
102+
errorMessage = S.current.server_not_found;
103+
}
104+
if (exception.response?.statusCode ==
105+
HttpStatus.serviceUnavailable) {
106+
errorMessage = S.current.server_unknown_error;
107+
} else if (exception.error is SocketException) {
108+
errorMessage = S.current.connection_problem;
109+
} else if (exception.error is HttpException) {
110+
errorMessage = S.current.connection_problem;
111+
}
112+
}
113+
}
114+
}
115+
}
116+
}
117+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import 'package:configuration/generated/l10n.dart';
2+
3+
class DataLocalException {
4+
late String errorMessage;
5+
6+
DataLocalException.sqlite() : errorMessage = S.current.database_exception;
7+
}

configuration/lib/l10n/intl_en.arb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,18 @@
1-
{}
1+
{
2+
"connectionTimedOut": "The connection has timed out. Please try again",
3+
"connectionProblem": "There are some problems with the connection. Please try again",
4+
"invalidCredentials": "Invalid credentials",
5+
"responseNull": "Response is null",
6+
"invalid_credentials": "Invalid credentials",
7+
"cancelled": "Cancelled",
8+
"connect_timeout": "Connect timeout",
9+
"receive_timeout": "Receive timeout",
10+
"send_timeout": "Send timeout",
11+
"connection_problem": "Connection problem",
12+
"please_check_internet_connection": "Please check internet connection",
13+
"server_error": "Server error.",
14+
"unknown_error": "Unknown error",
15+
"server_unknown_error": "Server unknown error",
16+
"server_not_found": "Server not found",
17+
"database_exception": "Database exception"
18+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import 'dart:io';
2+
3+
import 'package:configuration/data/common/base_response.dart';
4+
import 'package:dio/dio.dart';
5+
6+
class DataFormatInterceptor extends InterceptorsWrapper {
7+
@override
8+
void onResponse(Response response, ResponseInterceptorHandler handler) {
9+
if (response.statusCode == HttpStatus.preconditionFailed) {
10+
handler.reject(DioError(
11+
requestOptions: response.requestOptions,
12+
response: Response<BaseResponse?>(
13+
data: BaseResponse.fromJson(response.data),
14+
requestOptions: response.requestOptions,
15+
),
16+
type: DioErrorType.response));
17+
return;
18+
}
19+
20+
if (response.statusCode != HttpStatus.ok) {
21+
handler.reject(DioError(
22+
requestOptions: response.requestOptions,
23+
response: Response(
24+
headers: response.headers,
25+
statusCode: response.statusCode,
26+
requestOptions: response.requestOptions,
27+
isRedirect: response.isRedirect,
28+
statusMessage: response.statusMessage,
29+
redirects: response.redirects,
30+
extra: response.extra,
31+
),
32+
type: DioErrorType.other));
33+
return;
34+
}
35+
36+
super.onResponse(response, handler);
37+
}
38+
}

0 commit comments

Comments
 (0)