Skip to content

Commit

Permalink
🛠 Add error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabi755 committed Mar 7, 2024
1 parent b61cce0 commit 92fa239
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 36 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ CHANGELOG

## 2.0.1 (12.02.204) ##

* 🛠 Add logging framework ([#66](https://github.com/tankste/app/issues/66))
* 🛠 Add error logging
* ➕ Add logging framework ([#66](https://github.com/tankste/app/issues/66))
* 🐞 Fix initial state hangup ([#64](https://github.com/tankste/app/issues/64))

## 2.0.0 (29.01.2024) ##
Expand Down
2 changes: 1 addition & 1 deletion app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: tankste
description: Finde die günstigste Tankstelle in deiner Nähe!
publish_to: 'none'

version: 2.0.1+26
version: 2.0.1+27

environment:
sdk: ">=2.19.6 <3.0.0"
Expand Down
4 changes: 4 additions & 0 deletions core/lib/log/log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ class Log {
static void e(String message) {
_logger.e(message);
}

static void exception(Exception exception) {
_logger.t(exception);
}
}
10 changes: 8 additions & 2 deletions sponsor/lib/repository/balance_repository.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:convert';
import 'package:core/log/log.dart';
import 'package:http/http.dart' as http;
import 'package:multiple_result/multiple_result.dart';
import 'package:sponsor/model/balance_model.dart';
Expand Down Expand Up @@ -39,7 +40,9 @@ class TanksteWebBalanceRepository extends BalanceRepository {
Result<ConfigModel, Exception> configResult =
await _configRepository.get().first;
if (configResult.isError()) {
return Result.error(configResult.tryGetError()!);
Exception error = configResult.tryGetError()!;
Log.exception(error);
return Result.error(error);
}
ConfigModel config = configResult.tryGetSuccess()!;

Expand All @@ -55,9 +58,12 @@ class TanksteWebBalanceRepository extends BalanceRepository {

return Result.success(balance);
} else {
return Result.error(Exception("API Error!\n\n${response.body}"));
Exception error = Exception("API Error!\n\n${response.body}");
Log.exception(error);
return Result.error(error);
}
} on Exception catch (e) {
Log.exception(e);
return Result.error(e);
}
}
Expand Down
37 changes: 27 additions & 10 deletions sponsor/lib/repository/comment_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import 'dart:async';
import 'dart:convert';
import 'package:core/device/model/device_model.dart';
import 'package:core/device/repository/device_repository.dart';
import 'package:core/log/log.dart';
import 'package:http/http.dart' as http;
import 'package:multiple_result/multiple_result.dart';
import 'package:sponsor/model/balance_model.dart';
import 'package:sponsor/model/comment_model.dart';
import 'package:sponsor/model/config_model.dart';
import 'package:sponsor/repository/config_repository.dart';
import 'package:sponsor/repository/dto/balance_dto.dart';
import 'package:sponsor/repository/dto/comment_dto.dart';

abstract class CommentRepository {
Expand Down Expand Up @@ -53,7 +52,9 @@ class TanksteWebCommentRepository extends CommentRepository {
Result<ConfigModel, Exception> configResult =
await _configRepository.get().first;
if (configResult.isError()) {
return Result.error(configResult.tryGetError()!);
Exception error = configResult.tryGetError()!;
Log.exception(error);
return Result.error(error);
}
ConfigModel config = configResult.tryGetSuccess()!;

Expand All @@ -69,9 +70,12 @@ class TanksteWebCommentRepository extends CommentRepository {

return Result.success(comments);
} else {
return Result.error(Exception("API Error!\n\n${response.body}"));
Exception error = Exception("API Error!\n\n${response.body}");
Log.exception(error);
return Result.error(error);
}
} on Exception catch (e) {
Log.exception(e);
return Result.error(e);
}
}
Expand All @@ -88,14 +92,18 @@ class TanksteWebCommentRepository extends CommentRepository {
Result<ConfigModel, Exception> configResult =
await _configRepository.get().first;
if (configResult.isError()) {
return Result.error(configResult.tryGetError()!);
Exception error = configResult.tryGetError()!;
Log.exception(error);
return Result.error(error);
}
ConfigModel config = configResult.tryGetSuccess()!;

Result<DeviceModel, Exception> deviceResult =
await _deviceRepository.get().first;
if (deviceResult.isError()) {
return Result.error(deviceResult.tryGetError()!);
Exception error = deviceResult.tryGetError()!;
Log.exception(error);
return Result.error(error);
}
DeviceModel device = deviceResult.tryGetSuccess()!;

Expand All @@ -111,9 +119,12 @@ class TanksteWebCommentRepository extends CommentRepository {

return Result.success(comment);
} else {
return Result.error(Exception("API Error!\n\n${response.body}"));
Exception error = Exception("API Error!\n\n${response.body}");
Log.exception(error);
return Result.error(error);
}
} on Exception catch (e) {
Log.exception(e);
return Result.error(e);
}
}
Expand All @@ -137,14 +148,18 @@ class TanksteWebCommentRepository extends CommentRepository {
Result<ConfigModel, Exception> configResult =
await _configRepository.get().first;
if (configResult.isError()) {
return Result.error(configResult.tryGetError()!);
Exception error = configResult.tryGetError()!;
Log.exception(error);
return Result.error(error);
}
ConfigModel config = configResult.tryGetSuccess()!;

Result<DeviceModel, Exception> deviceResult =
await _deviceRepository.get().first;
if (deviceResult.isError()) {
return Result.error(deviceResult.tryGetError()!);
Exception error = deviceResult.tryGetError()!;
Log.exception(error);
return Result.error(error);
}
DeviceModel device = deviceResult.tryGetSuccess()!;

Expand All @@ -160,7 +175,9 @@ class TanksteWebCommentRepository extends CommentRepository {

return Result.success(comment);
} else {
return Result.error(Exception("API Error!\n\n${response.body}"));
Exception error = Exception("API Error!\n\n${response.body}");
Log.exception(error);
return Result.error(error);
}
} on Exception catch (e) {
return Result.error(e);
Expand Down
2 changes: 2 additions & 0 deletions sponsor/lib/repository/config_repository.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:core/log/log.dart';
import 'package:multiple_result/multiple_result.dart';
import 'package:sponsor/model/config_model.dart';
import 'package:core/config/model/config_model.dart' as core;
Expand Down Expand Up @@ -36,6 +37,7 @@ class LocalConfigRepository extends ConfigRepository {
apiBaseUrl: jsonStationConfig["apiBaseUrl"] ?? "",
));
} on Exception catch (e) {
Log.exception(e);
return Result.error(e);
}
}
Expand Down
48 changes: 36 additions & 12 deletions sponsor/lib/repository/product_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';

import 'package:core/device/model/device_model.dart';
import 'package:core/device/repository/device_repository.dart';
import 'package:core/log/log.dart';
import 'package:in_app_purchase/in_app_purchase.dart';
import 'package:in_app_purchase_android/in_app_purchase_android.dart';
import 'package:in_app_purchase_storekit/in_app_purchase_storekit.dart';
Expand Down Expand Up @@ -72,15 +73,19 @@ class MobileProductRepository extends ProductRepository {
Future<Result<ProductModel, Exception>> _getAsync(String id) async {
try {
if (!await InAppPurchase.instance.isAvailable()) {
return Result.error(Exception("InAppPurchase is not available"));
Exception error = Exception("InAppPurchase is not available");
Log.exception(error);
return Result.error(error);
}

final ProductDetailsResponse response =
await InAppPurchase.instance.queryProductDetails({id});

if (response.notFoundIDs.isNotEmpty) {
return Result.error(Exception(
"Product '$id' not found. Reason: ${response.error?.message}"));
Exception error = Exception(
"Product '$id' not found. Reason: ${response.error?.message}");
Log.exception(error);
return Result.error(error);
}

ProductDetails productDto = response.productDetails.first;
Expand All @@ -93,6 +98,7 @@ class MobileProductRepository extends ProductRepository {
);
return Result.success(product);
} on Exception catch (e) {
Log.exception(e);
return Result.error(e);
}
}
Expand All @@ -115,7 +121,9 @@ class MobileProductRepository extends ProductRepository {
Future<Result<void, Exception>> _purchaseAsync(String id) async {
try {
if (!await InAppPurchase.instance.isAvailable()) {
return Result.error(Exception("InAppPurchase is not available"));
Exception error = Exception("InAppPurchase is not available");
Log.exception(error);
return Result.error(error);
}

final ProductDetailsResponse response =
Expand All @@ -125,7 +133,9 @@ class MobileProductRepository extends ProductRepository {
Result<DeviceModel, Exception> deviceResult =
await _deviceRepository.get().first;
if (deviceResult.isError()) {
return Result.error(deviceResult.tryGetError()!);
Exception error = Exception(deviceResult.tryGetError()!);
Log.exception(error);
return Result.error(error);
}
DeviceModel device = deviceResult.tryGetSuccess()!;

Expand Down Expand Up @@ -166,9 +176,12 @@ class MobileProductRepository extends ProductRepository {
provider: PurchaseProvider.appleStore))
.first;
} else {
return Result.error(Exception("Unsupported purchase details!"));
Exception error = Exception("Unsupported purchase details!");
Log.exception(error);
return Result.error(error);
}
} on Exception catch (e) {
Log.exception(e);
return Result.error(e);
}
}
Expand All @@ -186,7 +199,9 @@ class MobileProductRepository extends ProductRepository {
Future<Result<void, Exception>> _restoreAsync() async {
try {
if (!await InAppPurchase.instance.isAvailable()) {
return Result.error(Exception("InAppPurchase is not available"));
Exception error = Exception("InAppPurchase is not available");
Log.exception(error);
return Result.error(error);
}

InAppPurchase.instance.restorePurchases();
Expand All @@ -201,7 +216,9 @@ class MobileProductRepository extends ProductRepository {
if (purchaseDetails is GooglePlayPurchaseDetails) {
// TODO
// This is not required for Google. But be fair and add this later :-)
return Result.error(Exception("Unsupported purchase details!"));
Exception error = Exception("Unsupported purchase details!");
Log.exception(error);
return Result.error(error);
} else if (purchaseDetails is AppStorePurchaseDetails) {
String transactionId = purchaseDetails.skPaymentTransaction
.originalTransaction?.transactionIdentifier ??
Expand All @@ -210,9 +227,12 @@ class MobileProductRepository extends ProductRepository {
.registerByAppleTransactionId(transactionId)
.first;
} else {
return Result.error(Exception("Unsupported purchase details!"));
Exception error = Exception("Unsupported purchase details!");
Log.exception(error);
return Result.error(error);
}
} on Exception catch (e) {
Log.exception(e);
return Result.error(e);
}
}
Expand Down Expand Up @@ -240,8 +260,9 @@ class MobileProductRepository extends ProductRepository {
await InAppPurchase.instance.completePurchase(purchaseDetails);
}

return Result.error(
Exception("Purchase error: ${purchaseDetails.error}"));
Exception error = Exception("Purchase error: ${purchaseDetails.error}");
Log.exception(error);
return Result.error(error);
case PurchaseStatus.restored:
if (purchaseDetails.pendingCompletePurchase && Platform.isIOS) {
await InAppPurchase.instance.completePurchase(purchaseDetails);
Expand All @@ -253,9 +274,12 @@ class MobileProductRepository extends ProductRepository {
await InAppPurchase.instance.completePurchase(purchaseDetails);
}

return Result.error(Exception("Purchase error: Canceled"));
Exception error = Exception("Purchase error: Canceled");
Log.exception(error);
return Result.error(error);
}
} on Exception catch (e) {
Log.exception(e);
return Result.error(e);
}
}
Expand Down
22 changes: 17 additions & 5 deletions sponsor/lib/repository/purchase_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:convert';

import 'package:core/device/model/device_model.dart';
import 'package:core/device/repository/device_repository.dart';
import 'package:core/log/log.dart';
import 'package:multiple_result/multiple_result.dart';
import 'package:http/http.dart' as http;
import 'package:sponsor/model/apple_purchase_model.dart';
Expand Down Expand Up @@ -49,14 +50,18 @@ class TanksteWebPurchaseRepository extends PurchaseRepository {
Result<ConfigModel, Exception> configResult =
await _configRepository.get().first;
if (configResult.isError()) {
return Result.error(configResult.tryGetError()!);
Exception error = configResult.tryGetError()!;
Log.exception(error);
return Result.error(error);
}
ConfigModel config = configResult.tryGetSuccess()!;

Result<DeviceModel, Exception> deviceResult =
await _deviceRepository.get().first;
if (deviceResult.isError()) {
return Result.error(deviceResult.tryGetError()!);
Exception error = deviceResult.tryGetError()!;
Log.exception(error);
return Result.error(error);
}
DeviceModel device = deviceResult.tryGetSuccess()!;

Expand All @@ -68,7 +73,9 @@ class TanksteWebPurchaseRepository extends PurchaseRepository {
} else if (purchase is ApplePurchaseModel) {
body = jsonEncode(ApplePurchaseDto.fromModel(purchase, device.id));
} else {
return Result.error(Exception("Unsupported purchase type!"));
Exception error = Exception("Unsupported purchase type!");
Log.exception(error);
return Result.error(error);
}

http.Response response = await http.post(url, body: body, headers: {
Expand All @@ -86,14 +93,19 @@ class TanksteWebPurchaseRepository extends PurchaseRepository {
ApplePurchaseDto.fromJson(jsonResponse);
purchaseResult = applePurchaseDto.toModel();
} else {
return Result.error(Exception("Unsupported purchase type!"));
Exception error = Exception("Unsupported purchase type!");
Log.exception(error);
return Result.error(error);
}

return Result.success(purchaseResult);
} else {
return Result.error(Exception("API Error!\n\n${response.body}"));
Exception error = Exception("API Error!\n\n${response.body}");
Log.exception(error);
return Result.error(error);
}
} on Exception catch (e) {
Log.exception(e);
return Result.error(e);
}
}
Expand Down
Loading

0 comments on commit 92fa239

Please sign in to comment.