Skip to content

Commit

Permalink
Error handling new pocketbase project
Browse files Browse the repository at this point in the history
  • Loading branch information
BhasherBEL committed Mar 26, 2023
1 parent 2768c1f commit e3d1afc
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 52 deletions.
21 changes: 21 additions & 0 deletions lib/model/connectors/pocketbase/provider.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -95,4 +96,24 @@ class PocketBaseProvider extends Provider {
Future<bool> 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),
),
);
}
}
}
34 changes: 8 additions & 26 deletions lib/screens/new_project_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(
Expand Down
55 changes: 29 additions & 26 deletions lib/screens/new_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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!),
),
),
),
),
),
],
],
),
),
),
),
Expand Down

0 comments on commit e3d1afc

Please sign in to comment.