Skip to content

Commit 1058203

Browse files
authored
Merge pull request #117 from lohanidamodar/feat-dart-sdk-custom-exception
Feat Dart custom exception
2 parents 43d63d6 + f5f6b28 commit 1058203

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

src/SDK/Language/Dart.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ public function getFiles()
281281
'template' => '/dart/lib/client.dart.twig',
282282
'minify' => false,
283283
],
284+
[
285+
'scope' => 'default',
286+
'destination' => '/lib/exception.dart',
287+
'template' => '/dart/lib/exception.dart.twig',
288+
'minify' => false,
289+
],
284290
[
285291
'scope' => 'default',
286292
'destination' => '/lib/{{ language.params.packageName }}.dart',

templates/dart/lib/client.dart.twig

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,28 @@ class Client {
7676
headers: {...this.headers, ...headers},
7777
method: method.name(),
7878
);
79+
try {
7980

80-
if(headers['content-type'] == 'multipart/form-data') {
81-
return http.request(path, data: FormData.fromMap(params), options: options);
82-
}
81+
if(headers['content-type'] == 'multipart/form-data') {
82+
return http.request(path, data: FormData.fromMap(params), options: options);
83+
}
8384

84-
if (method == HttpMethod.get) {
85-
params.keys.forEach((key) {if (params[key] is int || params[key] is double) {
86-
params[key] = params[key].toString();
87-
}});
88-
89-
return http.get(path, queryParameters: params, options: options);
90-
} else {
91-
return http.request(path, data: params, options: options);
85+
if (method == HttpMethod.get) {
86+
params.keys.forEach((key) {if (params[key] is int || params[key] is double) {
87+
params[key] = params[key].toString();
88+
}});
89+
90+
return http.get(path, queryParameters: params, options: options);
91+
} else {
92+
return http.request(path, data: params, options: options);
93+
}
94+
} on DioError catch(e) {
95+
if(e.response == null) {
96+
throw {{spec.title | caseUcfirst}}Exception(e.message);
97+
}
98+
throw {{spec.title | caseUcfirst}}Exception(e.response.data['message'],e.response.data['code'], e.response.data);
99+
} catch(e) {
100+
throw {{spec.title | caseUcfirst}}Exception(e.message);
92101
}
93102
}
94103
}
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/dart/lib/package.dart.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export 'package:dio/dio.dart' show Response, MultipartFile;
1111
part 'client.dart';
1212
part 'enums.dart';
1313
part 'service.dart';
14+
part 'exception.dart';
1415
{% for service in spec.services %}
1516
part 'services/{{service.name | caseDash}}.dart';
1617
{% endfor %}

0 commit comments

Comments
 (0)