Skip to content

Commit

Permalink
Start adding bugsUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jun 5, 2022
1 parent 2ad256b commit 75bb72f
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/error/modern/opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ export const getOpts = function (opts = {}) {
throw new TypeError(`Options must be a plain object: ${opts}`)
}

const { onCreate } = opts
const { onCreate, bugsUrl } = opts
validateOnCreate(onCreate)
return { onCreate }
const bugsUrlA = normalizeBugsUrl(bugsUrl)
return { onCreate, bugsUrl: bugsUrlA }
}

const isObject = function (value) {
Expand All @@ -18,3 +19,33 @@ const validateOnCreate = function (onCreate) {
throw new TypeError(`"onCreate" option must be a function: ${onCreate}`)
}
}

const normalizeBugsUrl = function (bugsUrl) {
if (bugsUrl === undefined) {
return
}

if (typeof bugsUrl !== 'string') {
throw new TypeError(`"bugsUrl" option must be a string: ${bugsUrl}`)
}

try {
return String(new URL(bugsUrl))
} catch (error) {
throw new Error(
`"bugsUrl" option "${bugsUrl}" must be ${getUrlError(error, bugsUrl)}`,
)
}
}

const getUrlError = function (error, bugsUrl) {
try {
// eslint-disable-next-line no-new
new URL(bugsUrl, EXAMPLE_ORIGIN)
return 'an absolute URL.'
} catch {
return `a valid URL: ${error.message}.`
}
}

const EXAMPLE_ORIGIN = 'https://www.example.com'

0 comments on commit 75bb72f

Please sign in to comment.