Skip to content

Commit aad61aa

Browse files
committed
refactor: add try catch to server/api endpoints
1 parent 6b1bf21 commit aad61aa

File tree

2 files changed

+44
-33
lines changed

2 files changed

+44
-33
lines changed

server/api/login.post.js

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
export default defineEventHandler(async (event) => {
2-
const config = useRuntimeConfig()
3-
const body = await readBody(event)
2+
try {
3+
const config = useRuntimeConfig()
4+
const body = await readBody(event)
45

5-
const url = `${config.public.COMMUNITY_API_URL}/auth/`
6-
const loginOptions = {
7-
method: "POST",
8-
body: body,
9-
headers: {
10-
"Accept": "*/*",
11-
"Content-Type": "application/json"
6+
const url = `${config.public.COMMUNITY_API_URL}/auth/`
7+
const loginOptions = {
8+
method: "POST",
9+
body: body,
10+
headers: {
11+
"Accept": "*/*",
12+
"Content-Type": "application/json"
13+
}
1214
}
13-
}
1415

15-
const loginResponse = await $fetch.raw(url + 'login', loginOptions);
16+
const loginResponse = await $fetch.raw(url + 'login', loginOptions);
1617

17-
const authResponse = await $fetch.raw(url, {
18-
method: 'GET',
19-
headers: {
20-
"Accept": "*/*",
21-
"Cookie": loginResponse.headers.get('set-cookie')
22-
}
23-
})
24-
appendHeader(event, 'Set-Cookie', loginResponse.headers.get('set-cookie')) // assuming we only set one cookie
25-
return authResponse._data
18+
const authResponse = await $fetch.raw(url, {
19+
method: 'GET',
20+
headers: {
21+
"Accept": "*/*",
22+
"Cookie": loginResponse.headers.get('set-cookie')
23+
}
24+
})
25+
appendHeader(event, 'Set-Cookie', loginResponse.headers.get('set-cookie')) // assuming we only set one cookie
26+
return authResponse._data
27+
28+
} catch (error) {
29+
console.log(error);
30+
return false;
31+
}
2632
});

server/api/member/index.get.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
export default defineEventHandler(async (event) => {
2-
const query = getQuery(event)
3-
const config = useRuntimeConfig()
4-
const sessionCookie = getCookie(event, `${config.public.SESSION_COOKIE_NAME}`);
2+
try {
3+
const query = getQuery(event)
4+
const config = useRuntimeConfig()
5+
const sessionCookie = getCookie(event, `${config.public.SESSION_COOKIE_NAME}`);
56

6-
const url = `${config.public.COMMUNITY_API_URL}/member/${query.id}`
7-
const options = {
8-
method: "GET",
9-
headers: {
10-
"Accept": "*/*",
11-
"Content-Type": "application/json",
12-
"Cookie": `${config.public.SESSION_COOKIE_NAME}=` + sessionCookie,
7+
const url = `${config.public.COMMUNITY_API_URL}/member/${query.id}`
8+
const options = {
9+
method: "GET",
10+
headers: {
11+
"Accept": "*/*",
12+
"Content-Type": "application/json",
13+
"Cookie": `${config.public.SESSION_COOKIE_NAME}=` + sessionCookie,
14+
}
1315
}
14-
}
1516

16-
const response = await $fetch.raw(url, options);
17-
return response._data
17+
const response = await $fetch.raw(url, options);
18+
return response._data
19+
} catch (error) {
20+
console.log(error);
21+
return false;
22+
}
1823
});

0 commit comments

Comments
 (0)