Skip to content

Commit 1c66b59

Browse files
committed
Finished error management
1 parent 316987c commit 1c66b59

File tree

2 files changed

+214
-0
lines changed

2 files changed

+214
-0
lines changed

lib/src/featherjs.dart

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import 'package:flutter_feathersjs/src/rest_client.dart';
2+
import 'package:flutter_feathersjs/src/scketio_client.dart';
3+
import 'dart:async';
4+
import 'package:flutter_feathersjs/src/helper.dart';
5+
import 'package:meta/meta.dart';
6+
7+
///FlutterFeatherJs allow you to communicate with your feathers js server
8+
///
9+
///Response format: You get exactly what feathers server send when no error
10+
///
11+
///Uploading file: Use rest client, socketio client cannot upload file
12+
///
13+
class FlutterFeathersjs {
14+
//RestClient
15+
RestClient rest;
16+
//SocketioClient
17+
SocketioClient scketio;
18+
19+
///Using singleton
20+
static final FlutterFeathersjs _flutterFeathersjs =
21+
FlutterFeathersjs._internal();
22+
factory FlutterFeathersjs() {
23+
return _flutterFeathersjs;
24+
}
25+
FlutterFeathersjs._internal();
26+
27+
///Intialize both rest and scoketio client
28+
init({@required String baseUrl, Map<String, dynamic> extraHeaders}) {
29+
rest = new RestClient()..init(baseUrl: baseUrl, extraHeaders: extraHeaders);
30+
31+
scketio = new SocketioClient()..init(baseUrl: baseUrl);
32+
}
33+
34+
/// Authenticate rest and scketio clients so you can use both of them
35+
///
36+
///___________________________________________________________________
37+
/// @params `username` can be : email, phone, etc;
38+
///
39+
/// But ensure that `userNameFieldName` is correct with your chosed `strategy`
40+
///
41+
/// By default this will be `email`and the strategy `local`
42+
Future<Map<String, dynamic>> authenticate(
43+
{String strategy = "local",
44+
@required String userName,
45+
@required String password,
46+
String userNameFieldName = "email"}) async {
47+
try {
48+
//Auth with rest to refresh or create accessToken
49+
var restAuthResponse = await rest.authenticate(
50+
strategy: strategy,
51+
userName: userName,
52+
userNameFieldName: userNameFieldName,
53+
password: password);
54+
55+
try {
56+
//Then auth with jwt socketio
57+
bool isAuthenticated = await scketio.authWithJWT();
58+
59+
// Check wether both client are authenticated or not
60+
if (restAuthResponse != null && isAuthenticated == true) {
61+
return restAuthResponse;
62+
} else {
63+
// Both failed
64+
throw new FeatherJsError(
65+
type: FeatherJsErrorType.IS_AUTH_FAILED_ERROR,
66+
error: "Auth failed with unknown reason");
67+
}
68+
} on FeatherJsError catch (e) {
69+
// Socketio failed
70+
throw new FeatherJsError(type: e.type, error: e);
71+
}
72+
} on FeatherJsError catch (e) {
73+
// Rest failed
74+
throw new FeatherJsError(type: e.type, error: e);
75+
}
76+
}
77+
78+
/// ReAuthenticate rest and scketio clients
79+
///
80+
///___________________________________________________________________
81+
Future<dynamic> reAuthenticate() async {
82+
try {
83+
//Auth with rest to refresh or create accessToken
84+
bool isRestAuthenticated = await rest.reAuthenticate();
85+
86+
try {
87+
//Then auth with jwt socketio
88+
bool isSocketioAuthenticated = await scketio.authWithJWT();
89+
90+
// Check wether both client are authenticated or not
91+
if (isRestAuthenticated == true && isSocketioAuthenticated == true) {
92+
return true;
93+
} else {
94+
// Both failed
95+
throw new FeatherJsError(
96+
type: FeatherJsErrorType.IS_AUTH_FAILED_ERROR,
97+
error: "Auth failed with unknown reason");
98+
}
99+
} on FeatherJsError catch (e) {
100+
// Socketio failed
101+
throw new FeatherJsError(type: e.type, error: e);
102+
}
103+
} on FeatherJsError catch (e) {
104+
// Rest failed
105+
throw new FeatherJsError(type: e.type, error: e);
106+
}
107+
}
108+
}

