Commit 1a885d00 authored by Erwan BOUSSE's avatar Erwan BOUSSE
Browse files

Add basic error management

parent 6df4f7e5
Pipeline #18619 passed with stages
in 47 seconds
......@@ -6,19 +6,16 @@ let ICAL = require("ical.js")
export function extractAndPrint(): void {
// Get inputs
// Get inputs from HTML
let firstName = (document.getElementById("fname") as HTMLInputElement).value
let lastName = (document.getElementById("lname") as HTMLInputElement).value
let courseId = (document.getElementById("courseID") as HTMLInputElement).value
extractAndPrintInternal(firstName, lastName, courseId, getMode(), webLog, webFlush)
wrapExec(() => { extractAndPrintInternal(firstName, lastName, courseId, getMode(), webLog, webFlush) }, webLog)
}
function extractAndPrintInternal(firstName: string, lastName: string, courseId: string, mode: Mode, log: (string) => void, flush: () => void): void {
// Clear output
flush()
switch (mode) {
case Mode.PERSON_AND_COURSE:
extractAndPrintPersonAndCourse(firstName, lastName, courseId, log)
......@@ -122,10 +119,8 @@ function sortEventsPerPerson(events: any[]): Map<string, any[]> {
}
function getAllURLsFromCelcatPageAsync(url: string, then: (allICS: Map<string, string>) => void) {
httpGet(url, (responseText: string) => {
// Find ICS
function getAllURLsFromCelcatPageAsync(url: string, then: (allICS: Map<string, string>) => void, log) {
httpGetAsync(url, (responseText: string) => {
let domparser: DOMParser = new DOMParser();
let parsedContent = domparser.parseFromString(responseText, 'text/html')
let options: HTMLCollection = parsedContent.getElementsByTagName("option")
......@@ -138,7 +133,7 @@ function getAllURLsFromCelcatPageAsync(url: string, then: (allICS: Map<string, s
}
}
then(allURLs)
})
}, log)
}
......@@ -148,11 +143,10 @@ function getEventsPersonAsync(firstName: string, lastName: string, log: (string:
getAllURLsFromCelcatPageAsync(url, (allURLs) => {
let fullName: string = `${lastName}, ${firstName}`
let icsURL: string = allURLs.get(fullName.toLowerCase())
// Download and parse ICS
getEventsFromICS(icsURL, log, (allEvents) => {
getEventsFromICSAsync(icsURL, log, (allEvents) => {
then(allEvents)
})
})
}, log)
}
function getEventsCourseAsync(courseID: string, log: (string: any) => void, then: (allEvents: any[]) => void): void {
......@@ -161,42 +155,47 @@ function getEventsCourseAsync(courseID: string, log: (string: any) => void, then
for (let key of allURLs.keys()) {
if (key.includes(courseID.toLowerCase())) {
let icsURL = allURLs.get(key)
getEventsFromICS(icsURL, log, (allEvents) => {
getEventsFromICSAsync(icsURL, log, (allEvents) => {
then(allEvents)
})
break
}
}
})
}, log)
}
function getEventsFromICS(url: string, log, then) {
function getEventsFromICSAsync(url: string, log, then) {
log("Récupération de l'emploi du temps depuis " + url)
httpGet(url, (responseText) => {
httpGetAsync(url, (responseText) => {
let icsContent: string = responseText
let jcalData = ICAL.parse(icsContent);
let structuredData = new ICAL.Component(jcalData);
// Get all events for the course
let allEvents = (structuredData.getAllSubcomponents("vevent") as Array<any>).map(vevent => new ICAL.Event(vevent))
log(allEvents.length + " créneaux trouvés dans cet ICS !")
log(allEvents.length + " créneaux trouvés dans cet ICS.")
then(allEvents)
})
}, log)
}
function httpGet(url: string, then) {
function httpGetAsync(url: string, then, log) {
let xmlHttp: XMLHttpRequest = new XMLHttpRequest();
xmlHttp.open("GET", url)
xmlHttp.send()
xmlHttp.onloadend = () => { then(xmlHttp.responseText) }
xmlHttp.onloadend = () => {
wrapExec(() => {
then(xmlHttp.responseText)
}, log)
}
}
//TODO there could be multiple persons per event
function getPersonFromEvent(event: any): string {
let enseignantResult = event.description.match('Personnel : (.*)\n')
let enseignant: string = enseignantResult[1]
return enseignant
if (enseignantResult) {
return enseignantResult[1]
} else {
return "Intervenant(s) inconnu(s)"
}
}
function getCourseFromEvent(event: any): string {
......@@ -224,7 +223,6 @@ function getCourseIDFromEvent(event: any): string {
} else {
return "UE(s) inconnue(s)"
}
}
......@@ -330,10 +328,20 @@ export function initPage(): void {
modeChanged()
}
function wrapExec(f, log) {
try {
f()
} catch (e) {
log("")
log("##### Une erreur imprévue a été rencontrée : " + e)
}
}
//extractAndPrintInternal("erwan", "bousse", "X4IP120", console.log, () => { })
//extractAndPrintInternal("erwan", "bousse", "", console.log, () => { })
//extractAndPrintInternal("", "", "X4IP120", console.log, () => { })
//extractAndPrintInternal("", "", "X12I040", console.log, () => { })
//extractAndPrintInternal("", "", "X3IA040", console.log, () => { })
//extractAndPrintInternal("", "", "X3IA040", console.log, () => { })
// extractAndPrintInternal("Emmanuel", "Desmontils", "X2IM030", Mode.PERSON, console.log, () => { })
\ No newline at end of file
//extractAndPrintInternal("Emmanuel", "Desmontils", "X2IM030", Mode.PERSON, console.log, () => { })
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment