Skip to content

Commit ebddd09

Browse files
committed
refactor: interface error exception
1 parent 14f5d4c commit ebddd09

File tree

11 files changed

+141
-0
lines changed

11 files changed

+141
-0
lines changed

app/Exceptions/ErrorException.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ public function render()
9393
}
9494

9595
// Other
96+
$previousUrl = url()->previous();
97+
$currentUrl = url()->current();
98+
99+
$previousPath = parse_url($previousUrl, PHP_URL_PATH);
100+
$currentPath = parse_url($currentUrl, PHP_URL_PATH);
101+
102+
if ($previousPath == $currentPath) {
103+
return redirect()->route('fresns.home')->with([
104+
'code' => $this->getCode(),
105+
'failure' => $this->getMessage(),
106+
]);
107+
}
108+
96109
return back()->with([
97110
'code' => $this->getCode(),
98111
'failure' => $this->getMessage(),

app/Interfaces/CommentInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public static function list(?array $query = []): array
4040
$resultContent = $response->getContent();
4141
$result = json_decode($resultContent, true);
4242
}
43+
44+
if ($result['code'] != 0) {
45+
throw new ErrorException($result['message'], $result['code']);
46+
}
4347
} catch (\Exception $e) {
4448
$code = (int) $e->getCode();
4549

@@ -73,6 +77,10 @@ public static function nearby(?array $query = []): array
7377
$resultContent = $response->getContent();
7478
$result = json_decode($resultContent, true);
7579
}
80+
81+
if ($result['code'] != 0) {
82+
throw new ErrorException($result['message'], $result['code']);
83+
}
7684
} catch (\Exception $e) {
7785
$code = (int) $e->getCode();
7886

@@ -119,6 +127,10 @@ public static function detail(string $cid, ?array $query = []): array
119127
$result = json_decode($resultContent, true);
120128
}
121129

