Skip to content

Commit

Permalink
Handle errors from backend (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsreichardt authored Oct 16, 2023
1 parent 5af5bb2 commit 7283988
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 44 deletions.
90 changes: 46 additions & 44 deletions lib/src/pages/session_page/error_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,55 +27,57 @@ class ErrorCard extends ConsumerWidget {
color: Theme.of(context).colorScheme.error,
),
const SizedBox(width: 12),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(left: 12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
Flexible(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(left: 12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Error!',
style: TextStyle(
color: Theme.of(context).colorScheme.error,
fontSize: 20,
fontWeight: FontWeight.w600),
),
const SizedBox(height: 4),
Text(
text ?? 'Something went wrong. Please try again.',
style: TextStyle(
color: Theme.of(context).colorScheme.error),
),
],
),
),
const SizedBox(height: 12),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
'Error!',
style: TextStyle(
color: Theme.of(context).colorScheme.error,
fontSize: 20,
fontWeight: FontWeight.w600),
TextButton(
onPressed: () {
ref.read(generateNotifierProvider.notifier).submit();
},
style: TextButton.styleFrom(
foregroundColor: Theme.of(context).colorScheme.error,
),
child: const Text('RETRY'),
),
const SizedBox(height: 4),
Text(
text ?? 'Something went wrong. Please try again.',
style: TextStyle(
color: Theme.of(context).colorScheme.error),
const SizedBox(width: 12),
TextButton(
onPressed: () => launchUrl(
Uri.parse('https://ankigpt.help/support')),
style: TextButton.styleFrom(
foregroundColor: Theme.of(context).colorScheme.error,
),
child: const Text('CONTACT SUPPORT'),
),
],
),
),
const SizedBox(height: 12),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
TextButton(
onPressed: () {
ref.read(generateNotifierProvider.notifier).submit();
},
style: TextButton.styleFrom(
foregroundColor: Theme.of(context).colorScheme.error,
),
child: const Text('RETRY'),
),
const SizedBox(width: 12),
TextButton(
onPressed: () =>
launchUrl(Uri.parse('https://ankigpt.help/support')),
style: TextButton.styleFrom(
foregroundColor: Theme.of(context).colorScheme.error,
),
child: const Text('CONTACT SUPPORT'),
),
],
),
],
],
),
),
],
),
Expand Down
40 changes: 40 additions & 0 deletions lib/src/providers/generate_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import 'package:ankigpt/src/providers/router_provider.dart';
import 'package:ankigpt/src/providers/session_repository_provider.dart';
import 'package:ankigpt/src/providers/user_repository_provider.dart';
import 'package:ankigpt/src/providers/wants_to_generate_provider.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cloud_functions/cloud_functions.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
Expand Down Expand Up @@ -119,10 +121,40 @@ class GenerateNotifier extends _$GenerateNotifier {
router.go('/deck/$sessionId');
} catch (e, s) {
_logger.e("Failed to generate cards", e, s);

if (e is FirebaseFunctionsException) {
state = GenerateState.error(
message: _getErrorMessageForFunctionsException(e));
return;
}
state = GenerateState.error(message: '$e');
}
}

String _getErrorMessageForFunctionsException(FirebaseFunctionsException e) {
if (e.message == 'too-long-input-exception') {
return 'Your input is too long. Please try again with a shorter input.';
}

if (e.message == 'too-less-input-exception') {
return 'Your input is too short. Please try again with a longer input (at least 400 characters). In case you uploaded a file, ensure that the file contains enough text (text from images is not supported). Or copy the text from the file and paste it into the text field.';
}

if (e.message == 'no-input-provided-exception') {
return 'The provided input was empty. Please try again with a non-empty input. In case you uploaded a file, ensure that the file contains enough text (text from images is not supported). Or copy the text from the file and paste it into the text field.';
}

if (e.message == 'could-not-read-pdf-exception') {
return 'The provided file could not be read. Please try again with a different file. Make sure your PDF is not passwort protected. Or copy the text from the file and paste it into the text field.';
}

if (e.message == 'unknown-language-exception') {
return 'It was not possible to detect the language of the input. Please try again with a different input. In case you uploading a file, ensure that the file contains enough text (text from images is not supported). Also, it could be that the text in your file is not readable by our OCR. In this case, please copy the text from the file and paste it into the text field.';
}

return 'An unknown error occurred. Please try again or contact the support.';
}

void _throwIfFreeLimitReached(CardGenrationSize size) {
if (!_hasPlus) {
final remainingFreeLimit = freeUsageLimitPerMonth - _currentMonthUsage;
Expand Down Expand Up @@ -205,6 +237,14 @@ class GenerateNotifier extends _$GenerateNotifier {
_logger.d('Uploaded file');
return true;
} catch (e) {
if (e is FirebaseException && e.code == 'unauthorized') {
_logger.e('Unauthorized to upload file', e);
state = const GenerateState.error(
message:
'Uploading the file was blocked. This could be because your file is too large. Please try again with a smaller file (< 100 MB).');
return false;
}

state = GenerateState.error(message: 'Failed to upload file: $e');
_logger.e('Failed to upload file', e);
return false;
Expand Down

0 comments on commit 7283988

Please sign in to comment.