Skip to content

Commit

Permalink
Retool form post so error response doesn't get lost. Display error on…
Browse files Browse the repository at this point in the history
… Error page.
  • Loading branch information
willyrk1 committed Jan 6, 2024
1 parent 898a313 commit aed6ca6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
32 changes: 20 additions & 12 deletions src/apis/StFrancisRescue.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,35 @@ const StFrancisRescue = (function() {
* jwt (optional) - JWT token if the request need authentication
*
*/
function post(url, body, jwt) {
async function post(url, body, jwt) {
const headers = {
Authorization: `Basic ${jwt}`,
accept: 'application/json',
}

log('REQUEST :=', url)

return fetch(url, { method: 'POST', headers, body, redirect: 'manual' })
.then(checkFetchResponseOK)
.then(response =>
response.type === 'opaqueredirect' ? '' : response.json()
)
.then(data => {
try {
const response = await fetch(url, { method: 'POST', headers, body, redirect: 'manual' })
if (response.type === 'opaqueredirect') {
log('RESPONSE :=')
}
else {
if (!response.ok) {
const errorText = await response.text()
console.error(response)
throw Error(errorText)
}

const data = await response.json()
log('RESPONSE :=', data)
return data
})
.catch(error => {
log('ERROR :=', error)
throw error
})
}
}
catch (error) {
log('ERROR :=', error)
throw error
}
}

return {
Expand Down
22 changes: 13 additions & 9 deletions src/components/Error.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import React from 'react'
import StandardLayout from './StandardLayout'
import ErrorImage from '../assets/images/error-lolcat-problemz.jpg'
import { useLocation } from 'react-router-dom'

const Error = () => (
<>
<h1>An Error Has Occurred</h1>
export default function Error() {
const { state } = useLocation()
return (
<>
<h1>An Error Has Occurred</h1>

<p>Well this is embarrassing, we're working on it right meow!</p>
<p>Well this is embarrassing, we're working on it right meow!</p>

<img alt="" src={ErrorImage} />
</>
)
<img alt="" src={ErrorImage} />

<p style={{ marginTop: 30 }}>Error: {state.message}</p>
</>
)
}

export const ErrorPage = () => (
<StandardLayout>
<Error />
</StandardLayout>
)

export default Error
4 changes: 2 additions & 2 deletions src/components/StandardForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const StandardForm = ({
setSubmitDisabled(true)
await StFrancisRescue.postForm(event, excludeList)
navigate(nextPage)
} catch {
navigate('/error')
} catch(error) {
navigate('/error', { state: error })
}
}

Expand Down

0 comments on commit aed6ca6

Please sign in to comment.