Skip to content

Commit

Permalink
fix: avoid crash if ingredients services called without ingredients_lc (
Browse files Browse the repository at this point in the history
#11055)

fixes #11047

In the future, it would be a good thing to enable the services to add
error messages that could be returned to the caller, when there is
missing data for instance.

---------

Co-authored-by: Open Food Facts Bot <contact@openfoodfacts.org>
Co-authored-by: Alex Garel <alex@garel.org>
  • Loading branch information
3 people authored Dec 5, 2024
1 parent 2c9a7f4 commit 1db3e94
Show file tree
Hide file tree
Showing 224 changed files with 605 additions and 158 deletions.
21 changes: 18 additions & 3 deletions lib/ProductOpener/APIProductServices.pm
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ An array list of services to perform.
Currently implemented services:
- echo : does nothing, mostly for testing
- parse_ingredients_text : parse the ingredients text list and return an ingredients object
- extend_ingredients : extend the ingredients object with additional information
- estimate_ingredients_percent : compute percent_min, percent_max, percent_estimate for each ingredient in the ingredients object
- analyze_ingredients : analyze the ingredients object and return a summary object
=head4 product
Expand Down Expand Up @@ -90,17 +93,23 @@ use ProductOpener::API qw/add_error customize_response_for_product/;

use Encode;

=head2 echo_service ($product_ref)
=head2 echo_service ($product_ref, $updated_product_fields_ref, $errors_ref)
Echo service that returns the input product unchanged.
=cut

sub echo_service ($product_ref, $updated_product_fields_ref) {
sub echo_service ($product_ref, $updated_product_fields_ref, $errors_ref) {

return;
}

=head2 Service function handlers
They will be called with the input product object, a reference to the updated fields hash, and a reference to the errors array.
=cut

my %service_functions = (
echo => \&echo_service,
parse_ingredients_text => \&ProductOpener::Ingredients::parse_ingredients_text_service,
Expand Down Expand Up @@ -191,6 +200,12 @@ sub product_services_api ($request_ref) {
if (not $error) {

my $product_ref = $request_body_ref->{product};
# Normalization of some fields like lc / lang
normalize_product_data($product_ref);

# TODO: check that the product object is valid
# any input product data can be sent, and most of the services expect a specific structure
# they may crash and return a 500 error if the input data is not as expected (e.g. hash instead of array)

# We will track of fields updated by the services so that we can return only those fields
# if the fields parameter value is "updated"
Expand All @@ -199,7 +214,7 @@ sub product_services_api ($request_ref) {
foreach my $service (@{$request_body_ref->{services}}) {
my $service_function = $service_functions{$service};
if (defined $service_function) {
&$service_function($product_ref, $request_ref->{updated_product_fields});
&$service_function($product_ref, $request_ref->{updated_product_fields}, $response_ref->{errors});
}
else {
add_error(
Expand Down
2 changes: 1 addition & 1 deletion lib/ProductOpener/FoodProducts.pm
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ sub specific_processes_for_food_product ($product_ref) {
# Ingredients analysis

# Select best language to parse ingredients
$product_ref->{ingredients_lc} = select_ingredients_lc($product_ref);
select_ingredients_lc($product_ref);
clean_ingredients_text($product_ref);
extract_ingredients_from_text($product_ref);
extract_additives_from_text($product_ref);
Expand Down
2 changes: 1 addition & 1 deletion lib/ProductOpener/Images.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ sub process_image_crop ($user_id, $product_id, $id, $imgid, $angle, $normalize,
}

print STDERR
"image_crop.pl - imgid: $imgid - crop_size: $crop_size - x1: $x1, y1: $y1, x2: $x2, y2: $y2, w: $w, h: $h\n";
"image_crop.pl - source_path: $source_path - product_id: $product_id - imgid: $imgid - crop_size: $crop_size - x1: $x1, y1: $y1, x2: $x2, y2: $y2, w: $w, h: $h\n";
$log->trace("calculating geometry",
{crop_size => $crop_size, x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, w => $w, h => $h})
if $log->is_trace();
Expand Down
Loading

0 comments on commit 1db3e94

Please sign in to comment.