feat: use throw error

This commit is contained in:
EETagent 2022-12-03 18:17:40 +01:00
parent 1c7b6eac55
commit e64be7cce2
2 changed files with 49 additions and 33 deletions

View file

@ -3,48 +3,59 @@ import type { CandidateData, CandidateLogin } from '$lib/stores/candidate';
import type { SubmissionProgress } from '$lib/stores/portfolio';
import { API_URL, errorHandler, type Fetch } from '.';
// SSR Compatible
export const apiLogout = async (fetchSsr?: Fetch) => {
try {
fetchSsr ? await fetchSsr(API_URL + '/candidate/logout', { method: 'POST', credentials: 'include' }) : await axios.post(API_URL + '/candidate/logout', { withCredentials: true });
fetchSsr
? await fetchSsr(API_URL + '/candidate/logout', { method: 'POST', credentials: 'include' })
: await axios.post(API_URL + '/candidate/logout', { withCredentials: true });
} catch (e: any) {
throw errorHandler(e, 'Logout failed');
}
}
};
// SSR Compatible
export const apiFetchDetails = async (fetchSsr?: Fetch): Promise<CandidateData | null> => {
export const apiFetchDetails = async (fetchSsr?: Fetch): Promise<CandidateData> => {
try {
if (fetchSsr) {
const res = await fetchSsr(API_URL + '/candidate/details', { method: "GET", credentials: 'include' });
const body = await res.text();
console.log(body);
const res = await fetchSsr(API_URL + '/candidate/details', {
method: 'GET',
credentials: 'include'
});
if (res.status != 200) {
return null;
throw new Error(await res.text());
}
return JSON.parse(body);
return await res.json();
}
const res = await axios.get(API_URL + '/candidate/details', { withCredentials: true });
return res.data;
} catch (e: any) {
console.log(e);
throw errorHandler(e, 'Failed to fill details');
}
}
};
// SSR Compatible
export const apiFetchSubmissionProgress = async (fetchSsr?: Fetch): Promise<SubmissionProgress> => {
try {
if (fetchSsr) {
const res = await fetchSsr(API_URL + '/candidate/portfolio/submission_progress', { method: "GET", credentials: 'include' });
const res = await fetchSsr(API_URL + '/candidate/portfolio/submission_progress', {
method: 'GET',
credentials: 'include'
});
if (res.status != 200) {
throw Error(await res.text());
}
return await res.json();
}
const res = await axios.get(API_URL + '/candidate/portfolio/submission_progress', { withCredentials: true });
const res = await axios.get(API_URL + '/candidate/portfolio/submission_progress', {
withCredentials: true
});
return res.data;
} catch (e: any) {
throw errorHandler(e, 'Failed to fetch submission progress');
}
}
};
export const apiWhoami = async (): Promise<string> => {
try {
@ -53,7 +64,7 @@ export const apiWhoami = async (): Promise<string> => {
} catch (e: any) {
throw errorHandler(e, 'Whoami failed');
}
}
};
export const apiLogin = async (data: CandidateLogin): Promise<number> => {
try {
@ -62,8 +73,7 @@ export const apiLogin = async (data: CandidateLogin): Promise<number> => {
} catch (e: any) {
throw errorHandler(e, 'Login failed');
}
}
};
export const apiFillDetails = async (data: CandidateData): Promise<CandidateData> => {
console.log(data);
@ -73,7 +83,7 @@ export const apiFillDetails = async (data: CandidateData): Promise<CandidateData
} catch (e: any) {
throw errorHandler(e, 'Failed to fill details');
}
}
};
export const apiUploadCoverLetter = async (
letter: File,
@ -84,15 +94,15 @@ export const apiUploadCoverLetter = async (
withCredentials: true,
data: letter,
headers: {
'Content-Type': 'application/pdf',
'Content-Type': 'application/pdf'
},
onUploadProgress: progressReporter,
onUploadProgress: progressReporter
});
return true;
} catch (e: any) {
throw errorHandler(e, 'Failed to upload cover letter');
}
}
};
export const apiUploadPortfolioLetter = async (
letter: File,
@ -103,15 +113,15 @@ export const apiUploadPortfolioLetter = async (
withCredentials: true,
data: letter,
headers: {
'Content-Type': 'application/pdf',
'Content-Type': 'application/pdf'
},
onUploadProgress: progressReporter,
onUploadProgress: progressReporter
});
return true;
} catch (e: any) {
throw errorHandler(e, 'Failed to upload cover letter');
}
}
};
export const apiUploadPortfolioZip = async (
portfolio: File,
@ -122,12 +132,12 @@ export const apiUploadPortfolioZip = async (
withCredentials: true,
data: portfolio,
headers: {
'Content-Type': 'application/zip',
'Content-Type': 'application/zip'
},
onUploadProgress: progressReporter,
onUploadProgress: progressReporter
});
return true;
} catch (e: any) {
throw errorHandler(e, 'Failed to upload cover letter');
}
}
};

View file

@ -3,22 +3,28 @@ import { redirect } from '@sveltejs/kit';
import type { LayoutServerLoad } from './$types';
export const load: LayoutServerLoad = async ({ fetch }) => {
const details = await apiFetchDetails(fetch);
if (details === null) {
let details;
try {
details = await apiFetchDetails(fetch);
} catch {
throw redirect(302, '/register');
}
const submissionProgress = await apiFetchSubmissionProgress(fetch);
try {
await apiFetchSubmissionProgress(fetch);
} catch {
// TODO:
}
return {
candidate: {
name: details.name,
surname: details.surname,
email: details.email
},
submission: {
...submissionProgress
}
/*submission: {
...submissionProgress
}*/
};
};