mirror of
https://github.com/foss42/apidash.git
synced 2025-06-03 08:16:25 +08:00
feat:add dio code gen
This commit is contained in:
26
lib/codegen/dart/shared.dart
Normal file
26
lib/codegen/dart/shared.dart
Normal file
@ -0,0 +1,26 @@
|
||||
import 'package:code_builder/code_builder.dart';
|
||||
|
||||
Block declareTryCatch({
|
||||
required List<Expression> body,
|
||||
required Map<String?, List<Expression>> onError,
|
||||
bool showStackStrace = false,
|
||||
}) {
|
||||
return Block((b) {
|
||||
b.statements.add(const Code('try'));
|
||||
b.statements.add(const Code('{'));
|
||||
b.statements.addAll(body.map((e) => e.statement).toList());
|
||||
final entries = onError.entries;
|
||||
|
||||
for (var error in entries) {
|
||||
b.statements.add(const Code('}'));
|
||||
if (error.key != null) {
|
||||
b.statements.add(Code('on ${error.key}'));
|
||||
}
|
||||
b.statements.add(Code(showStackStrace ? 'catch(e,s)' : 'catch(e)'));
|
||||
|
||||
b.statements.add(const Code('{'));
|
||||
b.statements.addAll(error.value.map((e) => e.statement).toList());
|
||||
if (entries.last.key == error.key) b.statements.add(const Code('}'));
|
||||
}
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user