Skip to content

Commit

Permalink
Improve not found barcode exception handling
Browse files Browse the repository at this point in the history
Signed-off-by: Dmytro Turskyi <[email protected]>
  • Loading branch information
Turskyi committed Feb 13, 2024
1 parent 300ec26 commit 7ca7e4d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 35 deletions.
1 change: 1 addition & 0 deletions components/core/entities/lib/entities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export 'src/enums/language.dart';
export 'src/enums/product_info_type.dart';
export 'src/enums/vegan.dart';
export 'src/enums/vegetarian.dart';
export 'src/exception/not_found_exception.dart';
export 'src/logging_interceptor/logging_interceptor.dart';
export 'src/product/product_info.dart';
export 'src/product/product_photo.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class NotFoundException implements Exception {
const NotFoundException(this.message);

final String message;

@override
String toString() => message;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:async';
import 'dart:developer';

import 'package:entities/entities.dart';
import 'package:flutter/foundation.dart';
import 'package:interface_adapters/src/data_sources/local/local_data_source.dart';
import 'package:interface_adapters/src/data_sources/remote/remote_data_source.dart';
import 'package:interface_adapters/src/error_message_extractor.dart';
Expand All @@ -19,7 +19,8 @@ class ProductInfoGatewayImpl implements ProductInfoGateway {
.getProductInfoAsFuture(input)
.onError((Object? error, StackTrace stackTrace) {
if (error is FormatException && error.source is String) {
log('Error in $runtimeType: ${extractErrorMessage(error.source)}.'
debugPrint(
'Error in $runtimeType: ${extractErrorMessage(error.source)}.'
'\nStacktrace: $stackTrace');
}
if (_isBarcode(input.code)) {
Expand All @@ -28,10 +29,13 @@ class ProductInfoGatewayImpl implements ProductInfoGateway {
return ProductInfo(website: input.code);
} else if (_isAmazonAsin(input.code)) {
return const ProductInfo(brand: 'Amazon');
} else if (error is NotFoundException) {
throw error;
} else {
throw Exception(
'Product information not found for barcode: $input.\nError: $error',
);
}
throw Exception(
'Product information not found for barcode: $input.\nError: $error',
);
}).then((ProductInfo info) {
if (info.origin.isNotEmpty || info.country.isNotEmpty) {
return info;
Expand Down
55 changes: 29 additions & 26 deletions lib/data/data_sources/remote/remote_data_source_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ class RemoteDataSourceImpl implements RemoteDataSource {
return const ProductInfo(brand: 'Amazon');
}
}
throw Exception(
'Product information not found for barcode: $input.',
throw NotFoundException(
input.language.isEnglish
? 'Product information not found for barcode: ${input.code}.'
: 'Інформація про продукт не знайдена для штрих-коду: '
'${input.code}',
);
});

Expand Down Expand Up @@ -115,16 +118,16 @@ class RemoteDataSourceImpl implements RemoteDataSource {
ingredients: product.ingredientList
.mapIndexed(
(int rank, String ingredient) => Ingredient(
rank: rank,
text: ingredient,
vegan: product.isVegan
? IngredientSpecialPropertyStatus.POSITIVE
: IngredientSpecialPropertyStatus.IGNORE,
vegetarian: product.isVegetarian
? IngredientSpecialPropertyStatus.POSITIVE
: IngredientSpecialPropertyStatus.IGNORE,
),
)
rank: rank,
text: ingredient,
vegan: product.isVegan
? IngredientSpecialPropertyStatus.POSITIVE
: IngredientSpecialPropertyStatus.IGNORE,
vegetarian: product.isVegetarian
? IngredientSpecialPropertyStatus.POSITIVE
: IngredientSpecialPropertyStatus.IGNORE,
),
)
.toList(),
ingredientsText: ingredientsText,
categories: product.countryTags.join(','),
Expand Down Expand Up @@ -169,7 +172,7 @@ class RemoteDataSourceImpl implements RemoteDataSource {
if (status.status != 'status ok') {
throw Exception(
'image could not be uploaded: ${status.error} '
'${status.imageId.toString()}',
'${status.imageId.toString()}',
);
}
}
Expand All @@ -191,8 +194,8 @@ class RemoteDataSourceImpl implements RemoteDataSource {
? OpenFoodFactsLanguage.ENGLISH
: OpenFoodFactsLanguage.UKRAINIAN,
).then(
(OcrIngredientsResult response) =>
response.status != 0 ? '' : response.ingredientsTextFromImage ?? '',
(OcrIngredientsResult response) =>
response.status != 0 ? '' : response.ingredientsTextFromImage ?? '',
);

/// Extract the ingredients of an existing product of the OpenFoodFacts
Expand Down Expand Up @@ -255,16 +258,16 @@ class RemoteDataSourceImpl implements RemoteDataSource {
ingredients: product.ingredientList
.mapIndexed(
(int rank, String ingredient) => Ingredient(
rank: rank,
text: ingredient,
vegan: product.isVegan
? IngredientSpecialPropertyStatus.POSITIVE
: IngredientSpecialPropertyStatus.IGNORE,
vegetarian: product.isVegetarian
? IngredientSpecialPropertyStatus.POSITIVE
: IngredientSpecialPropertyStatus.IGNORE,
),
)
rank: rank,
text: ingredient,
vegan: product.isVegan
? IngredientSpecialPropertyStatus.POSITIVE
: IngredientSpecialPropertyStatus.IGNORE,
vegetarian: product.isVegetarian
? IngredientSpecialPropertyStatus.POSITIVE
: IngredientSpecialPropertyStatus.IGNORE,
),
)
.toList(),
ingredientsText: ingredientsText,
ingredientsTextInLanguages: <OpenFoodFactsLanguage, String>{
Expand Down Expand Up @@ -305,7 +308,7 @@ class RemoteDataSourceImpl implements RemoteDataSource {

if (productResult.status != ProductResultV3.statusSuccess) {
throw Exception(
'product not found, please insert data for 3613042717385',
'product not found, please insert data for ${photo.info.barcode}',
);
}
}
Expand Down
8 changes: 4 additions & 4 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ packages:
dependency: "direct main"
description:
name: bloc
sha256: "3820f15f502372d979121de1f6b97bfcf1630ebff8fe1d52fb2b0bfa49be5b49"
sha256: f53a110e3b48dcd78136c10daa5d51512443cea5e1348c9d80a320095fa2db9e
url: "https://pub.dev"
source: hosted
version: "8.1.2"
version: "8.1.3"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -353,10 +353,10 @@ packages:
dependency: "direct main"
description:
name: flutter_bloc
sha256: e74efb89ee6945bcbce74a5b3a5a3376b088e5f21f55c263fc38cbdc6237faae
sha256: "87325da1ac757fcc4813e6b34ed5dd61169973871fdf181d6c2109dd6935ece1"
url: "https://pub.dev"
source: hosted
version: "8.1.3"
version: "8.1.4"
flutter_email_sender:
dependency: transitive
description:
Expand Down

0 comments on commit 7ca7e4d

Please sign in to comment.