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

Handle API Exception #54

Merged
merged 2 commits into from
Jan 8, 2021
Merged
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
33 changes: 17 additions & 16 deletions src/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function __construct( Plugin $loader ) {

/**
* Get products from simple shop via API
* TODO: Show message if loading pf products failed
*/
public function wp_ajax_load_simple_shop_products() {
$this->update_simpleshop_products_cache();
Expand All @@ -52,7 +53,6 @@ public function wp_ajax_load_simple_shop_products() {
* Return products. If you need force refresh products from API, call `update_simpleshop_products_cache()` before
*
* @return array
* @throws VyfakturujAPIException
*/
public function get_simpleshop_products() {
$products = $this->get_simpleshop_products_cache();
Expand Down Expand Up @@ -90,23 +90,24 @@ protected function get_simpleshop_products_cache() {

/**
* Update Products cache from Vyfakturuj API
*
* @throws VyfakturujAPIException
*/
public function update_simpleshop_products_cache() {
$products = $this->load_simpleshop_products();

$cacheKey = $this->loader->get_cache_user_key();
$cachedTime = time();

$cache = [
$cacheKey => array_merge(
$products,
[ self::PRODUCTS_CACHE_FIELD => $cachedTime ]
)
];

update_option( 'ssc_cache_products', $cache );
try {
$products = $this->load_simpleshop_products();
$cacheKey = $this->loader->get_cache_user_key();
$cachedTime = time();

$cache = [
$cacheKey => array_merge(
$products,
[ self::PRODUCTS_CACHE_FIELD => $cachedTime ]
)
];

return update_option( 'ssc_cache_products', $cache );
} catch ( VyfakturujAPIException $e ) {
return new \WP_Error( 'api-error', $e->getMessage() );
}
}


Expand Down