Skip to content

Commit

Permalink
prerender: Reply with the HTTP error status code returned from the API
Browse files Browse the repository at this point in the history
  • Loading branch information
shesek committed Oct 18, 2024
1 parent a4b445b commit 03cb94c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
4 changes: 3 additions & 1 deletion client/src/run-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export default function render(pathname, args='', body, locals={}, cb) {
cb(null, data || {
html: lastHtml
, title: lastState.title
, status: lastState.view == 'notFound' ? 404 : lastState.error ? 400 : 200
, status: lastState.view == 'notFound' ? 404
: lastState.view == 'error' ? lastState.error.status || 400
: 200
})
}

Expand Down
12 changes: 7 additions & 5 deletions client/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const dropErrors = r$$ => r$$.switchMap(r$ => r$.catch(_ => O.empty()))

export const extractErrors = r$$ =>
r$$.flatMap(r$ => r$.flatMap(_ => O.empty()).catch(err => O.of(err)))
.map(e => e.response && e.status != 502 ? parseError(e.response) : e)
.map(e => e.response ? responseError(e.response) : e)

// Create a stream that ticks every `ms`, but only when the window is focused.
// Returns an empty stream in the server-side pre-renderer environment.
Expand All @@ -126,10 +126,12 @@ export const tickWhileFocused = ms =>
export const tickWhileViewing = (ms, view, view$) =>
tickWhileFocused(ms).withLatestFrom(view$).filter(([ _, shown_view ]) => shown_view == view)

const parseError = res =>
(res.body && Object.keys(res.body).length)
? res.body.message || res.body
: res.text
const responseError = res => ({
status: res.status,
message: (res.body && Object.keys(res.body).length)
? res.body.message || res.body
: res.text
})

export const dbg = (obj, label='stream', dbg=debug(label)) =>
Object.keys(obj).forEach(k => obj[k] && obj[k].subscribe(
Expand Down
2 changes: 1 addition & 1 deletion client/src/views/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const formatError = err =>
? 'We encountered an error. Please try again later.'
: (err.status && err.status === 502)
? 'Esplora is currently unavailable, please try again later.'
: err.toString()
: (err.message || err.toString())

export const error = ({ t, error, ...S }) => layout(<div>
<div className="container text-center"><h1>{ t(formatError(error)) }</h1></div>
Expand Down

0 comments on commit 03cb94c

Please sign in to comment.