Skip to content

Commit

Permalink
chore: created new type for API errors, created and implemented type …
Browse files Browse the repository at this point in the history
…predicates (#6877)

* chore: created new type for API errors, created and implemented type predicates

Created APIError interface in `command-helpers.ts`.
Created two type predicates `isAPIError` and `errorHasStatus`.
Implemented predicates across the code base.

Co-authored-by: Ben Hancock <benhancock859@gmail.com>

* chore: deleted remaining TSFIXME comments that were not needed anymore

Deleted remaining TSFIXME comments that were not needed anymore
due to adding `unknown` type to custom `error` function.

Co-authored-by: Ben Hancock <benhancock859@gmail.com>

* fix: deleted unnecessary comment in command-helpers.ts

* fix: added missing argument in fn call in status.ts

* Update src/utils/hooks/requires-site-info.ts

Co-authored-by: Daniel Lew <51924260+DanielSLew@users.noreply.github.com>

* Update src/utils/framework-server.ts

Co-authored-by: Daniel Lew <51924260+DanielSLew@users.noreply.github.com>

* fix: removed type predicates in favor of type assertion for APIError

Co-authored-by: Ben Hancock <benhancock859@gmail.com>

* fix: fixed syntax error in deploy.ts

Co-authored-by: Ben Hancock <benhancock859@gmail.com>

---------

Co-authored-by: Ben Hancock <benhancock859@gmail.com>
Co-authored-by: Daniel Lew <51924260+DanielSLew@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 18, 2024
1 parent 5c13f5d commit e9623e6
Show file tree
Hide file tree
Showing 21 changed files with 67 additions and 86 deletions.
5 changes: 2 additions & 3 deletions src/commands/addons/addons-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ADDON_VALIDATION, prepareAddonCommand } from '../../utils/addons/prepar
import generatePrompts from '../../utils/addons/prompts.js'
import { renderConfigValues } from '../../utils/addons/render.js'
import { missingConfigValues, requiredConfigValues, updateConfigValues } from '../../utils/addons/validation.js'
import { chalk, error, log } from '../../utils/command-helpers.js'
import { chalk, error, log, APIError } from '../../utils/command-helpers.js'
import { parseRawFlags } from '../../utils/parse-raw-flags.js'
import BaseCommand from '../base-command.js'

Expand All @@ -35,8 +35,7 @@ const update = async function ({ addonName, api, currentConfig, instanceId, newC
})
log(`Add-on "${addonName}" successfully updated`)
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
error(error_.message)
error((error_ as APIError).message)
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/commands/addons/addons-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ADDON_VALIDATION, prepareAddonCommand } from '../../utils/addons/prepar
import generatePrompts from '../../utils/addons/prompts.js'
import { renderConfigValues, renderMissingValues } from '../../utils/addons/render.js'
import { missingConfigValues, requiredConfigValues, updateConfigValues } from '../../utils/addons/validation.js'
import { chalk, error, log } from '../../utils/command-helpers.js'
import { chalk, error, log, APIError } from '../../utils/command-helpers.js'
import { parseRawFlags } from '../../utils/parse-raw-flags.js'
import BaseCommand from '../base-command.js'
// @ts-expect-error TS(7031) FIXME: Binding element 'addonName' implicitly has an 'any... Remove this comment to see the full error message
Expand All @@ -23,8 +23,7 @@ const createAddon = async ({ addonName, api, config, siteData, siteId }) => {
log(`${response.config.message}`)
}
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
error(error_.message)
error((error_ as APIError).message)
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/commands/addons/addons-delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { OptionValues } from 'commander'
import inquirer from 'inquirer'

import { ADDON_VALIDATION, prepareAddonCommand } from '../../utils/addons/prepare.js'
import { error, exit, log } from '../../utils/command-helpers.js'
import { error, exit, log, APIError } from '../../utils/command-helpers.js'
import BaseCommand from '../base-command.js'

