Skip to content

Commit

Permalink
Use helper function to standardize error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
katamartin committed Jan 13, 2025
1 parent 665820d commit 4af50ef
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions components/utils/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,12 @@ export const inspectDataset = async (url) => {

if (!response.ok) {
const statusText = response.statusText ?? 'Dataset request failed.'
if (response.status === 403) {
throw new Error(
`STATUS 403: Access forbidden. Ensure that URL is correct and that dataset is publicly accessible.`
)
} else if (response.status === 404) {
throw new Error(
`STATUS 404: ${statusText} Ensure that URL path is correct.`
)
} else {
throw new Error(
`STATUS ${response.status}: ${statusText}. URL: ${sanitized}`
)
}
const errorMessage = generateErrorMessage(
response.status,
statusText,
sanitized
)
throw new Error(errorMessage)
}
let metadata = await response.json()

Expand All @@ -57,6 +50,21 @@ export const inspectDataset = async (url) => {
return { url: visualizedUrl, metadata, pyramid }
}

const generateErrorMessage = (status, statusText, sanitizedUrl) => {
switch (status) {
case 403:
return `STATUS 403: Access forbidden. Ensure that URL is correct and that dataset is publicly accessible.`
case 404:
return `STATUS 404: ${statusText}. Ensure that URL path is correct.`
case 500:
case 502:
case 503:
return `STATUS ${status}: ${statusText}. The server encountered an error. Please try again later.`
default:
return `STATUS ${status}: ${statusText}. URL: ${sanitizedUrl}`
}
}

// Infer axes from consolidated metadata
export const inferCfAxes = (metadata, pyramid) => {
const prefix = pyramid ? '0/' : ''
Expand Down

0 comments on commit 4af50ef

Please sign in to comment.