Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hooks/useBoundary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function useBoundary(area: Area, code: string) {
throw new Error(
res.statusCode === 404
? `Data not found for ${area} ${code}`
: `Unexpected status code: ${res.statusCode}`,
: res.message || `Unexpected status code: ${res.statusCode}`,
)
},
})
Expand Down
36 changes: 33 additions & 3 deletions lib/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,25 @@ export async function getData<A extends Area, P extends string | Query<A>>(
url.searchParams.append('page', query.page.toString())
}

const res = await fetch(url)
let res: Response
try {
res = await fetch(url)
} catch (error) {
return {
statusCode: 500,
message: `Unable to connect to the server at ${url.host}. Please check your network connection or verify that the server is reachable.`,
error: (error as Error).message,
}
}

if (!res.ok) {
const errorMessage = await res.text()
return {
statusCode: res.status,
message: `Failed to fetch data, ${errorMessage}`,
error: res.statusText,
}
}

return await res.json()
}
Expand All @@ -110,8 +128,20 @@ export async function getBoundaryData(
area: Area,
code: string,
): Promise<BoundaryResponse> {
const url = `${config.dataSource.boundary.url}/${endpoints[area]}/${addDotSeparator(code.replaceAll('.', ''))}.geojson`
const res = await fetch(url)
const url = new URL(
`${config.dataSource.boundary.url}/${endpoints[area]}/${addDotSeparator(code.replaceAll('.', ''))}.geojson`,
)

let res: Response
try {
res = await fetch(url)
} catch (error) {
return {
statusCode: 500,
message: `Unable to connect to the server at ${url.host}. Please check your network connection or verify that the server is reachable.`,
data: undefined,
}
}

return {
statusCode: res.status,
Expand Down
Loading