export const addonsDelete = async (addonName: string, options: OptionValues, command: BaseCommand) => {
Expand Down Expand Up @@ -31,7 +31,6 @@ export const addonsDelete = async (addonName: string, options: OptionValues, com
})
log(`Addon "${addonName}" deleted`)
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
error(error_.message)
error((error_ as APIError).message)
}
}
1 change: 0 additions & 1 deletion src/commands/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export const apiCommand = async (apiMethod: string, options: OptionValues, comma
const apiResponse = await api[apiMethod](payload)
logJson(apiResponse)
} catch (error_) {
// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
error(error_)
}
}
1 change: 0 additions & 1 deletion src/commands/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,6 @@ export default class BaseCommand extends Command {
// the option to say that we don't need API data.)
if (isUserError && !config.offline && config.token) {
if (flags.debug) {
// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
error(error_, { exit: false })
warn('Failed to resolve config, falling back to offline resolution')
}
Expand Down
7 changes: 3 additions & 4 deletions src/commands/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
log,
logJson,
warn,
APIError,
} from '../../utils/command-helpers.js'
import { DEFAULT_DEPLOY_TIMEOUT } from '../../utils/deploy/constants.js'
import { deploySite } from '../../utils/deploy/deploy-site.js'
Expand Down Expand Up @@ -58,12 +59,10 @@ const triggerDeploy = async ({ api, options, siteData, siteId }) => {
)
}
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
if (error_.status === 404) {
if ((error_ as APIError).status === 404) {
error('Site not found. Please rerun "netlify link" and make sure that your site has CI configured.')
} else {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
error(error_.message)
error((error_ as APIError).message)
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions src/commands/functions/functions-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ import ora from 'ora'

import { fileExistsAsync } from '../../lib/fs.js'
import { getAddons, getCurrentAddon, getSiteData } from '../../utils/addons/prepare.js'
import { NETLIFYDEVERR, NETLIFYDEVLOG, NETLIFYDEVWARN, chalk, error, log } from '../../utils/command-helpers.js'
import {
APIError,
NETLIFYDEVERR,
NETLIFYDEVLOG,
NETLIFYDEVWARN,
chalk,
error,
log,
} from '../../utils/command-helpers.js'
import { copyTemplateDir } from '../../utils/copy-template-dir/copy-template-dir.js'
import { getDotEnvVariables, injectEnvVariables } from '../../utils/dev.js'
import execa from '../../utils/execa.js'
Expand Down Expand Up @@ -492,7 +500,6 @@ const scaffoldFromTemplate = async function (command, options, argumentName, fun
await downloadFromURL(command, options, argumentName, functionsDir)
} catch (error_) {
error(`$${NETLIFYDEVERR} Error downloading from URL: ${options.url}`)
// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
error(error_)
process.exit(1)
}
Expand Down Expand Up @@ -583,8 +590,7 @@ const createFunctionAddon = async function ({ addonName, addons, api, siteData,
log(`Add-on "${addonName}" created for ${siteData.name}`)
return true
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
error(error_.message)
error((error_ as APIError).message)
}
}

Expand Down Expand Up @@ -682,8 +688,7 @@ const installAddons = async function (command, functionAddons, fnPath) {

await handleAddonDidInstall({ addonCreated, addonDidInstall, command, fnPath })
} catch (error_) {
// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
error(`${NETLIFYDEVERR} Error installing addon: `, error_)
error(`${NETLIFYDEVERR} Error installing addon: ${error_}`)
}
})
return Promise.all(arr)
Expand Down
5 changes: 2 additions & 3 deletions src/commands/functions/functions-invoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { OptionValues } from 'commander'
import inquirer from 'inquirer'
import fetch from 'node-fetch'

import { NETLIFYDEVWARN, chalk, error, exit } from '../../utils/command-helpers.js'
import { APIError, NETLIFYDEVWARN, chalk, error, exit } from '../../utils/command-helpers.js'
import { BACKGROUND, CLOCKWORK_USERAGENT, getFunctions } from '../../utils/functions/index.js'
import BaseCommand from '../base-command.js'