lib/src/helper.dart

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import 'dart:async';
2+
import 'package:shared_preferences/shared_preferences.dart';
3+
4+
/// Use to save feathers js token
5+
const String FEATHERSJS_ACCESS_TOKEN = "FEATHERSJS_ACCESS_TOKEN";
6+
7+
/// Socketio Connected
8+
class Connected {}
9+
10+
/// Socketio Disconnected
11+
class DisConnected {}
12+
13+
/// Utilities for FlutterFeathersJs
14+
class FeatherjsHelper {
15+
SharedPreferences prefs;
16+
FeatherjsHelper();
17+
18+
/// Store JWT for reAuth() purpose
19+
Future<bool> setAccessToken({String token}) async {
20+
prefs = await SharedPreferences.getInstance();
21+
22+
return await prefs.setString(FEATHERSJS_ACCESS_TOKEN, token);
23+
}
24+
25+
/// Get the early stored JWT for reAuth() purpose
26+
Future<String> getAccessToken({String token}) async {
27+
prefs = await SharedPreferences.getInstance();
28+
return prefs.getString(FEATHERSJS_ACCESS_TOKEN);
29+
}
30+
}
31+
32+
/// FeatherJsErrorType
33+
enum FeatherJsErrorType {
34+
// This error come from your feathers js server
35+
IS_SERVER_ERROR,
36+
37+
/// This error come from any FlutterFeathersjs.rest's method
38+
IS_REST_ERROR,
39+
40+
/// This error come from any FlutterFeathersjs.scketio's method
41+
IS_SOCKETIO_ERROR,
42+
43+
/// Error when derialiazing realtime object in socketio listen method
44+
IS_DESERIALIZATION_ERROR,
45+
46+
/// Unknown error
47+
IS_UNKNOWN_ERROR,
48+
49+
/// When in auth method, jwt cause a problem
50+
IS_JWT_TOKEN_ERROR,
51+
52+
/// Error while building form data in rest client
53+
IS_FORM_DATA_ERROR,
54+
55+
/// Jwt not found in local storage
56+
IS_JWT_TOKEN_NOT_FOUND_ERROR,
57+
// Jwt is invalid
58+
IS_JWT_INVALID_ERROR,
59+
// Jwt expired
60+
IS_JWT_EXPIRED_ERROR,
61+
// User credentials are wrong for authentication on the server
62+
IS_INVALID_CREDENTIALS_ERROR,
63+
// Wrong strategy is given to the auth method
64+
IS_INVALID_STRATEGY_ERROR,
65+
// Auth failed with unknown reason
66+
IS_AUTH_FAILED_ERROR,
67+
}
68+
69+
/// FeatherJsError describes the error info when request failed.
70+
class FeatherJsError implements Exception {
71+
FeatherJsError({
72+
this.type = FeatherJsErrorType.IS_UNKNOWN_ERROR,
73+
this.error,
74+
});
75+
76+
FeatherJsErrorType type;
77+
78+
dynamic error;
79+
80+
StackTrace _stackTrace;
81+
82+
set stackTrace(StackTrace stack) => _stackTrace = stack;
83+
84+
StackTrace get stackTrace => _stackTrace;
85+
86+
String get message => (error?.toString() ?? '');
87+
88+
@override
89+
String toString() {
90+
var msg = 'FeatherJsError [$type]: $message';
91+
if (_stackTrace != null) {
92+
msg += '\n$stackTrace';
93+
}
94+
return msg;
95+
}
96+
}
97+
98+
/// Feathers Js realtime event data
99+
class FeathersJsEventData<T> {
100+
FeathersJsEventType type;
101+
T data;
102+
FeathersJsEventData({this.type, this.data});
103+
}
104+
105+
/// Feathers Js realtime event type
106+
enum FeathersJsEventType { updated, patched, created, removed }

0 commit comments

Comments
 (0)