130+
if ($result['code'] != 0) {
131+
throw new ErrorException($result['message'], $result['code']);
132+
}
133+
122134
$results = [
123135
'comment' => $result,
124136
'comments' => CommentInterface::list($query),
@@ -160,6 +172,10 @@ public static function interaction(string $cid, string $type, ?array $query = []
160172
'comment' => json_decode($detailResponse->getContent(), true),
161173
'users' => json_decode($usersResponse->getContent(), true),
162174
];
175+
176+
if ($results['comment']['code'] != 0) {
177+
throw new ErrorException($results['comment']['message'], $results['comment']['code']);
178+
}
163179
} catch (\Exception $e) {
164180
$code = (int) $e->getCode();
165181

app/Interfaces/GeotagInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public static function list(?array $query = []): array
3838

3939
$resultContent = $response->getContent();
4040
$result = json_decode($resultContent, true);
41+
42+
if ($result['code'] != 0) {
43+
throw new ErrorException($result['message'], $result['code']);
44+
}
4145
} catch (\Exception $e) {
4246
$code = (int) $e->getCode();
4347

@@ -100,6 +104,10 @@ public static function detail(string $gtid, ?string $type = null, ?array $query
100104
$resultContent = $response->getContent();
101105
$result = json_decode($resultContent, true);
102106

107+
if ($result['code'] != 0) {
108+
throw new ErrorException($result['message'], $result['code']);
109+
}
110+
103111
switch ($type) {
104112
case 'posts':
105113
$postRequest = Request::create('/api/fresns/v1/post/list', 'GET', $query);
@@ -166,6 +174,10 @@ public static function interaction(string $gtid, string $type, ?array $query = [
166174
'geotag' => json_decode($detailResponse->getContent(), true),
167175
'users' => json_decode($usersResponse->getContent(), true),
168176
];
177+
178+
if ($results['geotag']['code'] != 0) {
179+
throw new ErrorException($results['geotag']['message'], $results['geotag']['code']);
180+
}
169181
} catch (\Exception $e) {
170182
$code = (int) $e->getCode();
171183

app/Interfaces/GroupInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public static function tree(): array
2929

3030
$resultContent = $response->getContent();
3131
$result = json_decode($resultContent, true);
32+
33+
if ($result['code'] != 0) {
34+
throw new ErrorException($result['message'], $result['code']);
35+
}
3236
} catch (\Exception $e) {
3337
$code = (int) $e->getCode();
3438

@@ -54,6 +58,10 @@ public static function list(?array $query = []): array
5458

5559
$resultContent = $response->getContent();
5660
$result = json_decode($resultContent, true);
61+
62+
if ($result['code'] != 0) {
63+
throw new ErrorException($result['message'], $result['code']);
64+
}
5765
} catch (\Exception $e) {
5866
$code = (int) $e->getCode();
5967

@@ -106,6 +114,10 @@ public static function detail(string $gid, ?string $type = null, ?array $query =
106114
$resultContent = $response->getContent();
107115
$result = json_decode($resultContent, true);
108116

117+
if ($result['code'] != 0) {
118+
throw new ErrorException($result['message'], $result['code']);
119+
}
120+
109121
switch ($type) {
110122
case 'posts':
111123
$results = [
@@ -158,6 +170,10 @@ public static function interaction(string $gid, string $type, ?array $query = []
158170
'group' => json_decode($detailResponse->getContent(), true),
159171
'users' => json_decode($usersResponse->getContent(), true),
160172
];
173+
174+
if ($results['group']['code'] != 0) {
175+
throw new ErrorException($results['group']['message'], $results['group']['code']);
176+
}
161177
} catch (\Exception $e) {
162178
$code = (int) $e->getCode();
163179

app/Interfaces/HashtagInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public static function list(?array $query = []): array
3838

3939
$resultContent = $response->getContent();
4040
$result = json_decode($resultContent, true);
41+
42+
if ($result['code'] != 0) {
43+
throw new ErrorException($result['message'], $result['code']);
44+
}
4145
} catch (\Exception $e) {
4246
$code = (int) $e->getCode();
4347

@@ -100,6 +104,10 @@ public static function detail(string $htid, ?string $type = null, ?array $query
100104
$resultContent = $response->getContent();
101105
$result = json_decode($resultContent, true);
102106

107+
if ($result['code'] != 0) {
108+
throw new ErrorException($result['message'], $result['code']);
109+
}
110+
103111
switch ($type) {
104112
case 'posts':
105113
$postRequest = Request::create('/api/fresns/v1/post/list', 'GET', $query);
@@ -166,6 +174,10 @@ public static function interaction(string $htid, string $type, ?array $query = [
166174
'hashtag' => json_decode($detailResponse->getContent(), true),
167175
'users' => json_decode($usersResponse->getContent(), true),
168176
];
177+
178+
if ($results['hashtag']['code'] != 0) {
179+
throw new ErrorException($results['hashtag']['message'], $results['hashtag']['code']);
180+
}
169181
} catch (\Exception $e) {
170182
$code = (int) $e->getCode();
171183

app/Interfaces/MeInterface.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public static function extcreditsRecords(?array $query = []): array
3434

3535
$resultContent = $response->getContent();
3636
$result = json_decode($resultContent, true);
37+
38+
if ($result['code'] != 0) {
39+
throw new ErrorException($result['message'], $result['code']);
40+
}
3741
} catch (\Exception $e) {
3842
$code = (int) $e->getCode();
3943

@@ -59,6 +63,10 @@ public static function walletRecords(?array $query = []): array
5963

6064
$resultContent = $response->getContent();
6165
$result = json_decode($resultContent, true);
66+
67+
if ($result['code'] != 0) {
68+
throw new ErrorException($result['message'], $result['code']);
69+
}
6270
} catch (\Exception $e) {
6371
$code = (int) $e->getCode();
6472

@@ -84,6 +92,10 @@ public static function drafts(string $type, ?array $query = []): array
8492

8593
$resultContent = $response->getContent();
8694
$result = json_decode($resultContent, true);
95+
96+
if ($result['code'] != 0) {
97+
throw new ErrorException($result['message'], $result['code']);
98+
}
8799
} catch (\Exception $e) {
88100
$code = (int) $e->getCode();
89101

@@ -107,6 +119,10 @@ public static function getDraftDetail(string $type, string $did): array
107119

108120
$resultContent = $response->getContent();
109121
$result = json_decode($resultContent, true);
122+
123+
if ($result['code'] != 0) {
124+
throw new ErrorException($result['message'], $result['code']);
125+
}
110126
} catch (\Exception $e) {
111127
$code = (int) $e->getCode();
112128

@@ -130,6 +146,10 @@ public static function archives(string $type): array
130146

131147
$resultContent = $response->getContent();
132148
$result = json_decode($resultContent, true);
149+
150+
if ($result['code'] != 0) {
151+
throw new ErrorException($result['message'], $result['code']);
152+
}
133153
} catch (\Exception $e) {
134154
$code = (int) $e->getCode();
135155

app/Interfaces/MessageInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public static function list(): array
5656
'conversations' => json_decode($resultContent, true),
5757
'pinConversations' => json_decode($pinResultContent, true),
5858
];
59+
60+
if ($results['conversations']['code'] != 0) {
61+
throw new ErrorException($results['conversations']['message'], $results['conversations']['code']);
62+
}
5963
} catch (\Exception $e) {
6064
$code = (int) $e->getCode();
6165

@@ -90,6 +94,10 @@ public static function conversation(int|string $uidOrUsername, ?array $query = [
9094
$resultContent = $response->getContent();
9195
$result = json_decode($resultContent, true);
9296

97+
if ($result['code'] != 0) {
98+
throw new ErrorException($result['message'], $result['code']);
99+
}
100+
93101
$messagesRequest = Request::create("/api/fresns/v1/conversation/{$uidOrUsername}/messages", 'GET', $query);
94102
$messagesResponse = $apiController->messages($uidOrUsername, $messagesRequest);
95103

@@ -128,6 +136,10 @@ public static function notifications(?array $query = []): array
128136

129137
$resultContent = $response->getContent();
130138
$result = json_decode($resultContent, true);
139+
140+
if ($result['code'] != 0) {
141+
throw new ErrorException($result['message'], $result['code']);
142+
}
131143
} catch (\Exception $e) {
132144
$code = (int) $e->getCode();
133145

app/Interfaces/PostInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public static function list(?array $query = []): array
4040
$resultContent = $response->getContent();
4141
$result = json_decode($resultContent, true);
4242
}
43+
44+
if ($result['code'] != 0) {
45+
throw new ErrorException($result['message'], $result['code']);
46+
}
4347
} catch (\Exception $e) {
4448
$code = (int) $e->getCode();
4549

@@ -73,6 +77,10 @@ public static function nearby(?array $query = []): array
7377
$resultContent = $response->getContent();
7478
$result = json_decode($resultContent, true);
7579
}
80+
81+
if ($result['code'] != 0) {
82+
throw new ErrorException($result['message'], $result['code']);
83+
}
7684
} catch (\Exception $e) {
7785
$code = (int) $e->getCode();
7886

@@ -126,6 +134,10 @@ public static function detail(string $pid, ?array $query = []): array
126134
$result = json_decode($resultContent, true);
127135
}
128136

137+
if ($result['code'] != 0) {
138+
throw new ErrorException($result['message'], $result['code']);
139+
}
140+
129141
$results = [
130142
'post' => $result,
131143
'comments' => CommentInterface::list($query),
@@ -171,6 +183,10 @@ public static function interaction(string $pid, string $type, ?array $query = []
171183
'post' => json_decode($detailResponse->getContent(), true),
172184
'users' => json_decode($usersResponse->getContent(), true),
173185
];
186+
187+
if ($results['post']['code'] != 0) {
188+
throw new ErrorException($results['post']['message'], $results['post']['code']);
189+
}
174190
} catch (\Exception $e) {
175191
$code = (int) $e->getCode();
176192

app/Interfaces/SearchInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public static function search(string $type, ?array $query = []): array
4040
$resultContent = $response->getContent();
4141
$result = json_decode($resultContent, true);
4242
}
43+
44+
if ($result['code'] != 0) {
45+
throw new ErrorException($result['message'], $result['code']);
46+
}
4347
} catch (\Exception $e) {
4448
$code = (int) $e->getCode();
4549

app/Interfaces/TimelineInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public static function posts(?array $query = []): array
4141
$resultContent = $response->getContent();
4242
$result = json_decode($resultContent, true);
4343
}
44+
45+
if ($result['code'] != 0) {
46+
throw new ErrorException($result['message'], $result['code']);
47+
}
4448
} catch (\Exception $e) {
4549
$code = (int) $e->getCode();
4650

@@ -74,6 +78,10 @@ public static function comments(?array $query = []): array
7478
$resultContent = $response->getContent();
7579
$result = json_decode($resultContent, true);
7680
}
81+
82+
if ($result['code'] != 0) {
83+
throw new ErrorException($result['message'], $result['code']);
84+
}
7785
} catch (\Exception $e) {
7886
$code = (int) $e->getCode();
7987

0 commit comments

Comments
 (0)