Skip to content

Commit 8f0157a

Browse files
fix: error to netinfo request
1 parent 0daf34a commit 8f0157a

File tree

1 file changed

+53
-13
lines changed

1 file changed

+53
-13
lines changed

src/templates/netinfo/Netinfo.vue

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,37 @@
22
<div class="flex flex-col gap-3 w-full">
33
<div class="flex justify-end">
44
<Button
5+
outlined
56
label=""
67
size="small"
7-
:icon="icon"
88
severity="secondary"
9-
outlined
9+
:disabled="!data"
10+
:icon="icon"
1011
@click="copy"
1112
/>
1213
</div>
1314

1415
<div>
15-
<ul v-if="data" v-show="data" class="block rounded border surface-border">
16+
<Message
17+
v-if="errormsg"
18+
severity="error"
19+
>
20+
<div class="flex flex-row items-center">
21+
<span>{{ errormsg }}</span>
22+
<Button
23+
link
24+
@click="retryBuildPage"
25+
class="text-sm"
26+
>
27+
Retry
28+
</Button>
29+
</div>
30+
</Message>
31+
<ul
32+
v-else-if="data"
33+
v-show="data"
34+
class="block rounded border surface-border"
35+
>
1636
<li class="flex flex-row gap-2 border-b surface-border p-2">
1737
<p>
1838
<strong class="text-nowrap">UUID:</strong> {{ data.uuid }}
@@ -74,25 +94,43 @@
7494
import { onMounted, ref } from 'vue'
7595
import Button from 'primevue/button'
7696
import Skeleton from 'primevue/skeleton'
97+
import Message from 'primevue/message'
7798
7899
const service = 'https://netinfo.azion.com/json'
79100
const data = ref(null)
80-
const error = ref(null)
101+
const errormsg = ref(null)
81102
const icon = ref('pi pi-copy')
82103
83104
onMounted(() => {
105+
buildpage()
106+
})
107+
108+
const buildpage = () => {
84109
getData(service)
85110
.then(json => {
86-
if(json.msg) {
87-
error.value = json.msg
111+
if(json.message) {
112+
setMessageError(json.message === 'Failed to fetch' ? 'Network error.' : json)
88113
return
89114
}
90115
91-
console.log(json)
92116
data.value = parseData(json)
93-
console.log(data.value)
117+
}).catch(error => {
118+
setMessageError(error.message === 'Failed to fetch' ? 'Network error.' : error)
94119
})
95-
})
120+
}
121+
122+
const retryBuildPage = () => {
123+
removeMessageError()
124+
buildpage()
125+
}
126+
127+
const setMessageError = (msg) => {
128+
errormsg.value = msg
129+
}
130+
131+
const removeMessageError = () => {
132+
errormsg.value = null
133+
}
96134
97135
const copy = () => {
98136
navigator.clipboard.writeText(JSON.stringify(data.value))
@@ -109,22 +147,24 @@
109147
if (!response.ok) {
110148
const errorData = {
111149
status: response.status,
112-
msg: '[!] Fetch ERRROR to API communication'
150+
message: '[!] Fetch ERRROR to API communication.'
113151
}
114152
115153
console.error(errorData)
116154
throw new Error(errorData)
117155
}
118156
157+
return response
158+
})
159+
.then(response => {
119160
return response.json()
120161
})
121162
.then(json => {
122163
return json
123164
})
124165
.catch(error => {
125-
const errorData = { msg: error.message || error }
126-
console.error(errorData)
127-
return { msg: errorData }
166+
console.error(error)
167+
return error
128168
})
129169
}
130170

0 commit comments

Comments
 (0)