Show helpful toast if someone hits a 404

This commit is contained in:
hunteraraujo
2023-09-28 23:43:02 -07:00
parent 88a4d1a0dd
commit 8c58df706a
8 changed files with 69 additions and 32 deletions

View File

@@ -20,7 +20,8 @@ class ChatService {
return await api.post( return await api.post(
'agent/tasks/$taskId/steps', stepRequestBody.toJson()); 'agent/tasks/$taskId/steps', stepRequestBody.toJson());
} catch (e) { } catch (e) {
throw Exception('Failed to execute step: $e'); // TODO: We are bubbling up the full response. Revisit this.
rethrow;
} }
} }

View File

@@ -19,7 +19,8 @@ class TaskService {
try { try {
return await api.post('agent/tasks', taskRequestBody.toJson()); return await api.post('agent/tasks', taskRequestBody.toJson());
} catch (e) { } catch (e) {
throw Exception('Failed to create a new task: $e'); // TODO: We are bubbling up the full response. Revisit this.
rethrow;
} }
} }

View File

@@ -50,7 +50,9 @@ class RestApiUtility {
if (response.statusCode == 200 || response.statusCode == 201) { if (response.statusCode == 200 || response.statusCode == 201) {
return json.decode(response.body); return json.decode(response.body);
} else { } else {
throw Exception('Failed to post data'); // TODO: We are bubbling up the full response to show better errors on the UI.
// Let's put some thought into how we would like to structure this.
throw response;
} }
} }
@@ -66,8 +68,6 @@ class RestApiUtility {
if (response.statusCode == 200 || response.statusCode == 201) { if (response.statusCode == 200 || response.statusCode == 201) {
return json.decode(response.body); return json.decode(response.body);
} else { } else {
print(response.statusCode);
print(response.body);
throw Exception('Failed to update data with PUT request'); throw Exception('Failed to update data with PUT request');
} }
} }

View File

@@ -166,9 +166,9 @@ class ChatViewModel with ChangeNotifier {
} }
print("Chats added for task ID: $_currentTaskId"); print("Chats added for task ID: $_currentTaskId");
} catch (error) { } catch (e) {
// TODO: Bubble up errors to UI // TODO: We are bubbling up the full response. Revisit this.
print("Error sending chat: $error"); rethrow;
// TODO: Handle additional error scenarios or log them as required // TODO: Handle additional error scenarios or log them as required
} finally { } finally {
_isWaitingForAgentResponse = false; _isWaitingForAgentResponse = false;

View File

@@ -26,19 +26,24 @@ class TaskViewModel with ChangeNotifier {
/// Adds a task and returns its ID. /// Adds a task and returns its ID.
Future<String> createTask(String title) async { Future<String> createTask(String title) async {
final newTask = TaskRequestBody(input: title); try {
// Add to data source final newTask = TaskRequestBody(input: title);
final createdTask = await _taskService.createTask(newTask); // Add to data source
// Create a Task object from the created task response final createdTask = await _taskService.createTask(newTask);
final newTaskObject = // Create a Task object from the created task response
Task(id: createdTask['task_id'], title: createdTask['input']); final newTaskObject =
Task(id: createdTask['task_id'], title: createdTask['input']);
fetchAndCombineData(); fetchAndCombineData();
final taskId = newTaskObject.id; final taskId = newTaskObject.id;
print("Task $taskId created successfully!"); print("Task $taskId created successfully!");
return newTaskObject.id; // Return the ID of the new task return newTaskObject.id;
} catch (e) {
// TODO: We are bubbling up the full response. Revisit this.
rethrow;
}
} }
/// Deletes a task. /// Deletes a task.

View File

@@ -8,7 +8,9 @@ import 'package:auto_gpt_flutter_client/views/chat/loading_indicator.dart';
import 'package:auto_gpt_flutter_client/views/chat/user_message_tile.dart'; import 'package:auto_gpt_flutter_client/views/chat/user_message_tile.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:auto_gpt_flutter_client/viewmodels/chat_viewmodel.dart'; import 'package:auto_gpt_flutter_client/viewmodels/chat_viewmodel.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:http/http.dart' as http;
class ChatView extends StatefulWidget { class ChatView extends StatefulWidget {
final ChatViewModel viewModel; final ChatViewModel viewModel;
@@ -118,20 +120,39 @@ class _ChatViewState extends State<ChatView> {
padding: const EdgeInsets.all(8.0), padding: const EdgeInsets.all(8.0),
child: ChatInputField( child: ChatInputField(
onSendPressed: (message) async { onSendPressed: (message) async {
if (widget.viewModel.currentTaskId != null) { try {
widget.viewModel.sendChatMessage( if (widget.viewModel.currentTaskId != null) {
(message == "") ? null : message, widget.viewModel.sendChatMessage(
continuousModeSteps: (message == "") ? null : message,
Provider.of<SettingsViewModel>(context, listen: false) continuousModeSteps: Provider.of<SettingsViewModel>(
.continuousModeSteps); context,
} else { listen: false)
String newTaskId = await taskViewModel.createTask(message); .continuousModeSteps);
widget.viewModel.setCurrentTaskId(newTaskId); } else {
widget.viewModel.sendChatMessage( String newTaskId = await taskViewModel.createTask(message);
(message == "") ? null : message, widget.viewModel.setCurrentTaskId(newTaskId);
continuousModeSteps: widget.viewModel.sendChatMessage(
Provider.of<SettingsViewModel>(context, listen: false) (message == "") ? null : message,
.continuousModeSteps); continuousModeSteps: Provider.of<SettingsViewModel>(
context,
listen: false)
.continuousModeSteps);
}
} catch (response) {
if (response is http.Response && response.statusCode == 404) {
Fluttertoast.showToast(
msg:
"404 error: Please ensure the correct baseURL for your agent in \nthe settings and that your agent adheres to the agent protocol.",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.TOP,
timeInSecForIosWeb: 5,
backgroundColor: Colors.red,
webPosition: "center",
webBgColor:
"linear-gradient(to right, #dc1c13, #dc1c13)",
textColor: Colors.white,
fontSize: 16.0);
}
} }
}, },
onContinuousModePressed: () { onContinuousModePressed: () {

View File

@@ -192,6 +192,14 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
fluttertoast:
dependency: "direct main"
description:
name: fluttertoast
sha256: "474f7d506230897a3cd28c965ec21c5328ae5605fc9c400cd330e9e9d6ac175c"
url: "https://pub.dev"
source: hosted
version: "8.2.2"
google_identity_services_web: google_identity_services_web:
dependency: transitive dependency: transitive
description: description:

View File

@@ -46,6 +46,7 @@ dependencies:
google_sign_in: ^6.1.5 google_sign_in: ^6.1.5
uuid: ^4.0.0 uuid: ^4.0.0
url_launcher: ^6.1.14 url_launcher: ^6.1.14
fluttertoast: ^8.2.2
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: