8000 [DART] Feat: add request on error (#765) · sdkgen/sdkgen@bf6e7ff · GitHub
[go: up one dir, main page]

Skip to content

Commit bf6e7ff

Browse files
authored
[DART] Feat: add request on error (#765)
1 parent 81fd6d1 commit bf6e7ff

File tree

4 files changed

+104
-48
lines changed

4 files changed

+104
-48
lines changed

dart-generator/src/dart-client.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ ${ast.operations
109109
for (const error of ast.errors) {
110110
const hasData = !(error.dataType instanceof VoidPrimitiveType);
111111

112-
code += ` '${error.name}': SdkgenErrorDescription('${error.dataType.name}', (msg, data) => ${error.name}(msg${hasData ? ", data" : ""})),\n`;
112+
const dataArg = hasData ? ", data" : "";
113+
114+
code += ` '${error.name}': SdkgenErrorDescription('${error.dataType.name}', (msg, req, data) => ${error.name}(msg, req${dataArg})),\n`;
113115
}
114116

115117
code += `};\n`;

dart-runtime/lib/deviceinfo_web.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'dart:js' as js;
44

55
Future<Map<String, Object?>> getDeviceInfo(String deviceId) async {
66
// Intl.DateTimeFormat().resolvedOptions().timeZone
7-
var timeZoneName = js.context['Intl'] == null
7+
final timeZoneName = js.context['Intl'] == null
88
? DateTime.now().timeZoneName
99
: (((js.context['Intl'] as js.JsObject).callMethod('DateTimeFormat')
1010
as js.JsObject)

dart-runtime/lib/http_client.dart

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ import 'deviceinfo_generic.dart'
1212
if (dart.library.html) 'deviceinfo_web.dart';
1313

1414
class SdkgenError implements Exception {
15-
String message;
16-
SdkgenError(this.message);
15+
final String message;
16+
final Json request;
17+
const SdkgenError(this.message, [this.request = const {}]);
1718

1819
@override
1920
String toString() => '$runtimeType: $message';
2021
}
2122

22-
class SdkgenErrorWithData<T> implements Exception {
23-
String message;
24-
T data;
25-
SdkgenErrorWithData(this.message, this.data);
23+
class SdkgenErrorWithData<T> extends SdkgenError {
24+
final T data;
25+
const SdkgenErrorWithData(super.message, super.request, this.data);
2626

2727
@override
2828
String toString() => '$runtimeType: $message';
@@ -65,15 +65,23 @@ class SdkgenHttpClient {
6565
growable: false,
6666
));
6767

68-
dynamic _createError(String type, String message, dynamic data) {
68+
SdkgenError _createError(
69+
String type,
70+
String message,
71+
dynamic data,
72+
Json request,
73+
) {
6974
final description = errTable[type] ?? errTable['Fatal']!;
7075
final decodedData = decode(
7176
typeTable,
7277
'$type.data',
7378
description.dataType,
7479
data,
7580
);
76-
return Function.apply(description.create, [message, decodedData]);
81+
return Function.apply(
82+
description.create,
83+
[message, request, decodedData],
84+
);
7785
}
7886

7987
Future<String> _deviceId() async {
@@ -95,6 +103,11 @@ class SdkgenHttpClient {
95103
String functionName,
96104
Map<String, Object?> args,
97105
) async {
106+
final Json requestInfo = {
107+
'requestId': _randomBytesHex(16),
108+
'name': functionName,
109+
};
110+
98111
try {
99112
final func = fnTable[functionName]!;
100113
final encodedArgs = args.map(
@@ -109,11 +122,11 @@ class SdkgenHttpClient {
109122
),
110123
);
111124

112-
final body = <String, dynamic>{
125+
requestInfo['args'] = encodedArgs;
126+
127+
final Json body = {
128+
...requestInfo,
113129
'version': 3,
114-
'requestId': _randomBytesHex(16),
115-
'name': functionName,
116-
'args': encodedArgs,
117130
'extra': extra,
118131
'deviceInfo': await getDeviceInfo(await _deviceId())
119132
};
@@ -129,8 +142,12 @@ class SdkgenHttpClient {
129142
final responseBody = jsonDecode(utf8.decode(response.bodyBytes));
130143

131144
if (responseBody['error'] != null) {
132-
throw _createError(responseBody['error']['type'],
133-
responseBody['error']['message'], responseBody['error']['data']);
145+
throw _createError(
146+
responseBody['error']['type'],
147+
responseBody['error']['message'],
148+
responseBody['error']['data'],
149+
requestInfo,
150+
);
134151
} else {
135152
final response = decode(
136153
typeTable, '$functionName.ret', func.ret, responseBody['result']);
@@ -140,11 +157,11 @@ class SdkgenHttpClient {
140157
return response;
141158
}
142159
} catch (e) {
143-
if (e is SdkgenError || e is SdkgenErrorWithData) {
160+
if (e is SdkgenError) {
144161
await interceptors.onError?.call(e);
145162
rethrow;
146163
} else {
147-
final error = _createError('Fatal', e.toString(), null);
164+
final error = _createError('Fatal', e.toString(), null, requestInfo);
148165
await interceptors.onError?.call(error);
149166

150167
throw error;

dart-runtime/lib/types.dart

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import 'dart:convert';
22
import 'package:decimal/decimal.dart';
33

4+
import 'http_client.dart';
5+
6+
typedef Json = Map<String, dynamic>;
7+
48
class SdkgenTypeException implements Exception {
59
String cause;
610
SdkgenTypeException(this.cause);
@@ -34,7 +38,7 @@ class FunctionDescription {
3438

3539
class SdkgenErrorDescription {
3640
String dataType;
37-
Function create;
41+
SdkgenError Function(String msg, Json req, dynamic data) create;
3842
SdkgenErrorDescription(this.dataType, this.create);
3943
}
4044

@@ -44,24 +48,28 @@ class LatLng {
4448
LatLng(this.lat, this.lng);
4549
}
4650

47-
const simpleStringTypes = ['string', 'cnpj', 'cpf', 'email', 'html', 'xml'];
48-
var simpleTypes = [
49-
'json',
50-
'bool',
51-
'url',
52-
'int',
53-
'uint',
54-
'float',
55-
'money',
56-
'hex',
57-
'uuid',
58-
'base64',
59-
'void'
60-
] +
61-
simpleStringTypes;
51+
const simpleStringTypes = {'string', 'cnpj', 'cpf', 'email', 'html', 'xml'};
52+
const simpleTypes = {
53+
'json',
54+
'bool',
55+
'url',
56+
'int',
57+
'uint',
58+
'float',
59+
'money',
60+
'hex',
61+
'uuid',
62+
'base64',
63+
'void',
64+
...simpleStringTypes,
65+
};
6266

6367
dynamic simpleEncodeDecode(
64-
Map<String, Object> typeTable, String path, String type, Object? value) {
68+
Map<String, Object> typeTable,
69+
String path,
70+
String type,
71+
Object? value,
72+
) {
6573
if (simpleStringTypes.contains(type)) {
6674
if (value is! String) {
6775
throw SdkgenTypeException(
@@ -143,7 +151,11 @@ dynamic simpleEncodeDecode(
143151
}
144152

145153
dynamic encode(
146-
Map<String, Object> typeTable, String path, Object type, Object? value) {
154+
Map<String, Object> typeTable,
155+
String path,
156+
Object type,
157+
Object? value,
158+
) {
147159
if (type is EnumTypeDescription) {
148160
if (!type.enumValues.contains(value) || value == null) {
149161
throw SdkgenTypeException(
@@ -155,11 +167,16 @@ dynamic encode(
155167
throw SdkgenTypeException(
156168
'Invalid Type at \'$path\', expected ${type.type}, got ${jsonEncode(value)}');
157169
}
158-
var map = Function.apply(type.exportAsMap, [value]) as Map<String, Object?>;
159-
var resultMap = {};
170+
final map =
171+
Function.apply(type.exportAsMap, [value]) as Map<String, Object?>;
172+
final resultMap = {};
160173
map.forEach((fieldName, fieldValue) {
161174
resultMap[fieldName] = encode(
162-
typeTable, '$path.$fieldName', type.fields[fieldName]!, fieldValue);
175+
typeTable,
176+
'$path.$fieldName',
177+
type.fields[fieldName]!,
178+
fieldValue,
179+
);
163180
});
164181
return resultMap;
165182
} else if (type is String) {
@@ -168,7 +185,11 @@ dynamic encode(
168185
return null;
169186
} else {
170187
return encode(
171-
typeTable, path, type.substring(0, type.length - 1), value);
188+
typeTable,
189+
path,
190+
type.substring(0, type.length - 1),
191+
value,
192+
);
172193
}
173194
} else if (type.endsWith('[]')) {
174195
if (value is! List) {
@@ -178,8 +199,12 @@ dynamic encode(
178199
return value
179200
.asMap()
180201
.entries
181-
.map((entry) => encode(typeTable, '$path[${entry.key}]',
182-
type.substring(0, type.length - 2), entry.value))
202+
.map((entry) => encode(
203+
typeTable,
204+
'$path[${entry.key}]',
205+
type.substring(0, type.length - 2),
206+
entry.value,
207+
))
183208
.toList();
184209
} else {
185210
switch (type) {
@@ -248,10 +273,14 @@ dynamic decode(
248273
throw SdkgenTypeException(
249274
'Invalid Type at \'$path\', expected ${type.type}, got ${jsonEncode(value)}');
250275
}
251-
var resultMap = {};
276+
final resultMap = {};
252277
for (var fieldName in type.fields.keys) {
253-
resultMap[fieldName] = decode(typeTable, '$path.$fieldName',
254-
type.fields[fieldName]!, value[fieldName]);
278+
resultMap[fieldName] = decode(
279+
typeTable,
280+
'$path.$fieldName',
281+
type.fields[fieldName]!,
282+
value[fieldName],
283+
);
255284
}
256285
return Function.apply(type.createFromFields, [resultMap]);
257286
} else if (type is String) {
@@ -260,7 +289,11 @@ dynamic decode(
260289
return null;
261290
} else {
262291
return decode(
263-
typeTable, path, type.substring(0, type.length - 1), value);
292+
typeTable,
293+
path,
294+
type.substring(0, type.length - 1),
295+
value,
296+
);
264297
}
265298
} else if (type.endsWith('[]')) {
266299
if (value is! List) {
@@ -270,8 +303,12 @@ dynamic decode(
270303
return value
271304
.asMap()
272305
.entries
273-
.map((entry) => decode(typeTable, '$path[${entry.key}]',
274-
type.substring(0, type.length - 2), entry.value))
306+
.map((entry) => decode(
307+
typeTable,
308+
'$path[${entry.key}]',
309+
type.substring(0, type.length - 2),
310+
entry.value,
311+
))
275312
.toList();
276313
} else {
277314
switch (type) {

0 commit comments

Comments
 (0)
0