Expand Down Expand Up @@ -234,7 +234,6 @@ export const functionsInvoke = async (nameArgument: string, options: OptionValue
const data = await response.text()
console.log(data)
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
error(`Ran into an error invoking your function: ${error_.message}`)
error(`Ran into an error invoking your function: ${(error_ as APIError).message}`)
}
}
21 changes: 6 additions & 15 deletions src/commands/link/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import inquirer from 'inquirer'
import isEmpty from 'lodash/isEmpty.js'

import { listSites } from '../../lib/api.js'
import { chalk, error, exit, log } from '../../utils/command-helpers.js'
import { chalk, error, exit, log, APIError } from '../../utils/command-helpers.js'
import getRepoData from '../../utils/get-repo-data.js'
import { ensureNetlifyIgnore } from '../../utils/gitignore.js'
import { track } from '../../utils/telemetry/index.js'
Expand Down Expand Up @@ -125,11 +125,9 @@ Run ${chalk.cyanBright('git remote -v')} to see a list of your git remotes.`)
options: { name: searchTerm, filter: 'all' },
})
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
if (error_.status === 404) {
if ((error_ as APIError).status === 404) {
error(`'${searchTerm}' not found`)
} else {
// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
error(error_)
}
}
Expand Down Expand Up @@ -172,7 +170,6 @@ or run ${chalk.cyanBright('netlify sites:create')} to create a site.`)
try {
sites = await listSites({ api, options: { maxPages: 1, filter: 'all' } })
} catch (error_) {
// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
error(error_)
}

