Skip to content

Commit 6401953

Browse files
committed
exception model and handling in flutter
1 parent e603b14 commit 6401953

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

templates/flutter/lib/client.dart.twig

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,24 @@ class Client {
108108
method: method.name(),
109109
);
110110

111-
if(headers['content-type'] == 'multipart/form-data') {
112-
return http.request(path, data: FormData.fromMap(params), options: options);
113-
}
111+
try {
112+
if(headers['content-type'] == 'multipart/form-data') {
113+
return http.request(path, data: FormData.fromMap(params), options: options);
114+
}
114115

115-
if (method == HttpMethod.get) {
116-
params.keys.forEach((key) {if (params[key] is int || params[key] is double) {
117-
params[key] = params[key].toString();
118-
}});
119-
120-
return http.get(path, queryParameters: params, options: options);
121-
} else {
122-
return http.request(path, data: params, options: options);
116+
if (method == HttpMethod.get) {
117+
params.keys.forEach((key) {if (params[key] is int || params[key] is double) {
118+
params[key] = params[key].toString();
119+
}});
120+
121+
return http.get(path, queryParameters: params, options: options);
122+
} else {
123+
return http.request(path, data: params, options: options);
124+
}
125+
} on DioError catch(e) {
126+
throw {{spec.title | caseUcfirst}}Exception(e.response.data['message'],e.response.data['code'], e.response.data);
127+
} catch(e) {
128+
throw {{spec.title | caseUcfirst}}Exception(e.message, 0);
123129
}
124130
}
125131
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
part of {{ language.params.packageName }};
2+
3+
class {{spec.title | caseUcfirst}}Exception implements Exception {
4+
final String message;
5+
final int code;
6+
final dynamic response;
7+
8+
{{spec.title | caseUcfirst}}Exception([this.message = "", this.code = 0, this.response]);
9+
10+
}

templates/flutter/lib/package.dart.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export 'package:dio/dio.dart' show Response;
1818
part 'client.dart';
1919
part 'enums.dart';
2020
part 'service.dart';
21+
part 'exception.dart';
2122
{% for service in spec.services %}
2223
part 'services/{{service.name | caseDash}}.dart';
2324
{% endfor %}

0 commit comments

Comments
 (0)