Skip to content

Commit e3d1afc

Browse files
committed
Error handling new pocketbase project
1 parent 2768c1f commit e3d1afc

File tree

3 files changed

+58
-52
lines changed

3 files changed

+58
-52
lines changed

lib/model/connectors/pocketbase/provider.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flutter/material.dart';
12
import 'package:pocketbase/pocketbase.dart';
23
import 'package:shared/model/connectors/local/deleted.dart';
34
import 'package:shared/model/connectors/pocketbase/deleted.dart';
@@ -95,4 +96,24 @@ class PocketBaseProvider extends Provider {
9596
Future<bool> joinWithTitle() async {
9697
return await PocketBaseProject(project, pb).join();
9798
}
99+
100+
static void onClientException(ClientException e, BuildContext c) {
101+
String message;
102+
103+
if (e.statusCode != 0 && e.response.containsKey('message')) {
104+
message = 'Error ${e.statusCode}: ${e.response['message']}';
105+
} else if (e.originalError != null) {
106+
message = e.originalError.toString();
107+
} else {
108+
message = e.toString();
109+
}
110+
111+
if (c.mounted) {
112+
ScaffoldMessenger.of(c).showSnackBar(
113+
SnackBar(
114+
content: Text(message),
115+
),
116+
);
117+
}
118+
}
98119
}

lib/screens/new_project_screen.dart

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:math';
22

33
import 'package:flutter/material.dart';
44
import 'package:pocketbase/pocketbase.dart';
5+
import 'package:shared/model/connectors/pocketbase/provider.dart';
56
import 'package:shared/model/connectors/provider.dart';
67
import 'package:shared/model/project_data.dart';
78
import 'package:shared/screens/new_screen.dart';
@@ -55,34 +56,15 @@ class NewProjectScreen extends StatelessWidget {
5556
}
5657
try {
5758
await project!.provider.connect();
58-
} on ClientException {
59-
print('Connection error');
60-
if (context.mounted) {
61-
ScaffoldMessenger.of(context).showSnackBar(
62-
const SnackBar(
63-
content: Text("Connection error"),
64-
),
65-
);
66-
}
67-
return;
68-
} catch (e) {
69-
print(e);
70-
if (context.mounted) {
71-
ScaffoldMessenger.of(context).showSnackBar(
72-
SnackBar(
73-
content: Text(e.toString()),
74-
),
75-
);
59+
AppData.current = project;
60+
await project!.conn.save();
61+
if (setupData.join) {
62+
await project!.provider.joinWithTitle();
7663
}
77-
return;
78-
}
79-
AppData.current = project;
80-
await project!.conn.save();
81-
if (setupData.join) {
82-
await project!.provider.joinWithTitle();
83-
}
84-
try {
8564
await project!.sync();
65+
} on ClientException catch (e) {
66+
PocketBaseProvider.onClientException(e, context);
67+
return;
8668
} catch (e) {
8769
if (context.mounted) {
8870
ScaffoldMessenger.of(context).showSnackBar(

lib/screens/new_screen.dart

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,38 @@ class NewScreen extends StatelessWidget {
3232
title: Text(title!),
3333
elevation: 4,
3434
),
35-
body: Form(
36-
key: formKey,
37-
child: SafeArea(
38-
child: Padding(
39-
padding:
40-
const EdgeInsets.symmetric(horizontal: 10, vertical: 20),
41-
child: Column(
42-
children: [
43-
Expanded(
44-
child: page,
45-
),
46-
Padding(
47-
padding: const EdgeInsets.symmetric(
48-
vertical: 10, horizontal: 20),
49-
child: SizedBox(
50-
width: double.infinity,
51-
child: ElevatedButton(
52-
onPressed: onValidate == null
53-
? null
54-
: () => onValidate!(context, formKey),
55-
child: Padding(
56-
padding: const EdgeInsets.all(15.0),
57-
child: Text(
58-
buttonTitle == null ? "Finish" : buttonTitle!),
35+
body: Builder(
36+
builder: (context) => Form(
37+
key: formKey,
38+
child: SafeArea(
39+
child: Padding(
40+
padding:
41+
const EdgeInsets.symmetric(horizontal: 10, vertical: 20),
42+
child: Column(
43+
children: [
44+
Expanded(
45+
child: page,
46+
),
47+
Padding(
48+
padding: const EdgeInsets.symmetric(
49+
vertical: 10, horizontal: 20),
50+
child: SizedBox(
51+
width: double.infinity,
52+
child: ElevatedButton(
53+
onPressed: onValidate == null
54+
? null
55+
: () => onValidate!(context, formKey),
56+
child: Padding(
57+
padding: const EdgeInsets.all(15.0),
58+
child: Text(buttonTitle == null
59+
? "Finish"
60+
: buttonTitle!),
61+
),
5962
),
6063
),
6164
),
62-
),
63-
],
65+
],
66+
),
6467
),
6568
),
6669
),

0 commit comments

Comments
 (0)