Skip to content

Commit

Permalink
feat: improve error handling
Browse files Browse the repository at this point in the history
Replace error code ERR_XPI_FILE with ERR_INVALID_INPUT. Remove the use
of `process.exit` when checking inputs; throw an error instead. This
helps us do the further unit tests.
  • Loading branch information
wdzeng committed Aug 16, 2024
1 parent 2844b7e commit 6683a27
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { CustomError } from 'ts-custom-error'

import { stringify } from '@/utils'

export const ERR_XPI_FILE = 1
export const ERR_XPI_VALIDATION_FAILED = 2
export const ERR_XPI_VALIDATION_TIMEOUT = 4
export const ERR_INVALID_INPUT = 5
Expand Down
35 changes: 18 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'node:fs'

import * as core from '@actions/core'

import { ERR_INVALID_INPUT, ERR_XPI_FILE, handleError } from '@/error'
import { ERR_INVALID_INPUT, FirefoxAddonActionError, handleError } from '@/error'
import { generateJwtToken, updateAddon, uploadXpi } from '@/firefox-addon'
import { isStringToStringMapping } from '@/utils'

Expand All @@ -17,15 +17,19 @@ function parseReleaseNotes(): undefined | Record<string, string> {
try {
ret = JSON.parse(releaseNotesInput)
} catch {
core.setFailed('Input "release-notes" is not a valid JSON string.')
core.debug(`release-notes: ${releaseNotesInput}`)
process.exit(ERR_INVALID_INPUT)
throw new FirefoxAddonActionError(
'Input "release-notes" is not a valid JSON string.',
ERR_INVALID_INPUT
)
}

if (!isStringToStringMapping(ret)) {
core.setFailed('Input "release-notes" is not a string-to-string mapping.')
core.debug(`release-notes: ${releaseNotesInput}`)
process.exit(ERR_INVALID_INPUT)
throw new FirefoxAddonActionError(
'Input "release-notes" is not a string-to-string mapping.',
ERR_INVALID_INPUT
)
}

return ret
Expand All @@ -35,34 +39,31 @@ function requireXpiFileExists(xpiPath: string): void {
try {
const s = fs.statSync(xpiPath)
if (!s.isFile()) {
core.setFailed(`Not a regular file: ${xpiPath}`)
process.exit(ERR_XPI_FILE)
throw new FirefoxAddonActionError(`Not a regular file: ${xpiPath}`, ERR_INVALID_INPUT)
}
core.debug('The xpi file exists and is a regular file.')
} catch {
core.setFailed(`File not found: ${xpiPath}`)
process.exit(ERR_XPI_FILE)
throw new FirefoxAddonActionError(`File not found: ${xpiPath}`, ERR_INVALID_INPUT)
}
}

function requireValidXpiExtensionName(xpiPath: string) {
const ext = xpiPath.split('.').at(-1)
if (!ext || !['zip', 'xpi', 'crx'].includes(ext)) {
core.setFailed('Input "xpi-path" must have a valid extension name (.zip, .xpi, .crx).')
core.debug(`xpi-path: ${xpiPath}`)
process.exit(ERR_XPI_FILE)
throw new FirefoxAddonActionError(
'Input "xpi-path" must have a valid extension name (.zip, .xpi, .crx).',
ERR_INVALID_INPUT
)
}
}

function requireValidSourceFileExtensionName(f: string) {
if (f.endsWith('.zip') || f.endsWith('.tar.gz') || f.endsWith('.tgz') || f.endsWith('.tar.bz2')) {
return
}
core.setFailed(
'Input "source-file-path" must have a valid extension name (.zip, .tar.gz, .tgz, .tar.bz2).'
throw new FirefoxAddonActionError(
'Input "source-file-path" must have a valid extension name (.zip, .tar.gz, .tgz, .tar.bz2).',
ERR_INVALID_INPUT
)
core.debug(`source-file-path: ${f}`)
process.exit(ERR_INVALID_INPUT)
}

async function run(
Expand Down

0 comments on commit 6683a27

Please sign in to comment.