From e3d1afc2ecae725cbf8d250a92778f17652eb542 Mon Sep 17 00:00:00 2001 From: Bhasher Date: Sun, 26 Mar 2023 19:33:56 +0200 Subject: [PATCH] Error handling new pocketbase project --- lib/model/connectors/pocketbase/provider.dart | 21 +++++++ lib/screens/new_project_screen.dart | 34 +++--------- lib/screens/new_screen.dart | 55 ++++++++++--------- 3 files changed, 58 insertions(+), 52 deletions(-) diff --git a/lib/model/connectors/pocketbase/provider.dart b/lib/model/connectors/pocketbase/provider.dart index bd9dee8..17adf7b 100644 --- a/lib/model/connectors/pocketbase/provider.dart +++ b/lib/model/connectors/pocketbase/provider.dart @@ -1,3 +1,4 @@ +import 'package:flutter/material.dart'; import 'package:pocketbase/pocketbase.dart'; import 'package:shared/model/connectors/local/deleted.dart'; import 'package:shared/model/connectors/pocketbase/deleted.dart'; @@ -95,4 +96,24 @@ class PocketBaseProvider extends Provider { Future joinWithTitle() async { return await PocketBaseProject(project, pb).join(); } + + static void onClientException(ClientException e, BuildContext c) { + String message; + + if (e.statusCode != 0 && e.response.containsKey('message')) { + message = 'Error ${e.statusCode}: ${e.response['message']}'; + } else if (e.originalError != null) { + message = e.originalError.toString(); + } else { + message = e.toString(); + } + + if (c.mounted) { + ScaffoldMessenger.of(c).showSnackBar( + SnackBar( + content: Text(message), + ), + ); + } + } } diff --git a/lib/screens/new_project_screen.dart b/lib/screens/new_project_screen.dart index 5be9214..da43b50 100644 --- a/lib/screens/new_project_screen.dart +++ b/lib/screens/new_project_screen.dart @@ -2,6 +2,7 @@ import 'dart:math'; import 'package:flutter/material.dart'; import 'package:pocketbase/pocketbase.dart'; +import 'package:shared/model/connectors/pocketbase/provider.dart'; import 'package:shared/model/connectors/provider.dart'; import 'package:shared/model/project_data.dart'; import 'package:shared/screens/new_screen.dart'; @@ -55,34 +56,15 @@ class NewProjectScreen extends StatelessWidget { } try { await project!.provider.connect(); - } on ClientException { - print('Connection error'); - if (context.mounted) { - ScaffoldMessenger.of(context).showSnackBar( - const SnackBar( - content: Text("Connection error"), - ), - ); - } - return; - } catch (e) { - print(e); - if (context.mounted) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text(e.toString()), - ), - ); + AppData.current = project; + await project!.conn.save(); + if (setupData.join) { + await project!.provider.joinWithTitle(); } - return; - } - AppData.current = project; - await project!.conn.save(); - if (setupData.join) { - await project!.provider.joinWithTitle(); - } - try { await project!.sync(); + } on ClientException catch (e) { + PocketBaseProvider.onClientException(e, context); + return; } catch (e) { if (context.mounted) { ScaffoldMessenger.of(context).showSnackBar( diff --git a/lib/screens/new_screen.dart b/lib/screens/new_screen.dart index 5fabe3e..92bd660 100644 --- a/lib/screens/new_screen.dart +++ b/lib/screens/new_screen.dart @@ -32,35 +32,38 @@ class NewScreen extends StatelessWidget { title: Text(title!), elevation: 4, ), - body: Form( - key: formKey, - child: SafeArea( - child: Padding( - padding: - const EdgeInsets.symmetric(horizontal: 10, vertical: 20), - child: Column( - children: [ - Expanded( - child: page, - ), - Padding( - padding: const EdgeInsets.symmetric( - vertical: 10, horizontal: 20), - child: SizedBox( - width: double.infinity, - child: ElevatedButton( - onPressed: onValidate == null - ? null - : () => onValidate!(context, formKey), - child: Padding( - padding: const EdgeInsets.all(15.0), - child: Text( - buttonTitle == null ? "Finish" : buttonTitle!), + body: Builder( + builder: (context) => Form( + key: formKey, + child: SafeArea( + child: Padding( + padding: + const EdgeInsets.symmetric(horizontal: 10, vertical: 20), + child: Column( + children: [ + Expanded( + child: page, + ), + Padding( + padding: const EdgeInsets.symmetric( + vertical: 10, horizontal: 20), + child: SizedBox( + width: double.infinity, + child: ElevatedButton( + onPressed: onValidate == null + ? null + : () => onValidate!(context, formKey), + child: Padding( + padding: const EdgeInsets.all(15.0), + child: Text(buttonTitle == null + ? "Finish" + : buttonTitle!), + ), ), ), ), - ), - ], + ], + ), ), ), ),