diff --git a/__tests__/git.test.ts b/__tests__/git.test.ts index 44da5e4dca..69e9bfac55 100644 --- a/__tests__/git.test.ts +++ b/__tests__/git.test.ts @@ -80,7 +80,7 @@ describe('git', () => { try { await init(action) } catch (error) { - expect(error.message).toBe( + expect(error instanceof Error && error.message).toBe( 'There was an error initializing the repository: Mocked throw ❌' ) } @@ -419,7 +419,7 @@ describe('git', () => { try { await deploy(action) } catch (error) { - expect(error.message).toBe( + expect(error instanceof Error && error.message).toBe( 'The deploy step encountered an error: Mocked throw ❌' ) } diff --git a/__tests__/ssh.test.ts b/__tests__/ssh.test.ts index b9dc73cde6..394815063b 100644 --- a/__tests__/ssh.test.ts +++ b/__tests__/ssh.test.ts @@ -133,7 +133,7 @@ describe('configureSSH', () => { try { await configureSSH(action) } catch (error) { - expect(error.message).toBe( + expect(error instanceof Error && error.message).toBe( 'The ssh client configuration encountered an error: Mocked throw ❌' ) } diff --git a/__tests__/util.test.ts b/__tests__/util.test.ts index a7ade4a396..fa5e1c6ab4 100644 --- a/__tests__/util.test.ts +++ b/__tests__/util.test.ts @@ -6,7 +6,8 @@ import { generateFolderPath, suppressSensitiveInformation, checkParameters, - stripProtocolFromUrl + stripProtocolFromUrl, + extractErrorMessage } from '../src/util' describe('util', () => { @@ -222,7 +223,7 @@ describe('util', () => { try { checkParameters(action) } catch (e) { - expect(e.message).toMatch( + expect(e instanceof Error && e.message).toMatch( 'No deployment token/method was provided. You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy. If you wish to use an ssh deploy token then you must set SSH to true.' ) } @@ -242,7 +243,7 @@ describe('util', () => { try { checkParameters(action) } catch (e) { - expect(e.message).toMatch( + expect(e instanceof Error && e.message).toMatch( 'No deployment token/method was provided. You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy. If you wish to use an ssh deploy token then you must set SSH to true.' ) } @@ -262,7 +263,7 @@ describe('util', () => { try { checkParameters(action) } catch (e) { - expect(e.message).toMatch('Branch is required.') + expect(e instanceof Error && e.message).toMatch('Branch is required.') } }) @@ -280,7 +281,7 @@ describe('util', () => { try { checkParameters(action) } catch (e) { - expect(e.message).toMatch( + expect(e instanceof Error && e.message).toMatch( 'You must provide the action with a folder to deploy.' ) } @@ -301,7 +302,7 @@ describe('util', () => { action.folderPath = generateFolderPath(action) checkParameters(action) } catch (e) { - expect(e.message).toMatch( + expect(e instanceof Error && e.message).toMatch( `The directory you're trying to deploy named notARealFolder doesn't exist. Please double check the path and any prerequisite build scripts and try again. ❗` ) } @@ -327,4 +328,22 @@ describe('util', () => { ) }) }) + + describe('extractErrorMessage', () => { + it('gets the message of a Error', () => { + expect(extractErrorMessage(new Error('a error message'))).toBe( + 'a error message' + ) + }) + + it('gets the message of a string', () => { + expect(extractErrorMessage('a error message')).toBe('a error message') + }) + + it('gets the message of a object', () => { + expect(extractErrorMessage({special: 'a error message'})).toBe( + `{"special":"a error message"}` + ) + }) + }) }) diff --git a/__tests__/worktree.error.test.ts b/__tests__/worktree.error.test.ts index 0bbf7df6fd..73f91410f1 100644 --- a/__tests__/worktree.error.test.ts +++ b/__tests__/worktree.error.test.ts @@ -28,7 +28,7 @@ describe('generateWorktree', () => { true ) } catch (error) { - expect(error.message).toBe( + expect(error instanceof Error && error.message).toBe( 'There was an error creating the worktree: Mocked throw ❌' ) } diff --git a/package.json b/package.json index e50cd1314a..ec87377135 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,6 @@ "prettier": "2.4.1", "rimraf": "3.0.2", "ts-jest": "26.5.6", - "typescript": "4.3.5" + "typescript": "4.4.3" } } diff --git a/src/git.ts b/src/git.ts index 4d733aa47c..c91d3c2964 100644 --- a/src/git.ts +++ b/src/git.ts @@ -4,7 +4,11 @@ import fs from 'fs' import {ActionInterface, Status, TestFlag} from './constants' import {execute} from './execute' import {generateWorktree} from './worktree' -import {isNullOrUndefined, suppressSensitiveInformation} from './util' +import { + extractErrorMessage, + isNullOrUndefined, + suppressSensitiveInformation +} from './util' /* Initializes git in the workspace. */ export async function init(action: ActionInterface): Promise { @@ -63,7 +67,7 @@ export async function init(action: ActionInterface): Promise { } catch (error) { throw new Error( `There was an error initializing the repository: ${suppressSensitiveInformation( - error.message, + extractErrorMessage(error), action )} ❌` ) @@ -208,7 +212,7 @@ export async function deploy(action: ActionInterface): Promise { } catch (error) { throw new Error( `The deploy step encountered an error: ${suppressSensitiveInformation( - error.message, + extractErrorMessage(error), action )} ❌` ) diff --git a/src/lib.ts b/src/lib.ts index 94738cf8b4..5703e7c0c7 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -4,6 +4,7 @@ import {deploy, init} from './git' import {configureSSH} from './ssh' import { checkParameters, + extractErrorMessage, generateFolderPath, generateRepositoryPath, generateTokenType @@ -52,7 +53,7 @@ export default async function run( status = await deploy(settings) } catch (error) { status = Status.FAILED - setFailed(error.message) + setFailed(extractErrorMessage(error)) } finally { info( `${ diff --git a/src/ssh.ts b/src/ssh.ts index 7df9e3a3d8..1c90392018 100644 --- a/src/ssh.ts +++ b/src/ssh.ts @@ -3,7 +3,7 @@ import {mkdirP} from '@actions/io' import {execFileSync, execSync} from 'child_process' import {appendFileSync} from 'fs' import {ActionInterface} from './constants' -import {suppressSensitiveInformation} from './util' +import {extractErrorMessage, suppressSensitiveInformation} from './util' export async function configureSSH(action: ActionInterface): Promise { try { @@ -46,7 +46,7 @@ export async function configureSSH(action: ActionInterface): Promise { } catch (error) { throw new Error( `The ssh client configuration encountered an error: ${suppressSensitiveInformation( - error.message, + extractErrorMessage(error), action )} ❌` ) diff --git a/src/util.ts b/src/util.ts index 7f67b14983..dba4a4193e 100644 --- a/src/util.ts +++ b/src/util.ts @@ -90,6 +90,13 @@ export const suppressSensitiveInformation = ( return value } +export const extractErrorMessage = (error: unknown): string => + error instanceof Error + ? error.message + : typeof error == 'string' + ? error + : JSON.stringify(error) + /** Strips the protocol from a provided URL. */ export const stripProtocolFromUrl = (url: string): string => url.replace(/^(?:https?:\/\/)?(?:www\.)?/i, '').split('/')[0] diff --git a/src/worktree.ts b/src/worktree.ts index e6cde73e6c..9ef4722211 100644 --- a/src/worktree.ts +++ b/src/worktree.ts @@ -1,7 +1,7 @@ import {info} from '@actions/core' import {ActionInterface} from './constants' import {execute} from './execute' -import {suppressSensitiveInformation} from './util' +import {extractErrorMessage, suppressSensitiveInformation} from './util' export class GitCheckout { orphan = false @@ -77,7 +77,7 @@ export async function generateWorktree( } catch (error) { throw new Error( `There was an error creating the worktree: ${suppressSensitiveInformation( - error.message, + extractErrorMessage(error), action )} ❌` ) diff --git a/yarn.lock b/yarn.lock index d50402c2fe..b3d36d61e1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4571,10 +4571,10 @@ typedarray-to-buffer@^3.1.5: dependencies: is-typedarray "^1.0.0" -typescript@4.3.5: - version "4.3.5" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.3.5.tgz#4d1c37cc16e893973c45a06886b7113234f119f4" - integrity sha512-DqQgihaQ9cUrskJo9kIyW/+g0Vxsk8cDtZ52a3NGh0YNTfpUSArXSohyUGnvbPazEPLu398C0UxmKSOrPumUzA== +typescript@4.4.3: + version "4.4.3" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.4.3.tgz#bdc5407caa2b109efd4f82fe130656f977a29324" + integrity sha512-4xfscpisVgqqDfPaJo5vkd+Qd/ItkoagnHpufr+i2QCHBsNYp+G7UAoyFl8aPtx879u38wPV65rZ8qbGZijalA== union-value@^1.0.0: version "1.0.1"