Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error logging #9

Merged
merged 4 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ Currently, the following attribute types are supported by this package:
- `pim_catalog_boolean`
- `pim_catalog_price_collection`
- `pim_catalog_simpleselect`
- `pim_catalog_multiselect`

## Commands

Expand Down
1 change: 1 addition & 0 deletions src/Actions/Product/SaveProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function save(ProductData $productData): void
$product->update = $product->isDirty(['checksum']);
}

$product->resetFailures();
VincentBean marked this conversation as resolved.
Show resolved Hide resolved
$product->save();
}

Expand Down
1 change: 1 addition & 0 deletions src/Actions/Product/UpdateProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function update(Product $product): void
$product->update = false;
$product->fail_count = 0;
$product->failed_at = null;
$product->resetFailures();
$product->save();
}

Expand Down
1 change: 1 addition & 0 deletions src/Actions/ProductModel/SaveProductModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function save(ProductModelData $productModelData): void
$productModel->update = $productModel->isDirty(['checksum']);
}

$productModel->resetFailures();
$productModel->save();
}

Expand Down
3 changes: 1 addition & 2 deletions src/Actions/ProductModel/UpdateProductModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public function update(ProductModel $productModel): void

$productModel->modified_at = now();
$productModel->update = false;
$productModel->fail_count = 0;
$productModel->failed_at = null;
$productModel->resetFailures();
$productModel->save();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Akeneo/Types/NumberType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class NumberType extends BaseType
public function format(AttributeData $attributeData, mixed $value): float|int
{
if (! is_numeric($value)) {
throw new InvalidValueException('The given value is not numeric');
throw new InvalidValueException($attributeData->code(), $value);
}

return $attributeData['decimals_allowed']
Expand Down
4 changes: 1 addition & 3 deletions src/Akeneo/Types/PriceCollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ public function format(AttributeData $attributeData, mixed $value): array
}

if (! isset($amount) || ! isset($currency)) {
throw new InvalidValueException(
'Value is not valid for the price collection type. Got value: '.var_export($value, true)
);
throw new InvalidValueException($attributeData->code(), $value);
}

return [
Expand Down
5 changes: 4 additions & 1 deletion src/Exceptions/InvalidValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@

class InvalidValueException extends Exception
{
//
public function __construct(string $attribute, mixed $value)
{
parent::__construct('The given value "'.var_export($value, true).'" for attribute "'.$attribute.'" is invalid.');
}
}
18 changes: 18 additions & 0 deletions src/Jobs/Product/RetrieveProductJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use JustBetter\AkeneoProducts\Contracts\Product\RetrievesProduct;
use JustBetter\AkeneoProducts\Models\Product;
use Spatie\Activitylog\ActivityLogger;
use Throwable;

class RetrieveProductJob implements ShouldBeUnique, ShouldQueue
{
Expand Down Expand Up @@ -37,4 +40,19 @@ public function tags(): array
$this->identifier,
];
}

public function failed(Throwable $throwable): void
{
/** @var ?Product $model */
$model = Product::query()->firstWhere('identifier', '=', $this->identifier);

$model?->failed();

activity()
->when($model, function (ActivityLogger $logger, Product $product): ActivityLogger {
return $logger->on($product);
})
->useLog('error')
->log('Failed to retrieve the product: '.$throwable->getMessage());
}
}
21 changes: 21 additions & 0 deletions src/Jobs/Product/SaveProductJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Illuminate\Queue\InteractsWithQueue;
use JustBetter\AkeneoProducts\Contracts\Product\SavesProduct;
use JustBetter\AkeneoProducts\Data\ProductData;
use JustBetter\AkeneoProducts\Models\Product;
use Spatie\Activitylog\ActivityLogger;
use Throwable;

class SaveProductJob implements ShouldBeUnique, ShouldQueue
{
Expand Down Expand Up @@ -38,4 +41,22 @@ public function tags(): array
$this->productData->identifier(),
];
}

public function failed(Throwable $throwable): void
{
/** @var ?Product $model */
$model = Product::query()->firstWhere('identifier', '=', $this->productData->identifier());

$model?->failed();

activity()
->when($model, function (ActivityLogger $logger, Product $product): ActivityLogger {
return $logger->on($product);
})
->useLog('error')
->withProperties([
'data' => $this->productData->toArray(),
])
->log('Failed to save the product data: '.$throwable->getMessage());
}
}
3 changes: 1 addition & 2 deletions src/Jobs/Product/UpdateProductJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ public function failed(Throwable $throwable): void
->on($this->product)
->useLog('error')
->withProperties([
'message' => $throwable->getMessage(),
'code' => $throwable->getCode(),
VincentBean marked this conversation as resolved.
Show resolved Hide resolved
])
->log('Failed to update product in Akeneo');
->log('Failed to update product in Akeneo: '.$throwable->getMessage());
}
}
18 changes: 18 additions & 0 deletions src/Jobs/ProductModel/RetrieveProductModelJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use JustBetter\AkeneoProducts\Contracts\ProductModel\RetrievesProductModel;
use JustBetter\AkeneoProducts\Models\ProductModel;
use Spatie\Activitylog\ActivityLogger;
use Throwable;

class RetrieveProductModelJob implements ShouldBeUnique, ShouldQueue
{
Expand Down Expand Up @@ -37,4 +40,19 @@ public function tags(): array
$this->code,
];
}