Expand Down Expand Up @@ -209,11 +206,9 @@ or run ${chalk.cyanBright('netlify sites:create')} to create a site.`)
try {
site = await api.getSite({ siteId })
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
if (error_.status === 404) {
error(new Error(`Site ID '${siteId}' not found`))
if ((error_ as APIError).status === 404) {
error(`Site ID '${siteId}' not found`)
} else {
// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
error(error_)
}
}
Expand Down Expand Up @@ -281,11 +276,9 @@ export const link = async (options: OptionValues, command: BaseCommand) => {
try {
siteData = await api.getSite({ site_id: options.id })
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
if (error_.status === 404) {
if ((error_ as APIError).status === 404) {
error(new Error(`Site id ${options.id} not found`))
} else {
// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
error(error_)
}
}
Expand All @@ -310,11 +303,9 @@ export const link = async (options: OptionValues, command: BaseCommand) => {
},
})
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
if (error_.status === 404) {
if ((error_ as APIError).status === 404) {
error(new Error(`${options.name} not found`))
} else {
// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
error(error_)
}
}
Expand Down
1 change: 0 additions & 1 deletion src/commands/lm/lm-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export const lmSetup = async (options: OptionValues, command: BaseCommand) => {
try {
helperInstalled = await installHelperIfMissing({ force: options.forceInstall })
} catch (error_) {
// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
error(error_)
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/commands/sites/sites-create-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import pick from 'lodash/pick.js'
import parseGitHubUrl from 'parse-github-url'
import { render } from 'prettyjson'

import { chalk, error, getTerminalLink, log, logJson, warn } from '../../utils/command-helpers.js'
import { chalk, error, getTerminalLink, log, logJson, warn, APIError } from '../../utils/command-helpers.js'
import execa from '../../utils/execa.js'
import getRepoData from '../../utils/get-repo-data.js'
import { getGitHubToken } from '../../utils/init/config-github.js'
Expand Down Expand Up @@ -163,16 +163,14 @@ export const sitesCreateTemplate = async (repository: string, options: OptionVal
})
}
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
if (error_.status === 422 || error_.message === 'Duplicate repo') {
if ((error_ as APIError).status === 422 || (error_ as APIError).message === 'Duplicate repo') {
warn(
`${name}.netlify.app already exists or a repository named ${name} already exists on this account. Please try a different slug.`,
)
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
await inputSiteName()
} else {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
error(`createSiteInTeam error: ${error_.status}: ${error_.message}`)
error(`createSiteInTeam error: ${(error_ as APIError).status}: ${(error_ as APIError).message}`)
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/commands/sites/sites-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import inquirer from 'inquirer'
import pick from 'lodash/pick.js'
import prettyjson from 'prettyjson'

import { chalk, error, log, logJson, warn } from '../../utils/command-helpers.js'
import { chalk, error, log, logJson, warn, APIError } from '../../utils/command-helpers.js'
import getRepoData from '../../utils/get-repo-data.js'
import { configureRepo } from '../../utils/init/config.js'
import { track } from '../../utils/telemetry/index.js'
Expand Down Expand Up @@ -70,14 +70,12 @@ export const sitesCreate = async (options: OptionValues, command: BaseCommand) =
body,
})
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
if (error_.status === 422) {
if ((error_ as APIError).status === 422) {
warn(`${siteName}.netlify.app already exists. Please try a different slug.`)
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
await inputSiteName()
} else {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
error(`createSiteInTeam error: ${error_.status}: ${error_.message}`)
error(`createSiteInTeam error: ${(error_ as APIError).status}: ${(error_ as APIError).message}`)
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/commands/sites/sites-delete.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OptionValues } from 'commander'
import inquirer from 'inquirer'

import { chalk, error, exit, log } from '../../utils/command-helpers.js'
import { chalk, error, exit, log, APIError } from '../../utils/command-helpers.js'
import BaseCommand from '../base-command.js'

export const sitesDelete = async (siteId: string, options: OptionValues, command: BaseCommand) => {
Expand All @@ -17,9 +17,10 @@ export const sitesDelete = async (siteId: string, options: OptionValues, command
try {
siteData = await api.getSite({ siteId })
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
if (error_.status === 404) {
if ((error_ as APIError).status === 404) {
error(`No site with id ${siteId} found. Please verify the siteId & try again.`)
} else {
error(error_)
}
}

Expand Down Expand Up @@ -74,12 +75,10 @@ export const sitesDelete = async (siteId: string, options: OptionValues, command
try {
await api.deleteSite({ site_id: siteId })
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
if (error_.status === 404) {
if ((error_ as APIError).status === 404) {
error(`No site with id ${siteId} found. Please verify the siteId & try again.`)
} else {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
error(`Delete Site error: ${error_.status}: ${error_.message}`)
error(`Delete Site error: ${(error_ as APIError).status}: ${(error_ as APIError).message}`)
}
}
log(`Site "${siteId}" successfully deleted!`)
Expand Down
7 changes: 4 additions & 3 deletions src/commands/status/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import clean from 'clean-deep'
import { OptionValues } from 'commander'
import prettyjson from 'prettyjson'

import { chalk, error, exit, getToken, log, logJson, warn } from '../../utils/command-helpers.js'
import { chalk, error, exit, getToken, log, logJson, warn, APIError } from '../../utils/command-helpers.js'
import BaseCommand from '../base-command.js'

export const status = async (options: OptionValues, command: BaseCommand) => {
Expand Down Expand Up @@ -31,9 +31,10 @@ export const status = async (options: OptionValues, command: BaseCommand) => {
// eslint-disable-next-line @typescript-eslint/no-extra-semi
;[accounts, user] = await Promise.all([api.listAccountsForUser(), api.getCurrentUser()])
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
if (error_.status === 401) {
if ((error_ as APIError).status === 401) {
error('Your session has expired. Please try to re-authenticate by running `netlify logout` and `netlify login`.')
} else {
error(error_)
}
}

Expand Down
1 change: 0 additions & 1 deletion src/commands/watch/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ export const watch = async (options: OptionValues, command: BaseCommand) => {
)
console.timeEnd('Deploy time')
} catch (error_) {
// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
error(error_)
}
}
2 changes: 0 additions & 2 deletions src/lib/exec-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ export const fetchLatestVersion = async ({ destination, execName, extension, lat
Please open up an issue on our CLI repository so that we can support it:
${issueLink}`)
}

// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
error(error_)
}
}
Loading

0 comments on commit e9623e6

Please sign in to comment.