Skip to content

Commit

Permalink
jserror
Browse files Browse the repository at this point in the history
  • Loading branch information
danylo-safonov-solid committed Sep 21, 2023
1 parent 82f0d47 commit 789adf6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 16 deletions.
60 changes: 44 additions & 16 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:js_interop';
import 'dart:js_util';

Check warning on line 2 in example/lib/main.dart

View workflow job for this annotation

GitHub Actions / build

Unused import: 'dart:js_util'.

Try removing the import directive. See https://dart.dev/diagnostics/unused_import to learn more about this problem.

import 'package:deno_postgres_interop/deno_postgres_interop.dart';
import 'package:supabase_functions/supabase_functions.dart';

Expand All @@ -9,34 +12,46 @@ Future<Response> fetch(Request _) async {

final client = Client(dbUrl);
await client.connect();
final result = await client.transaction(
'transaction',
transaction,
TransactionOptions(isolationLevel: IsolationLevel.serializable),
);
await client.end();
try {
final result = await client.transaction(
'transaction',
transaction,
TransactionOptions(isolationLevel: IsolationLevel.serializable),
);
await client.end();

return Response(
[
result.command == CommandType.select,
'warnings = ${result.warnings}',
'''
return Response(
[
result.command == CommandType.select,
'warnings = ${result.warnings}',
'''
rowDescription =
columnCount = ${result.rowDescription?.columnCount}
columns =
${result.rowDescription?.columns.map((e) => ' name = ${e.name}').join('\n')}
''',
result.query.resultType,
...result.rows.map(rowToPrettyString),
].join('\n\n'),
);
result.query.resultType,
...result.rows.map(rowToPrettyString),
].join('\n\n'),
);
} on JSError catch (e) {
await client.end();

return Response('''
${e.name}
${e.cause}
${e.message}
$e
''');
}
}

Future<QueryObjectResult<dynamic>> transaction(Transaction transaction) async {
await transaction.queryObject(
'UPDATE public."User" '
r'SET username=$1 '
"WHERE last_name='user'",
"WHERE last_name='user'"
'AND y = z',
["'user${transaction.hashCode}'"],
);
await Future.delayed(const Duration(seconds: 10));
Expand All @@ -48,3 +63,16 @@ String rowToPrettyString(Map<String, dynamic> row) => row
.toString()
.replaceAll(', ', '\n')
.replaceAllMapped(RegExp(r'\{|\}'), (_) => '');

/// [js/Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error).
@JS('Error')
class JSError {
/// [js/Error/name](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/name).
external String get name;

/// [js/Error/cause](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause).
external Error? get cause;

/// [js/Error/message](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/message).
external String get message;
}
14 changes: 14 additions & 0 deletions lib/src/errors/js_error.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'dart:js_interop';

/// [js/Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error).
@JS('Error')
class JSError {
/// [js/Error/name](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/name).
external String get name;

/// [js/Error/cause](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause).
external Error? get cause;

/// [js/Error/message](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/message).
external String get message;
}

0 comments on commit 789adf6

Please sign in to comment.