public function failed(Throwable $throwable): void
{
/** @var ?ProductModel $model */
$model = ProductModel::query()->firstWhere('code', '=', $this->code);

$model?->failed();

activity()
->when($model, function (ActivityLogger $logger, ProductModel $productModel): ActivityLogger {
return $logger->on($productModel);
})
->useLog('error')
->log('Failed to retrieve the productmodel: '.$throwable->getMessage());
}
}
18 changes: 18 additions & 0 deletions src/Jobs/ProductModel/SaveProductModelJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Illuminate\Queue\InteractsWithQueue;
use JustBetter\AkeneoProducts\Contracts\ProductModel\SavesProductModel;
use JustBetter\AkeneoProducts\Data\ProductModelData;
use JustBetter\AkeneoProducts\Models\ProductModel;
use Spatie\Activitylog\ActivityLogger;
use Throwable;

class SaveProductModelJob implements ShouldBeUnique, ShouldQueue
{
Expand Down Expand Up @@ -38,4 +41,19 @@ public function tags(): array
$this->productModelData->code(),
];
}

public function failed(Throwable $throwable): void
{
/** @var ?ProductModel $model */
$model = ProductModel::query()->firstWhere('code', '=', $this->productModelData->code());

$model?->failed();

activity()
->when($model, function (ActivityLogger $logger, ProductModel $productModel): ActivityLogger {
return $logger->on($productModel);
})
->useLog('error')
->log('Failed to save the productmodel: '.$throwable->getMessage());
}
}
18 changes: 18 additions & 0 deletions src/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use JustBetter\AkeneoProducts\Retrievers\Product\BaseProductRetriever;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;

/**
* @property int $id
Expand All @@ -26,6 +28,7 @@
*/
class Product extends Model
{
use LogsActivity;
use SoftDeletes;

protected $table = 'akeneo_products';
Expand Down Expand Up @@ -69,4 +72,19 @@ public function failed(): void

$this->save();
}

public function resetFailures(): void
{
$this->synchronize = true;
$this->fail_count = 0;
$this->failed_at = null;
}

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnlyDirty()
->dontSubmitEmptyLogs()
->logOnly(['data']);
}
}
18 changes: 18 additions & 0 deletions src/Models/ProductModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use JustBetter\AkeneoProducts\Retrievers\ProductModel\BaseProductModelRetriever;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;

/**
* @property int $id
Expand All @@ -26,6 +28,7 @@
*/
class ProductModel extends Model
{
use LogsActivity;
use SoftDeletes;

protected $table = 'akeneo_product_models';
Expand Down Expand Up @@ -69,4 +72,19 @@ public function failed(): void

$this->save();
}

public function resetFailures(): void
{
$this->synchronize = true;
$this->fail_count = 0;
$this->failed_at = null;
}

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnlyDirty()
->dontSubmitEmptyLogs()
->logOnly(['data']);
}
}
28 changes: 28 additions & 0 deletions tests/Jobs/Product/RetrieveProductJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace JustBetter\AkeneoProducts\Tests\Jobs\Product;

use Exception;
use JustBetter\AkeneoProducts\Contracts\Product\RetrievesProduct;
use JustBetter\AkeneoProducts\Jobs\Product\RetrieveProductJob;
use JustBetter\AkeneoProducts\Models\Product;
use JustBetter\AkeneoProducts\Tests\TestCase;
use Mockery\MockInterface;
use PHPUnit\Framework\Attributes\Test;
Expand Down Expand Up @@ -32,4 +34,30 @@ public function it_has_correct_tags_and_unique_id(): void
$this->assertEquals(['identifier'], $job->tags());
$this->assertEquals('identifier', $job->uniqueId());
}

#[Test]
public function it_can_fail(): void
{
/** @var Product $product */
$product = Product::query()->create([
'identifier' => 'identifier',
'data' => [],
]);

$job = new RetrieveProductJob('identifier');
$job->failed(new Exception);

$product->refresh();

$this->assertNotNull($product->failed_at);
}

#[Test]
public function it_can_fail_without_product_model(): void
{
$job = new RetrieveProductJob('identifier');
$job->failed(new Exception);

$this->assertTrue(true, 'No exception thrown');
}
}
54 changes: 54 additions & 0 deletions tests/Jobs/Product/SaveProductJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace JustBetter\AkeneoProducts\Tests\Jobs\Product;

use Exception;
use JustBetter\AkeneoProducts\Contracts\Product\SavesProduct;
use JustBetter\AkeneoProducts\Data\ProductData;
use JustBetter\AkeneoProducts\Jobs\Product\SaveProductJob;
use JustBetter\AkeneoProducts\Models\Product;
use JustBetter\AkeneoProducts\Tests\TestCase;
use Mockery\MockInterface;
use PHPUnit\Framework\Attributes\Test;
Expand Down Expand Up @@ -58,4 +60,56 @@ public function it_has_correct_tags_and_unique_id(): void
$this->assertEquals(['identifier'], $job->tags());
$this->assertEquals('identifier', $job->uniqueId());
}

#[Test]
public function it_can_fail(): void
{
/** @var Product $product */
$product = Product::query()->create([
'identifier' => 'identifier',
'data' => [],
]);

$productData = ProductData::of([
'identifier' => 'identifier',
'values' => [
'name' => [
[
'locale' => 'nl_NL',
'scope' => 'ecommerce',
'data' => 'Ziggy',
],
],
],
]);

$job = new SaveProductJob($productData);
$job->failed(new Exception);

$product->refresh();

$this->assertNotNull($product->failed_at);
}

#[Test]
public function it_can_fail_without_product_model(): void
{
$productData = ProductData::of([
'identifier' => 'identifier',
'values' => [
'name' => [
[
'locale' => 'nl_NL',
'scope' => 'ecommerce',
'data' => 'Ziggy',
],
],
],
]);

$job = new SaveProductJob($productData);
$job->failed(new Exception);

$this->assertTrue(true, 'No exception thrown');
}
}
Loading