Skip to content

Commit 149ed28

Browse files
committed
Added error handling
1 parent cde5330 commit 149ed28

File tree

2 files changed

+80
-28
lines changed

2 files changed

+80
-28
lines changed

src/Traits/WooCommerceTrait.php

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ trait WooCommerceTrait
1515
*/
1616
public function all($endpoint = '', $options = [])
1717
{
18-
self::__construct();
18+
try {
19+
self::__construct();
1920

20-
return $this->client->get($endpoint, $options);
21+
return $this->client->get($endpoint, $options);
22+
23+
} catch ( \Exception $e) {
24+
throw new \Exception($e->getMessages(), 1);
25+
26+
}
2127
}
2228

2329
/**
@@ -31,9 +37,15 @@ public function all($endpoint = '', $options = [])
3137
*/
3238
public function find($endpoint = '', $options = [])
3339
{
34-
self::__construct();
40+
try {
41+
self::__construct();
42+
43+
return $this->client->get($endpoint, $options);
3544

36-
return $this->client->get($endpoint, $options);
45+
} catch ( \Exception $e) {
46+
throw new \Exception($e->getMessages(), 1);
47+
48+
}
3749
}
3850

3951
/**
@@ -47,9 +59,15 @@ public function find($endpoint = '', $options = [])
4759
*/
4860
public function create($endpoint, $data)
4961
{
50-
self::__construct();
62+
try {
63+
self::__construct();
5164

52-
return $this->client->post($endpoint, $data);
65+
return $this->client->post($endpoint, $data);
66+
67+
} catch ( \Exception $e) {
68+
throw new \Exception($e->getMessages(), 1);
69+
70+
}
5371
}
5472

5573
/**
@@ -63,9 +81,15 @@ public function create($endpoint, $data)
6381
*/
6482
public function update($endpoint, $data)
6583
{
66-
self::__construct();
84+
try {
85+
self::__construct();
86+
87+
return $this->client->put($endpoint, $data);
6788

68-
return $this->client->put($endpoint, $data);
89+
} catch ( \Exception $e) {
90+
throw new \Exception($e->getMessages(), 1);
91+
92+
}
6993
}
7094

7195
/**
@@ -79,9 +103,15 @@ public function update($endpoint, $data)
79103
*/
80104
public function delete($endpoint, $options = [])
81105
{
82-
self::__construct();
106+
try {
107+
self::__construct();
83108

84-
return $this->client->delete($endpoint, $options);
109+
return $this->client->delete($endpoint, $options);
110+
111+
} catch ( \Exception $e) {
112+
throw new \Exception($e->getMessages(), 1);
113+
114+
}
85115
}
86116

87117
/**
@@ -91,7 +121,15 @@ public function delete($endpoint, $options = [])
91121
*/
92122
public function getRequest()
93123
{
94-
return $this->client->http->getRequest();
124+
try {
125+
126+
return $this->client->http->getRequest();
127+
128+
} catch ( \Exception $e) {
129+
throw new \Exception($e->getMessages(), 1);
130+
131+
}
132+
95133
}
96134

97135
/**
@@ -101,7 +139,14 @@ public function getRequest()
101139
*/
102140
public function getResponse()
103141
{
104-
return $this->client->http->getResponse();
142+
try {
143+
144+
return $this->client->http->getResponse();
145+
146+
} catch ( \Exception $e) {
147+
throw new \Exception($e->getMessages(), 1);
148+
149+
}
105150
}
106151

107152
/**

src/WooCommerceApi.php

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,29 @@ class WooCommerceApi
2626
*/
2727
public function __construct()
2828
{
29-
$this->headers = [
30-
'header_total' => config('woocommerce.header_total') ?? 'X-WP-Total',
31-
'header_total_pages' => config('woocommerce.header_total_pages') ?? 'X-WP-TotalPages',
32-
];
29+
try {
30+
31+
$this->headers = [
32+
'header_total' => config('woocommerce.header_total') ?? 'X-WP-Total',
33+
'header_total_pages' => config('woocommerce.header_total_pages') ?? 'X-WP-TotalPages',
34+
];
35+
36+
$this->client = new Client(
37+
config('woocommerce.store_url'),
38+
config('woocommerce.consumer_key'),
39+
config('woocommerce.consumer_secret'),
40+
[
41+
'version' => 'wc/'.config('woocommerce.api_version'),
42+
'wp_api' => config('woocommerce.wp_api_integration'),
43+
'verify_ssl' => config('woocommerce.verify_ssl'),
44+
'query_string_auth' => config('woocommerce.query_string_auth'),
45+
'timeout' => config('woocommerce.timeout'),
46+
]
47+
);
3348

34-
$this->client = new Client(
35-
config('woocommerce.store_url'),
36-
config('woocommerce.consumer_key'),
37-
config('woocommerce.consumer_secret'),
38-
[
39-
'version' => 'wc/'.config('woocommerce.api_version'),
40-
'wp_api' => config('woocommerce.wp_api_integration'),
41-
'verify_ssl' => config('woocommerce.verify_ssl'),
42-
'query_string_auth' => config('woocommerce.query_string_auth'),
43-
'timeout' => config('woocommerce.timeout'),
44-
]
45-
);
49+
} catch ( \Exception $e) {
50+
throw new \Exception($e->getMessages(), 1);
51+
52+
}
4653
}
4754
}

0 commit comments

Comments
 (0)