handles errors in a more use friendly way
Some checks failed
Build and deploy the backend to staging / Deploy to staging (pull_request) Has been cancelled
Build and deploy the backend to staging / Build and push image (pull_request) Has been cancelled
Run linting on the backend code / Build (pull_request) Has been cancelled

This commit is contained in:
2025-04-06 19:27:26 +02:00
parent 8ef60104f0
commit 720e4d1c17
15 changed files with 527 additions and 51 deletions

View File

@@ -1,5 +1,7 @@
import "dart:async";
import "dart:convert";
import "dart:developer";
import "dart:io";
import "package:anyway/main.dart";
import 'package:dio/dio.dart';
@@ -18,11 +20,6 @@ Dio dio = Dio(
// also accept 500 errors, since we cannot rule out that the server is at fault. We still want to gracefully handle these errors
validateStatus: (status) => status! <= 500,
receiveDataWhenStatusError: true,
// api is notoriously slow
// headers: {
// HttpHeaders.userAgentHeader: 'dio',
// 'api': '1.0.0',
// },
contentType: Headers.jsonContentType,
responseType: ResponseType.json,
),
@@ -48,25 +45,70 @@ fetchTrip(
);
} catch (e) {
trip.updateUUID("error");
trip.updateError(e.toString());
// Format the error message to be more user friendly
String errorDescription;
if (e is DioException) {
errorDescription = e.message ?? "Unknown error";
} else if (e is SocketException) {
errorDescription = "No internet connection";
} else if (e is TimeoutException) {
errorDescription = "Request timed out";
} else {
errorDescription = "Unknown error";
}
String errorMessage = """
We're sorry, the following error was generated:
${errorDescription.trim()}
""".trim();
trip.updateError(errorMessage);
log(e.toString());
log(errorMessage);
return;
}
// handle errors
// handle more specific errors
if (response.statusCode != 200) {
trip.updateUUID("error");
String errorDetail;
String errorDescription;
if (response.data.runtimeType == String) {
errorDetail = response.data;
errorDescription = response.data;
} else if (response.data.runtimeType == Map<String, dynamic>) {
errorDescription = response.data["detail"] ?? "Unknown error";
} else {
errorDetail = response.data["detail"] ?? "Unknown error";
errorDescription = "Unknown error";
}
trip.updateError(errorDetail);
log(errorDetail);
String errorMessage = """
We're sorry, our servers generated the following error:
${errorDescription.trim()}
Please try again.
""".trim();
trip.updateError(errorMessage);
log(errorMessage);
// Actualy no need to throw an exception, we can just log the error and let the user retry
// throw Exception(errorDetail);
} else {
// if the response data is not json, throw an error
if (response.data is! Map<String, dynamic>) {
log("${response.data.runtimeType}");
trip.updateUUID("error");
String errorMessage = """
We're sorry, our servers generated the following error:
${response.data.trim()}
Please try again.
""".trim();
trip.updateError(errorMessage);
log(errorMessage);
return;
}
Map<String, dynamic> json = response.data;
// only fill in the trip "meta" data for now