Skip to content

Commit

Permalink
Merge pull request #175 from SELab-2/helper-error-translate
Browse files Browse the repository at this point in the history
Helper error translate
  • Loading branch information
tyboro2002 authored Mar 27, 2024
2 parents 97c58cb + 6412b96 commit 696a365
Show file tree
Hide file tree
Showing 17 changed files with 186 additions and 137 deletions.
16 changes: 16 additions & 0 deletions frontend/src/assets/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@
"title": "Calendar"
}
},
"composables": {
"helpers": {
"errors": {
"notFound": "Not Found",
"notFoundDetail": "Resource not found.",
"unauthorized": "Unauthorized",
"unauthorizedDetail": "You are not authorized to access this resource.",
"server": "Server Error",
"serverDetail": "An error occurred on the server.",
"network": "Network Error",
"networkDetail": "Unable to connect to the server.",
"request": "Request Error",
"requestDetail": "An error occurred while making the request."
}
}
},
"components": {
"buttons": {
"academic_year": "Academic year {0}"
Expand Down
16 changes: 16 additions & 0 deletions frontend/src/assets/lang/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@
"title": "Kalender"
}
},
"composables": {
"helpers": {
"errors": {
"notFound": "Niet Gevonden",
"notFoundDetail": "Resource niet gevonden.",
"unauthorized": "Onbevoegd",
"unauthorizedDetail": "Je bent niet bevoegd om deze resource te bereiken.",
"server": "Server Fout",
"serverDetail": "Er vond een fout plaats op de server.",
"network": "Netwerk Fout",
"networkDetail": "Kan de server niet bereiken.",
"request": "Verzoek Fout",
"requestDetail": "Een fout vond plaats tijdens het maken van het verzoek."
}
}
},
"components": {
"buttons": {
"academic_year": "Academiejaar {0}"
Expand Down
20 changes: 10 additions & 10 deletions frontend/src/composables/services/admins.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@ import {ref} from 'vue';
import {endpoints} from '@/config/endpoints.ts';
import { get, getList, create, delete_id } from '@/composables/services/helpers.ts';
import { useToast } from 'primevue/usetoast';
import {ComposerTranslation} from "vue-i18n";

export function useAdmin() {
const admins = ref<Admin[]|null>(null);
const admin = ref<Admin|null>(null);
const toast = useToast();

async function getAdminByID(id: string) {
async function getAdminByID(id: string, t: ComposerTranslation) {
const endpoint = endpoints.admins.retrieve.replace('{id}', id);
get<Admin>(endpoint, admin, Admin.fromJSON, toast);
get<Admin>(endpoint, admin, Admin.fromJSON, toast, t);
}

async function getAdmins() {
async function getAdmins(t: ComposerTranslation) {
const endpoint = endpoints.admins.index;
getList<Admin>(endpoint, admins, Admin.fromJSON, toast);
getList<Admin>(endpoint, admins, Admin.fromJSON, toast, t);
}


async function createAdmin(admin_data: any) {
async function createAdmin(admin_data: any, t: ComposerTranslation) {
const endpoint = endpoints.admins.index;
create<Admin>(endpoint, admin_data, admin, Admin.fromJSON, toast);
create<Admin>(endpoint, admin_data, admin, Admin.fromJSON, toast, t);
}

async function deleteAdmin(id: string) {
const endpoint = endpoints.admins.retrieve.replace('{id}', id.toString());
delete_id<Admin>(endpoint, admin, Admin.fromJSON, toast);
async function deleteAdmin(id: string, , t: ComposerTranslation) {
const endpoint = endpoints.admins.retrieve.replace('{id}', id);
delete_id<Admin>(endpoint, admin, Admin.fromJSON, toast, t);
}

return {
Expand Down
27 changes: 14 additions & 13 deletions frontend/src/composables/services/assistant.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,42 @@ import {ref} from 'vue';
import {endpoints} from '@/config/endpoints.ts';
import { get, getList, create, delete_id } from '@/composables/services/helpers.ts';
import { useToast } from 'primevue/usetoast';
import {ComposerTranslation} from "vue-i18n";

export function useAssistant() {
const assistants = ref<Assistant[]|null>(null);
const assistant = ref<Assistant|null>(null);
const response = ref<Response|null>(null);
const toast = useToast();

async function getAssistantByID(id: string) {
async function getAssistantByID(id: string, t: ComposerTranslation) {
const endpoint = endpoints.assistants.retrieve.replace('{id}', id);
get<Assistant>(endpoint, assistant, Assistant.fromJSON, toast);
get<Assistant>(endpoint, assistant, Assistant.fromJSON, toast, t);
}

async function getAssistantByCourse(course_id: string) {
async function getAssistantByCourse(course_id: string, t: ComposerTranslation) {
const endpoint = endpoints.assistants.byCourse.replace('{course_id}', course_id);
get<Assistant>(endpoint, assistant, Assistant.fromJSON, toast);
get<Assistant>(endpoint, assistant, Assistant.fromJSON, toast, t);
}

async function getAssistants() {
async function getAssistants(t: ComposerTranslation) {
const endpoint = endpoints.assistants.index;
getList<Assistant>(endpoint, assistants, Assistant.fromJSON, toast);
getList<Assistant>(endpoint, assistants, Assistant.fromJSON, toast, t);
}

async function assistantJoinCourse(course_id: string, assistant_id: string) {
async function assistantJoinCourse(course_id: string, assistant_id: string, t: ComposerTranslation) {
const endpoint = endpoints.assistants.byCourse.replace('{course_id}', course_id);
create<Response>(endpoint, {assistant_id: assistant_id}, response, Response.fromJSON, toast);
create<Response>(endpoint, {assistant_id: assistant_id}, response, Response.fromJSON, toast, t);
}

async function createAssistant(assistant_data: any) {
async function createAssistant(assistant_data: any, t: ComposerTranslation) {
const endpoint = endpoints.assistants.index;
create<Assistant>(endpoint, assistant_data, assistant, Assistant.fromJSON, toast);
create<Assistant>(endpoint, assistant_data, assistant, Assistant.fromJSON, toast, t);
}

async function deleteAssistant(id: string) {
const endpoint = endpoints.admins.retrieve.replace('{id}', id.toString());
delete_id<Assistant>(endpoint, assistant, Assistant.fromJSON, toast);
async function deleteAssistant(id: string, t: ComposerTranslation) {
const endpoint = endpoints.admins.retrieve.replace('{id}', id);
delete_id<Assistant>(endpoint, assistant, Assistant.fromJSON, toast, t);
}

return {
Expand Down
31 changes: 16 additions & 15 deletions frontend/src/composables/services/courses.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,41 @@ import {ref} from 'vue';
import {endpoints} from '@/config/endpoints.ts';
import { get, getList, create, delete_id } from '@/composables/services/helpers.ts';
import { useToast } from 'primevue/usetoast';
import {ComposerTranslation} from "vue-i18n";

export function useCourses() {
const courses = ref<Course[]|null>(null);
const course = ref<Course|null>(null);
const toast = useToast();

async function getCourseByID(id: string) {
async function getCourseByID(id: string, t: ComposerTranslation) {
const endpoint = endpoints.courses.retrieve.replace('{id}', id);
get<Course>(endpoint, course, Course.fromJSON, toast);
get<Course>(endpoint, course, Course.fromJSON, toast, t);
}

async function getCourses() {
async function getCourses(t: ComposerTranslation) {
const endpoint = endpoints.courses.index;
getList<Course>(endpoint, courses, Course.fromJSON, toast);
getList<Course>(endpoint, courses, Course.fromJSON, toast, t);
}

async function createCourse(course_data: any) {
async function getCoursesByStudent(student_id: string, t: ComposerTranslation) {
const endpoint = endpoints.courses.byStudent.replace('{student_id}', student_id);
getList<Course>(endpoint, courses, Course.fromJSON, toast, t);
}

async function createCourse(course_data: any, t: ComposerTranslation) {
const endpoint = endpoints.courses.index;
create<Course>(endpoint, course_data, course, Course.fromJSON, toast);
create<Course>(endpoint, course_data, course, Course.fromJSON, toast, t);
}

async function cloneCourse(course_id: string, clone_assistants: boolean) {
async function cloneCourse(course_id: string, clone_assistants: boolean, t: ComposerTranslation) {
const endpoint = endpoints.courses.clone.replace('{course_id}', course_id);
create<Course>(endpoint, {clone_assistants: clone_assistants.toString() }, course, Course.fromJSON, toast);
create<Course>(endpoint, {clone_assistants: clone_assistants.toString() }, course, Course.fromJSON, toast, t);
}

async function deleteCourse(id: string) {
async function deleteCourse(id: string, t: ComposerTranslation) {
const endpoint = endpoints.courses.retrieve.replace('{id}', id);
delete_id<Course>(endpoint, course, Course.fromJSON, toast);
}

async function getCoursesByStudent(student_id: string) {
const endpoint = endpoints.courses.byStudent.replace('{student_id}', student_id);
getList<Course>(endpoint, courses, Course.fromJSON, toast);
delete_id<Course>(endpoint, course, Course.fromJSON, toast, t);
}


Expand Down
17 changes: 9 additions & 8 deletions frontend/src/composables/services/faculties.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,31 @@ import {ref} from 'vue';
import {endpoints} from '@/config/endpoints.ts';
import { get, getList, create, delete_id } from '@/composables/services/helpers.ts';
import { useToast } from 'primevue/usetoast';
import {ComposerTranslation} from "vue-i18n";

export function useFaculty() {
const faculties = ref<Faculty[]|null>(null);
const faculty = ref<Faculty|null>(null);
const toast = useToast();

async function getFacultyByID(name: string) {
async function getFacultyByID(name: string, t: ComposerTranslation) {
const endpoint = endpoints.faculties.retrieve.replace('{name}', name);
get<Faculty>(endpoint, faculty, Faculty.fromJSON, toast);
get<Faculty>(endpoint, faculty, Faculty.fromJSON, toast, t);
}

async function getFacultys() {
async function getFacultys(t: ComposerTranslation) {
const endpoint = endpoints.faculties.index;
getList<Faculty>(endpoint, faculties, Faculty.fromJSON, toast);
getList<Faculty>(endpoint, faculties, Faculty.fromJSON, toast, t);
}

async function createFaculty(faculty_data: any) {
async function createFaculty(faculty_data: any, t: ComposerTranslation) {
const endpoint = endpoints.faculties.index;
create<Faculty>(endpoint, faculty_data, faculty, Faculty.fromJSON, toast);
create<Faculty>(endpoint, faculty_data, faculty, Faculty.fromJSON, toast, t);
}

async function deleteFaculty(id: string) {
async function deleteFaculty(id: string, t: ComposerTranslation) {
const endpoint = endpoints.faculties.retrieve.replace('{id}', id);
delete_id<Faculty>(endpoint, faculty, Faculty.fromJSON, toast);
delete_id<Faculty>(endpoint, faculty, Faculty.fromJSON, toast, t);
}

return {
Expand Down
17 changes: 9 additions & 8 deletions frontend/src/composables/services/groups.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,31 @@ import {ref} from 'vue';
import {endpoints} from '@/config/endpoints.ts';
import { get, getList, create, delete_id } from '@/composables/services/helpers.ts';
import { useToast } from 'primevue/usetoast';
import {ComposerTranslation} from "vue-i18n";

export function useGroup() {
const groups = ref<Group[]|null>(null);
const group = ref<Group|null>(null);
const toast = useToast();

async function getGroupByID(id: string) {
async function getGroupByID(id: string, t: ComposerTranslation) {
const endpoint = endpoints.groups.retrieve.replace('{id}', id);
get<Group>(endpoint, group, Group.fromJSON, toast);
get<Group>(endpoint, group, Group.fromJSON, toast, t);
}

async function getGroupsByProject(project_id: string) {
async function getGroupsByProject(project_id: string, t: ComposerTranslation) {
const endpoint = endpoints.groups.byProject.replace('{project_id}', project_id);
getList<Group>(endpoint, groups, Group.fromJSON, toast);
getList<Group>(endpoint, groups, Group.fromJSON, toast, t);
}

async function createGroup(group_data: any, group_id: string) {
async function createGroup(group_data: any, group_id: string, t: ComposerTranslation) {
const endpoint = endpoints.groups.byProject.replace('{group_id}', group_id);
create<Group>(endpoint, group_data, group, Group.fromJSON, toast);
create<Group>(endpoint, group_data, group, Group.fromJSON, toast, t);
}

async function deleteGroup(id: string) {
async function deleteGroup(id: string, t: ComposerTranslation) {
const endpoint = endpoints.groups.retrieve.replace('{id}', id);
delete_id<Group>(endpoint, group, Group.fromJSON, toast);
delete_id<Group>(endpoint, group, Group.fromJSON, toast, t);
}

return {
Expand Down
37 changes: 20 additions & 17 deletions frontend/src/composables/services/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import axios, { AxiosError, AxiosResponse } from 'axios';
// import { useI18n } from 'vue-i18n';
import {Ref} from 'vue';
import {ComposerTranslation} from "vue-i18n";
const lifeTime = 3000;

export async function get<T>(endpoint: string, ref: Ref<T|null>, fromJson: (data: any) => T, toast:any): Promise<void> {
// const { t } = useI18n();

export async function get<T>(endpoint: string, ref: Ref<T|null>, fromJson: (data: any) => T, toast:any, t: ComposerTranslation): Promise<void> {
await axios.get(endpoint).then((response: AxiosResponse) => {
ref.value = fromJson(response.data);
//toast.add({severity: "success", summary: "Success Message", detail: "Order submitted", life: lifeTime});
}).catch((error: AxiosError) => {
processError(error, toast);
processError(error, toast, t);
console.error(error); // Log the error for debugging
});
}

export async function create<T>(endpoint: string, data:any, ref: Ref<T|null>, fromJson: (data: any) => T, toast:any): Promise<void> {
export async function create<T>(endpoint: string, data:any, ref: Ref<T|null>, fromJson: (data: any) => T, toast:any, t: ComposerTranslation): Promise<void> {
const headers = {
// TODO change this to your token
Authorization: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzQyODQwMjY1LCJpYXQiOjE3MTEzMDQyNjUsImp0aSI6ImQwYTgxY2YxMzU5NTQ4OWQ4OGNiZDFmZmZiMGI0MmJhIiwidXNlcl9pZCI6IjAwMDIwMTI0NzAxMSJ9.izGK0MStcMiPkOAWs0wgWsYEs0_5S1WvsleWaIcttnk"
Expand All @@ -21,12 +25,12 @@ export async function create<T>(endpoint: string, data:any, ref: Ref<T|null>, fr
ref.value = fromJson(response.data);
//toast.add({severity: "success", summary: "Success Message", detail: "Order submitted", life: lifeTime});
}).catch((error: AxiosError) => {
processError(error, toast);
processError(error, toast, t);
console.error(error); // Log the error for debugging
});
}

export async function delete_id<T>(endpoint: string, ref: Ref<T|null>, fromJson: (data: any) => T, toast:any): Promise<void> {
export async function delete_id<T>(endpoint: string, ref: Ref<T|null>, fromJson: (data: any) => T, toast:any, t: ComposerTranslation): Promise<void> {
const headers = {
// TODO change this to your token
Authorization: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzQyODQwMjY1LCJpYXQiOjE3MTEzMDQyNjUsImp0aSI6ImQwYTgxY2YxMzU5NTQ4OWQ4OGNiZDFmZmZiMGI0MmJhIiwidXNlcl9pZCI6IjAwMDIwMTI0NzAxMSJ9.izGK0MStcMiPkOAWs0wgWsYEs0_5S1WvsleWaIcttnk"
Expand All @@ -35,25 +39,24 @@ export async function delete_id<T>(endpoint: string, ref: Ref<T|null>, fromJson:
ref.value = fromJson(response.data);
//toast.add({severity: "success", summary: "Success Message", detail: "Order submitted", life: lifeTime});
}).catch((error: AxiosError) => {
processError(error, toast);
processError(error, toast, t);
console.error(error); // Log the error for debugging
});
}


export async function getList<T>(endpoint: string, ref: Ref<T[]|null>, fromJson: (data: any) => T, toast:any): Promise<void> {
export async function getList<T>(endpoint: string, ref: Ref<T[]|null>, fromJson: (data: any) => T, toast:any, t: ComposerTranslation): Promise<void> {
await axios.get(endpoint).then(response => {
ref.value = response.data.map((data: T) => fromJson(data));
//toast.add({severity: "success", summary: "Success Message", detail: "Order submitted", life: lifeTime});
console.log(ref.value);
}
).catch((error: AxiosError) => {
processError(error, toast);
processError(error, toast, t);
console.error(error); // Log the error for debugging
});
}

export async function getListMerged<T>(endpoints: string[], ref: Ref<T[]|null>, fromJson: (data: any) => T, toast:any): Promise<void> {
export async function getListMerged<T>(endpoints: string[], ref: Ref<T[]|null>, fromJson: (data: any) => T, toast:any, t: ComposerTranslation): Promise<void> {

// Create an array to accumulate all response data
const allData: T[] = [];
Expand All @@ -66,29 +69,29 @@ export async function getListMerged<T>(endpoints: string[], ref: Ref<T[]|null>,
// toast.add({severity: "success", summary: "Success Message", detail: "Order submitted", life: lifeTime});
}
).catch((error: AxiosError) => {
processError(error, toast);
processError(error, toast, t);
console.error(error); // Log the error for debugging
});
}
ref.value = allData;
}

export function processError(error: AxiosError, toast:any){
export function processError(error: AxiosError, toast:any, t: ComposerTranslation){
if (error.response) {
console.log(error.response.status);
// The request was made and the server responded with a status code
if (error.response.status === 404) {
toast.add({ severity: 'error', summary: 'Not Found', detail: 'Resource not found.', life: lifeTime });
toast.add({ severity: 'error', summary: t('composables.helpers.errors.notFound'), detail: t('composables.helpers.errors.notFoundDetail'), life: lifeTime });
} else if (error.response.status === 401) {
toast.add({ severity: 'error', summary: 'Unauthorized', detail: 'You are not authorized to access this resource.', life: lifeTime });
toast.add({ severity: 'error', summary: t('composables.helpers.errors.unauthorized'), detail: t('composables.helpers.errors.unauthorizedDetail'), life: lifeTime });
} else {
toast.add({ severity: 'error', summary: 'Server Error', detail: 'An error occurred on the server.', life: lifeTime });
toast.add({ severity: 'error', summary: t('composables.helpers.errors.server'), detail: t('composables.helpers.errors.serverDetail'), life: lifeTime });
}
} else if (error.request) {
// The request was made but no response was received
toast.add({ severity: 'error', summary: 'Network Error', detail: 'Unable to connect to the server.', life: lifeTime });
toast.add({ severity: 'error', summary: t('composables.helpers.errors.network'), detail: t('composables.helpers.errors.networkDetail'), life: lifeTime });
} else {
// Something happened in setting up the request that triggered an error
toast.add({ severity: 'error', summary: 'Request Error', detail: 'An error occurred while making the request.', life: lifeTime });
toast.add({ severity: 'error', summary: t('composables.helpers.errors.request'), detail: t('composables.helpers.errors.requestDetail'), life: lifeTime });
}
}
Loading

0 comments on commit 696a365

Please sign in to comment.