Skip to content

Commit

Permalink
Show helpful toast if someone hits a 404
Browse files Browse the repository at this point in the history
  • Loading branch information
hunteraraujo committed Sep 29, 2023
1 parent 88a4d1a commit 8c58df7
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 35 deletions.
3 changes: 2 additions & 1 deletion frontend/lib/services/chat_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class ChatService {
return await api.post(
'agent/tasks/$taskId/steps', stepRequestBody.toJson());
} catch (e) {
throw Exception('Failed to execute step: $e');
// TODO: We are bubbling up the full response. Revisit this.
rethrow;
}
}

Expand Down
3 changes: 2 additions & 1 deletion frontend/lib/services/task_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class TaskService {
try {
return await api.post('agent/tasks', taskRequestBody.toJson());
} catch (e) {
throw Exception('Failed to create a new task: $e');
// TODO: We are bubbling up the full response. Revisit this.
rethrow;
}
}

Expand Down
6 changes: 3 additions & 3 deletions frontend/lib/utils/rest_api_utility.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ class RestApiUtility {
if (response.statusCode == 200 || response.statusCode == 201) {
return json.decode(response.body);
} 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;
}
}

Expand All @@ -66,8 +68,6 @@ class RestApiUtility {
if (response.statusCode == 200 || response.statusCode == 201) {
return json.decode(response.body);
} else {
print(response.statusCode);
print(response.body);
throw Exception('Failed to update data with PUT request');
}
}
Expand Down
6 changes: 3 additions & 3 deletions frontend/lib/viewmodels/chat_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ class ChatViewModel with ChangeNotifier {
}

print("Chats added for task ID: $_currentTaskId");
} catch (error) {
// TODO: Bubble up errors to UI
print("Error sending chat: $error");
} catch (e) {
// TODO: We are bubbling up the full response. Revisit this.
rethrow;
// TODO: Handle additional error scenarios or log them as required
} finally {
_isWaitingForAgentResponse = false;
Expand Down
31 changes: 18 additions & 13 deletions frontend/lib/viewmodels/task_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,24 @@ class TaskViewModel with ChangeNotifier {

/// Adds a task and returns its ID.
Future<String> createTask(String title) async {
final newTask = TaskRequestBody(input: title);
// Add to data source
final createdTask = await _taskService.createTask(newTask);
// Create a Task object from the created task response
final newTaskObject =
Task(id: createdTask['task_id'], title: createdTask['input']);

fetchAndCombineData();

final taskId = newTaskObject.id;
print("Task $taskId created successfully!");

return newTaskObject.id; // Return the ID of the new task
try {
final newTask = TaskRequestBody(input: title);
// Add to data source
final createdTask = await _taskService.createTask(newTask);
// Create a Task object from the created task response
final newTaskObject =
Task(id: createdTask['task_id'], title: createdTask['input']);

fetchAndCombineData();

final taskId = newTaskObject.id;
print("Task $taskId created successfully!");

return newTaskObject.id;
} catch (e) {
// TODO: We are bubbling up the full response. Revisit this.
rethrow;
}
}

/// Deletes a task.
Expand Down
49 changes: 35 additions & 14 deletions frontend/lib/views/chat/chat_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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:flutter/material.dart';
import 'package:auto_gpt_flutter_client/viewmodels/chat_viewmodel.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import 'package:http/http.dart' as http;

class ChatView extends StatefulWidget {
final ChatViewModel viewModel;
Expand Down Expand Up @@ -118,20 +120,39 @@ class _ChatViewState extends State<ChatView> {
padding: const EdgeInsets.all(8.0),
child: ChatInputField(
onSendPressed: (message) async {
if (widget.viewModel.currentTaskId != null) {
widget.viewModel.sendChatMessage(
(message == "") ? null : message,
continuousModeSteps:
Provider.of<SettingsViewModel>(context, listen: false)
.continuousModeSteps);
} else {
String newTaskId = await taskViewModel.createTask(message);
widget.viewModel.setCurrentTaskId(newTaskId);
widget.viewModel.sendChatMessage(
(message == "") ? null : message,
continuousModeSteps:
Provider.of<SettingsViewModel>(context, listen: false)
.continuousModeSteps);
try {
if (widget.viewModel.currentTaskId != null) {
widget.viewModel.sendChatMessage(
(message == "") ? null : message,
continuousModeSteps: Provider.of<SettingsViewModel>(
context,
listen: false)
.continuousModeSteps);
} else {
String newTaskId = await taskViewModel.createTask(message);
widget.viewModel.setCurrentTaskId(newTaskId);
widget.viewModel.sendChatMessage(
(message == "") ? null : message,
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: () {
Expand Down
8 changes: 8 additions & 0 deletions frontend/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ packages:
description: flutter
source: sdk
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:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions frontend/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dependencies:
google_sign_in: ^6.1.5
uuid: ^4.0.0
url_launcher: ^6.1.14
fluttertoast: ^8.2.2

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 8c58df7

Please sign in to comment.