1
0
mirror of https://github.com/comatory/fb2iCal synced 2025-01-27 15:49:17 +01:00

add JSON response for errors when using JS

This commit is contained in:
Ondrej Synacek 2019-10-21 21:20:03 +02:00
parent 6a1ef614ad
commit 31a54b4ef7
3 changed files with 34 additions and 18 deletions

View File

@ -1,7 +1,19 @@
const FBURLError = () => new Error('Not a valid Facebook URL!')
const sendJSON = (req) => {
return req.accepts().includes('application/json')
}
const genericErrorHandler = (err, req, res, next) => {
console.error(err.stack)
if (sendJSON(req)) {
res
.status(500)
.send({ error: err.toString() })
return
}
res
.status(500)
.render('error', { error: err.toString() })

View File

@ -172,7 +172,6 @@
}
const handleError = (error) => {
console.error(error)
finishedRequest()
setStatusError(error)
}
@ -182,11 +181,16 @@
fetch('/download', {
method: 'POST',
headers: {
'Accept': 'text/calendar, application/json',
'Content-Type': 'application/x-www-form-urlencoded',
},
body: data,
}).then((response) => {
if (response.status === 500) {
if (response.status !== 200) {
if (response.body.constructor === ReadableStream) {
response.json().then((json) => reject(json.error || response.statusText))
return
}
reject(response.statusText)
return
}
@ -195,6 +199,7 @@
}).catch(reject)
})
}
window.postURL = postURL
const form = document.querySelector('form')
const submitButton = document.querySelector("#submit")

View File

@ -23,22 +23,21 @@
into your calendar.
</p>
<div class="row">
<form action='/download' method='POST' id="form">
<input id="url" type='text' name='url' placeholder='Paste / type FB event URL here...' />
<input id="submit" type='submit' value='Submit' />
</form>
<div id="status">
<div class="status-item" id="network">
Fetching file...
</div>
<div class="status-item" id="parsing">
Parsing data...
</div>
<div class="status-item" id="error">
</div>
<div class="status-item" id="service-worker">
</div>
<form action='/download' method='POST' id="form">
<input id="url" type='text' name='url' placeholder='Paste / type FB event URL here...' />
<input id="submit" type='submit' value='Submit' />
</form>
<div id="status">
<div class="status-item" id="network">
Fetching file...
</div>
<div class="status-item" id="parsing">
Parsing data...
</div>
<div class="status-item" id="error">
</div>
<div class="status-item" id="service-worker">
</div>
</div>