Skip to content

Commit 7f7306a

Browse files
authored
Merge pull request #113 from lohanidamodar/feat-custom-exception-model
feat-custom-exception-model
2 parents e3cbd44 + c6dce47 commit 7f7306a

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

templates/flutter/lib/client.dart.twig

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,27 @@ 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 await 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 await http.get(path, queryParameters: params, options: options);
122+
} else {
123+
return await http.request(path, data: params, options: options);
124+
}
125+
} on DioError catch(e) {
126+
if(e.response == null) {
127+
throw {{spec.title | caseUcfirst}}Exception(e.message);
128+
}
129+
throw {{spec.title | caseUcfirst}}Exception(e.response.data['message'],e.response.data['code'], e.response.data);
130+
} catch(e) {
131+
throw {{spec.title | caseUcfirst}}Exception(e.message);
123132
}
124133
}
125134
}
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, 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)